在Nexus 4上使用Android 4.3进行蓝牙低功耗服务发现

jne*_*ewt 7 android bluetooth-lowenergy nexus-4

我正在尝试使用我的nexus 4(android 4.3)的BLE模块(bluegiga BLE112).我可以连接,获取设备的名称,连接到GATT,但服务发现失败.

这是最初的gatt连接(它似乎成功运行:

dev.connectGatt(getBaseContext(), true, btGattCB);
Run Code Online (Sandbox Code Playgroud)

这是关贸总协定的回调:

private BluetoothGattCallback btGattCB = new BluetoothGattCallback() {
    @Override
    public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
        super.onConnectionStateChange(gatt, status, newState);
        if(newState == BluetoothProfile.STATE_CONNECTED){
            Log.i(TAG, "Gatt Connected");
            gatt.discoverServices();
        }
        else if(newState == BluetoothProfile.STATE_DISCONNECTED){
            Log.i(TAG, "Gatt Disconnected");
        }
    }

    @Override
    public void onServicesDiscovered(BluetoothGatt gatt, int status){
        Log.i(TAG,"Status onServiceDiscovered: "+status);   //status code i'm getting here is 129
        List<BluetoothGattService> btServices = gatt.getServices();//try anyway
    }
};
Run Code Online (Sandbox Code Playgroud)

这是我的日志:

09-28 12:58:37.611    4118-4130/com.jnewt.btFive I/PDU? Scan Callback
09-28 12:58:37.611    4118-4130/com.jnewt.btFive I/PDU? Device is: 00:07:80:67:2F:63
09-28 12:58:37.611    4118-4130/com.jnewt.btFive I/PDU? Device Name: BGT GPIO Test
09-28 12:58:43.607    4118-4118/com.jnewt.btFive I/PDU? Scan Timeout
09-28 12:59:13.539    4118-4129/com.jnewt.btFive I/PDU? Gatt Connected
09-28 12:59:43.561    4118-4190/com.jnewt.btFive I/PDU? Service Discovery Failed
09-28 12:59:43.581    4118-4130/com.jnewt.btFive I/PDU? Gatt Disconnected
09-28 13:00:00.920    4118-4129/com.jnewt.btFive I/PDU? Gatt Connected
09-28 13:00:30.902    4118-4130/com.jnewt.btFive I/PDU? Service Discovery Failed
09-28 13:00:30.922    4118-4190/com.jnewt.btFive I/PDU? Gatt Disconnected
09-28 13:01:20.265    4118-4129/com.jnewt.btFive I/PDU? Gatt Connected
09-28 13:01:50.277    4118-4190/com.jnewt.btFive I/PDU? Service Discovery Failed
09-28 13:01:50.297    4118-4130/com.jnewt.btFive I/PDU? Gatt Disconnected
09-28 13:01:56.113    4118-4129/com.jnewt.btFive I/PDU? Gatt Connected
09-28 13:02:26.115    4118-4190/com.jnewt.btFive I/PDU? Service Discovery Failed
09-28 13:02:26.125    4118-4130/com.jnewt.btFive I/PDU? Gatt Disconnected
Run Code Online (Sandbox Code Playgroud)

https://developer.android.com/reference/android/bluetooth/BluetoothGatt.html页面中,我没有看到129被提及为可能的状态(没有一个常数匹配129).

在这一点上,我完全没有想法.我通过测试iphone的类似示例将问题隔离到Android手机.我也尝试了在Play商店提供的几个应用程序,他们有类似的问题(可以连接,获取名称等,但没有服务).

小智 6

在Nexus 7上测试我的应用程序时,实际上今天得到了相同的错误代码.我的问题是,我在短时间内调用了2 X Gatt.connect.也许这有助于你.请注意,您不能在短时间内连接两次传感器设备.


小智 1

嗯,我也有同样的问题。

检查 BluetoothGATT 中常量的二进制值时,我建议简单地使用 129 表示失败。也许,在定义常量时,有人错误地输入了太多的 0,最终结果是 257 而不是 129。(如果是状态代码,则始终查找二进制表示形式,它显示的不仅仅是十进制值)

我绝对不会仅仅为了查明它是以二进制还是十进制形式插入的而去研究 Android 源代码。

   Binary    Dec
-----------  ---
0 1000 0001  129
1 0000 0001  257
0 0000 0101  5
0 0000 1111  15
0 0000 1101  13
0 0000 0111  7
0 0000 0110  6
0 0000 0000  0
0 0000 0011  3
Run Code Online (Sandbox Code Playgroud)

不过,您仍然需要解决这个问题吗?对我来说,重新启动手机两次有帮助。在第二次重新启动时,我首先手动关闭蓝牙。

在错误发生之前,我在应用程序的每次测试运行中都以编程方式关闭并重新激活蓝牙。它完美地工作了近两个月。然后我删除了“浪费时间”的 BT-restart-code ,这个错误出现了,花了我半天时间才检查出来。

检测时间对我来说从来不是问题,设备会立即显示。

开发手机:Nexus 4 / Android 4.3(不断更新) BLE 设备:Texas Instruments CC2541