I need to check which BT headsets are currently connected (not just paired) in OS 2.0 - 2.3. Such functionality doesn't exist until API version 11, where a Bluetooth Headset class was introduced. But there already existed a class called BluetoothHeadset in prior APIs, but it wasn't publicly accessible. Here's the documentation for it: http://www.kiwidoc.com/java/l/x/android/android/9/p/android.bluetooth/c/BluetoothHeadset. So, I was trying to use reflection to invoke the "isConnected" method, but I'm pretty horrible at reflection, and I'm getting an error java.lang.IllegalArgumentException: object is not an instance of the class
.
I got a list of paired devices using BluetoothDevice.getBondedDevices()
, and I try to use the isConnected()
method on each one. Here's 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 the exception on the line that invokes the method, but I'm not sure what I'm doing wrong.