其他分享
首页 > 其他分享> > Android BluetoothGatt无法接收特征通知BluetoothGatt#writeDescriptor(desc)返回false

Android BluetoothGatt无法接收特征通知BluetoothGatt#writeDescriptor(desc)返回false

作者:互联网

我正在开发需要与Bluetooth LE设备通信的应用程序.

这是我用来设置CharacteristicNotification的代码

public boolean setCharacteristicNotification(
    BluetoothGattCharacteristic characteristic, boolean enable) {

if(mBluetoothAdapter == null || mBluetoothGatt == null) {
    Log.w(TAG, "BluetoothAdapter not initialized");
    return false;
}

Log.v(TAG, "setCharacteristicNotification(): uuid=" + characteristic.getUuid() + " enabled=" + enable);

boolean notifications = mBluetoothGatt.setCharacteristicNotification(characteristic, enable);

if(notifications) {
    Log.v(TAG, "setCharacteristicNotification(): Notifications are enabled");
}
else {
    Log.w(TAG, "setCharacteristicNotification(): Notifications are not enabled for characteristic " + characteristic);
}

BluetoothGattDescriptor desc = characteristic.getDescriptor(
        UUID.fromString(FBGattAttributes.CHARACTERISTIC_CLIENT_CONFIG));

desc.setValue(enable ?
                BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE :
                new byte[]{0x00, 0x00}
);

boolean ok = mBluetoothGatt.writeDescriptor(desc);

if(ok){
    Log.v(TAG, "wrote descriptor value for notification: ok=" + ok);
}else{
    Log.w(TAG, "writeDescriptor failed: we will not get notifications=" + ok);
}


return ok;
}

在此代码中,“ mBluetoothGatt.writeDescriptor(desc);”有时返回false,这就是我无法从BluetoothGatt收到任何通知的原因.我不确定如何解决此问题.

此问题仅发生在具有OS 5.02的LG G2之前,该操作系统版本为4.4.问题不是那么频繁,但是在更新之后,除第一次以外,我每次都得到“假”消息.
如果我们尝试在连接后第一次设置通知,那么它将起作用,并且一旦我断开连接并尝试连接,它将始终返回False.
我需要杀死并重新启动该应用程序才能使其再次运行.
有谁知道为什么这不起作用?提前致谢

解决方法:

在Lollypop中,对BluetoothGatt.java进行了更新,以包含“设备繁忙”块,这些块可防止应用程序一次请求多个异步操作.请参阅BluetoothGatt.java:1029以了解从writeDescriptor返回的内容.

我不得不设置一个命令队列来解决这个问题.基本上,我的逻辑是将所有失败的异步调用加入队列并在我的BluetoothGattCallback中执行重试.

标签:bluetooth,bluetooth-lowenergy,lg,android-ble,android
来源: https://codeday.me/bug/20191028/1950369.html