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;
}

解决方法

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

“Android 4.2引入了一种针对Android设备进行优化的新型蓝牙堆栈,由Google和Broadcom合作开发的新蓝牙解决方案替代了基于BlueZ的堆栈,并提供了更好的兼容性和可靠性.

Nexus 7上的公开api有很多bt相关的东西不起作用.

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...