android4.0 bluez版本,Android.bluetooth.IBluetooth.createBond()在4.2.1中找不到,但在较早的操作系统版本中可用...
我有一些代码可以通过调用createBond()自动与蓝牙设备配对,注册一个用于
android.bluetooth.device.action.PAIRING_REQUEST的广播接收器,然后手动输入PIN码进行配对.
这已经很好,所有测试的设备到目前为止,Andoid 4.0,但今天我尝试这个在我的Nexus 7与Android 4.2.1并得到以下错误:
java.lang.noSuchMethodException:android.bluetooth.IBluetooth.createBond
他们实际上从库中删除了这个功能吗?
UPDATE
实际发生的是IBluetooth接口对象,我用来调用createBond没有被初始化.在以下代码中,尝试获取名为BTBinder的IBinder的行将在此过程失败导致BTInterface在结尾设置为null时返回null.所以现在我的问题是为什么在我的Nexus 7与Android 4.2.1做的调用来获取绑定返回null,但正确的5其他设备我测试过?
public static IBluetooth getBluetoothInterface()
{
//Gets a bluetooth interface from private Android system API
IBluetooth BTInterface = null;
try
{
Class> ServiceManager = Class.forName("android.os.ServiceManager");
Method getService = ServiceManager.getDeclaredMethod("getService",String.class);
IBinder BTBinder = (IBinder) getService.invoke(null,"bluetooth");
Class> IBluetooth = Class.forName("android.bluetooth.IBluetooth");
Class>[] IBluetoothClasses = IBluetooth.getDeclaredClasses();
Class> IBluetoothClass0 = IBluetoothClasses[0];
Method asInterface = IBluetoothClass0.getDeclaredMethod("asInterface",IBinder.class);
asInterface.setAccessible(true);
BTInterface = (IBluetooth) asInterface.invoke(null,BTBinder);
}
catch (Exception e)
{
return null;
}
return BTInterface;
}