【问题标题】:ENABLE_NOTIFICATION_VALUE - Web Bluetooth APIENABLE_NOTIFICATION_VALUE - 网络蓝牙 API
【发布时间】:2018-05-03 00:18:16
【问题描述】:

客户端配置特征描述符 (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));

因安全错误而失败 :-(

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

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

【问题讨论】:

    标签: javascript bluetooth bluetooth-lowenergy web-bluetooth


    【解决方案1】:

    调用 characteristic.StartNotifications() 为您设置 CCCD。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多