Android.bluetooth.IBluetooth.createBond()在4.2.1中找不到,但在早期的操作系统版本中可用
我有一些代码可以通过调用createBond()自动与蓝牙设备配对,为android.bluetooth.device.action.PAIRING_请求注册广播接收器,然后手动输入PIN码进行配对 到目前为止,这在所有测试到Andoid 4.0的设备上都非常有效,但今天我在我的Nexus 7上使用Android 4.2.1尝试了这一功能,并出现以下错误: java.lang.noSuchMethodException:android.bluetooth.IBluetooth.createBond 他们真的从库中删除了这个函数吗 更新 实际发生的是我用来调用createBond的IBluetooth接口对象没有被初始化。在下面的代码中,当此过程失败时,尝试获取名为BTBinder的IBinder的行将返回null,从而导致BTInterface在末尾设置为null。所以,我现在的问题是,为什么在我的Nexus7和Android 4.2.1上,获取绑定器的调用返回空值,但在我测试过的其他5台设备上正常工作Android.bluetooth.IBluetooth.createBond()在4.2.1中找不到,但在早期的操作系统版本中可用,android,bluetooth,Android,Bluetooth,我有一些代码可以通过调用createBond()自动与蓝牙设备配对,为android.bluetooth.device.action.PAIRING_请求注册广播接收器,然后手动输入PIN码进行配对 到目前为止,这在所有测试到Andoid 4.0的设备上都非常有效,但今天我在我的Nexus 7上使用Android 4.2.1尝试了这一功能,并出现以下错误: java.lang.noSuchMethodException:android.bluetooth.IBluetooth.createBon
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;
}
公共静态IBluetooth GetBluetooth接口()
{
//从专用Android系统API获取蓝牙接口
IBluetooth BTInterface=null;
尝试
{
Class ServiceManager=Class.forName(“android.os.ServiceManager”);
方法getService=ServiceManager.getDeclaredMethod(“getService”,String.class);
IBinder BTBinder=(IBinder)getService.invoke(null,“蓝牙”);
Class IBluetooth=Class.forName(“android.bluetooth.IBluetooth”);
类[]IBluetooth类=IBluetooth.getDeclaredClasses();
类别IBluetoothClass0=IBluetoothClasses[0];
方法asInterface=IBluetoothClass0.getDeclaredMethod(“asInterface”,IBinder.class);
asInterface.setAccessible(true);
BTInterface=(IBluetooth)asInterface.invoke(null,BTBinder);
}
捕获(例外e)
{
返回null;
}
返回接口;
}
在Android 4.2中,他们更改了蓝牙协议栈的实现
“安卓4.2引入了一个新的蓝牙协议栈,该协议栈针对安卓设备进行了优化。谷歌和Broadcom合作开发的新蓝牙协议栈取代了基于BlueZ的协议栈,提高了兼容性和可靠性。”
甚至在Nexus7上的公共api中,也有很多与bt相关的东西不起作用
public boolean createBond(BluetoothDevice btDevice)
throws Exception
{
Class class1 = Class.forName("android.bluetooth.BluetoothDevice");
Method createBondMethod = class1.getMethod("createBond");
Boolean returnValue = (Boolean) createBondMethod.invoke(btDevice);
return returnValue.booleanValue();
}
这在4.2.1 Galaxy Nexus上起作用。还没有在Nexus7上尝试过,但在使用IBluetooth方法时,我遇到了MethodNotFoundException的相同问题。因此,Nexus7也可能会修复。有人知道在哪里可以看到新的接口吗?我使用这个:但它还没有4.2。我对tuis问题非常感兴趣。如果你有一些消息,请分享:p。