1

客户端配置特征描述符 (CCCD)。

UUID -“00002902-0000-1000-8000-00805f9b34fb”/“gatt.client_characteristic_configuration”。

要在 Android 的 Java 代码中设置 CCCD,我们这样做:

public static final byte[] ENABLE_NOTIFICATION_VALUE = {0x01, 0x00};

BluetoothGattDescriptor descriptor =    characteristic.getDescriptor("00002902-0000-1000-8000-00805f9b34fb");
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
mBluetoothGatt.writeDescriptor(descriptor);

使用 Web 蓝牙 API 时,如何在 JavaScript 代码中执行类似的配置?

我的 JavaScript 版本:

.then(descriptors => {    
let queue = Promise.resolve();
 descriptors.forEach(descriptor => {
        switch (descriptor.uuid) {    
          case BluetoothUUID.getDescriptor('gatt.client_characteristic_configuration'):
           queue = queue.then(_ => descriptor.readValue()).then(value => {
              descriptorCache = descriptor;
            });
   ...

    var data = new Uint8Array([0x01, 0x00]);
    descriptorCache.writeValue(data);
    //descriptorCache.writeValue(new TextEncoder().encode(data));

因安全错误而失败:-(

Uncaught (in promise) DOMException: writeValue() 在标记为排除写入的阻止列表对象上调用。 https://webbluetoothcg.github.io/web-bluetooth/#attacks-on-devices

我理解安全的必要性。但毕竟很多设备都需要对CCCD进行预设。

4

1 回答 1

1

呼叫characteristic.StartNotifications()为您设置 CCCD。

于 2018-05-02T16:18:16.227 回答