1

我有一个 BLE 外围设备,可以在 iOS 和 Android 7.0 上与给定的服务/特征 UUID 一起正常工作。在 Android 6.0 Marshmallow 上,即使在描述符上设置了 ENABLE_NOTIFICATION_VALUE,onCharacteristicChanged 方法也不会触发。请帮我弄清楚如何让 onCharacteristicChanged 为运行 Android OS 6.0 Marshmallow 的 Android 设备触发。这是我用来尝试让通知工作的代码:

    boolean success = mBluetoothGatt.setCharacteristicNotification(characteristic, true);
    Log.e(TAG, "set char notification = " + (success ? "It worked :)" : "It did not work :("));

    if (UUID_DATA_PACKET.equals(characteristic.getUuid())) {
        BluetoothGattDescriptor descriptor = characteristic.getDescriptor(
                UUID.fromString(EkoCoreGattAttributes.CLIENT_CHARACTERISTIC_CONFIG));

        descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);

        success = mBluetoothGatt.writeDescriptor(descriptor);
        Log.e(TAG, "set descriptor = " + descriptor.getCharacteristic().getWriteType() + ", success = " + (success ? "It worked :)" : "It did not work :("));
    }

在上面的代码中,setCharacteristicNotification 和 writeDescriptor 调用都返回成功(1 / true / 等)。此外,onDescriptorWrite 回调返回 GATT_SUCCESS。但是,当我们稍后在代码中读取描述符时,会发现通知值仍然设置为禁用。我们尝试了许多解决方案,例如在 setCharacteristicNotification 和 writeDescriptor 调用之间设置延迟,但尚未找到解决此问题的方法。与相关外围设备配对没有问题,但获取通知似乎是不可能的。任何提示将不胜感激。

4

1 回答 1

1

在编写描述符之前添加以下内容解决了我的问题:

characteristic.setWriteType(BluetoothGattCharacteristic.WRIT‌​E_TYPE_DEFAULT);

显然,Android 6 中存在一些错误。我希望这个答案对其他人有所帮助。找到它真是令人头疼。

于 2017-07-25T06:48:09.367 回答