Calling a private (unpublished) method in the Android API

I need to check which BT headsets are currently connected (and not just paired) in OS 2.0-2.3. This functionality does not exist until API version 11, where the Bluetooth headset class was introduced. But there already existed a class called BluetoothHeadset in previous APIs, but it was not publicly available. Here is the documentation for it: http://www.kiwidoc.com/java/l/x/android/android/9/p/android.bluetooth/c/BluetoothHeadset . So, I tried to use reflection to call the isConnected method, but I'm pretty terrible at reflection, and I get the error java.lang.IllegalArgumentException: object is not an instance of the class .

I got a list of paired devices using BluetoothDevice.getBondedDevices() , and I'm trying to use the isConnected() method for each of them. Here is the code:

 public boolean isBtDevConnected(BluetoothDevice btDev){ boolean connected = false; try { Class<?> BTHeadset = Class.forName("android.bluetooth.BluetoothHeadset"); Method isConnected = BTHeadset.getMethod("isConnected", new Class[] {BluetoothDevice.class}); connected = isConnected.invoke(BTHeadset, new Object[] {btDev}); } } } catch (Exception e) { WriteToLog(e); } return connected; } 

I get an exception on the line that calls the method, but I'm not sure what I'm doing wrong.

+5
android reflection bluetooth headset
source share
1 answer

BluetoothHeadset is a proxy for controlling the Bluetooth headset service through IPC.

Use getProfileProxy (Context, BluetoothProfile.ServiceListener, int) to get the BluetoothHeadset proxy. Use closeProfileProxy (int, BluetoothProfile) to close the service connection.

Android only supports one connected Bluetooth headset at a time. Each method is protected with the appropriate permission.

source: http://developer.android.com/reference/android/bluetooth/BluetoothHeadset.html

0
source share

All Articles