我需要检查当前在 OS 2.0 - 2.3 中连接了哪些 BT 耳机(不仅仅是配对)。此类功能直到 API 版本 11 才出现,其中引入了蓝牙耳机类。但是在以前的 API 中已经存在一个名为 BluetoothHeadset 的类,但它不能公开访问。这是它的文档:http://www.kiwidoc.com/java/l/x/android/android/9/p/android.bluetooth/c/BluetoothHeadset .所以,我试图使用反射来调用“isConnected”方法,但我在反射时非常糟糕,并且出现错误 java.lang.IllegalArgumentException: object is not an instance of the class
。
我使用 BluetoothDevice.getBondedDevices()
获得了配对设备列表,我尝试对每个设备使用 isConnected()
方法。这是代码:
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;
}
我在调用该方法的那一行得到了异常,但我不确定我做错了什么。
最佳答案
BluetoothHeadset 是一个代理对象,用于通过 IPC 控制蓝牙耳机服务。
使用 getProfileProxy(Context, BluetoothProfile.ServiceListener, int) 获取 BluetoothHeadset 代理对象。使用 closeProfileProxy(int, BluetoothProfile) 关闭服务连接。
Android 一次只支持连接一个蓝牙耳机。每种方法都受到适当权限的保护。
来源:http://developer.android.com/reference/android/bluetooth/BluetoothHeadset.html
关于android - 在 Android API 中调用私有(private)(未发布)方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6020421/