我需要检查哪些BT耳机当前连接(而不仅仅是配对)的OS 2.0 - 2.3。这样的功能并不存在,直到API版本11,引入蓝牙耳机类的地方。但是,已经存在的类在以前的API被称为BluetoothHeadset,但它不是向公众开放。下面是它的文档:的http://www.kiwidoc.com/java/l/x/android/android/9/p/android.bluetooth/c/BluetoothHeadset.于是,我尝试使用反射来调用isConnected的方法,但我pretty的可怕的反省,我发现了一个错误 java.lang.IllegalArgumentException:如果对象不是这个类的一个实例
。
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
.
我配对的设备使用 BluetoothDevice.getBondedDevices()
的列表,我尝试使用 isConnected()
每一种方法。这里的code:
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.
BluetoothHeadset是通过工控机控制蓝牙耳机服务代理对象。
BluetoothHeadset is a proxy object for controlling the Bluetooth Headset Service via IPC.
使用getProfileProxy(背景下,BluetoothProfile.ServiceListener,int)以获得BluetoothHeadset代理对象。使用closeProfileProxy(INT,BluetoothProfile)以贴心的服务连接。
Use getProfileProxy(Context, BluetoothProfile.ServiceListener, int) to get the BluetoothHeadset proxy object. Use closeProfileProxy(int, BluetoothProfile) to close the service connection.
Android的只支持一个连接蓝牙耳机的时间。每一种方法是受保护的,其相应的权限。
Android only supports one connected Bluetooth Headset at a time. Each method is protected with its appropriate permission.
来源:的http://developer.android.com/reference/android/bluetooth/BluetoothHeadset.html