【问题标题】:Android BluetoothGatt not receving Characteristic Notifications BluetoothGatt#writeDescriptor(desc) return falseAndroid BluetoothGatt 未收到特征通知 BluetoothGatt#writeDescriptor(desc) 返回 false
【发布时间】:2015-06-24 17:25:06
【问题描述】:

我正在开发一个需要与蓝牙 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 获得任何通知的原因。我不知道如何解决这个问题。

这个问题只发生在 LG G2 的 OS 5.02 之前,它有 4.4 问题不是那么频繁,但是在更新之后我每次都得到“假”,除了第一次。 如果我们在连接后第一次尝试设置通知,它会起作用,一旦我断开连接并尝试连接,它总是会返回 False。 我需要杀死并重新启动应用程序才能再次工作。 有谁知道为什么这不起作用?提前致谢

【问题讨论】:

标签: android bluetooth bluetooth-lowenergy lg android-ble


【解决方案1】:

在 Lollypop 中,BluetoothGatt.java 已更新为包含“设备忙”块,可防止应用程序一次请求多个异步操作。请参阅BluetoothGatt.java:1029 从 writeDescriptor 返回的那个。

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

【讨论】:

  • 嗨。你能进一步解释你的解决方案吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-09
  • 2014-08-13
  • 1970-01-01
  • 2015-06-22
  • 1970-01-01
相关资源
最近更新 更多