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.
android reflection bluetooth headset
user496854
source share