Android.bluetooth.IBluetooth.createBond()在4.2.1中找不到,但适用于早期的OS版本

CHo*_*n82 10 android bluetooth

我有一些代码通过调用createBond()自动与蓝牙设备配对,为android.bluetooth.device.action.PAIRING_REQUEST注册广播接收器,然后手动输入要配对的PIN码.

到目前为止,所有经过测试的设备都可以用到Andoid 4.0,但今天我在Android 4.2.1的Nexus 7上尝试了这个并且出现了以下错误:

java.lang.noSuchMethodException:android.bluetooth.IBluetooth.createBond

他们真的从库中删除了这个功能吗?

UPDATE

实际发生的是我用来调用createBond的IBluetooth接口对象没有被初始化.在下面的代码中,当此进程失败导致BTInterface在结尾处设置为null时,尝试获取名为BTBinder的IBinder的行返回null.所以,我现在的问题是为什么在我的带有Android 4.2.1的Nexus 7上调用绑定器返回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;
}
Run Code Online (Sandbox Code Playgroud)

小智 6

在Android 4.2中,他们改变了蓝牙堆栈的实现.

"Android 4.2推出了一款针对Android设备优化的新蓝牙堆栈.Google和Broadcom合作开发的新蓝牙堆栈取代了基于BlueZ的堆栈,提供了更高的兼容性和可靠性."

即使是Nexus 7上的公共API,也有很多相关的东西无法正常工作.