javascript - ENABLE_NOTIFICATION_VALUE - Web 蓝牙 API

标签 javascript bluetooth bluetooth-lowenergy web-bluetooth

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

因安全错误而失败:-(

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

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

最佳答案

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

关于javascript - ENABLE_NOTIFICATION_VALUE - Web 蓝牙 API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50097579/

相关文章:

javascript - Knockout 和 breeze.js take 方法不工作分页

javascript - 某些语言的 SignalR 客户端调用失败

javascript - 方法未捕获安全错误: Blocked a frame with origin

bluetooth - 蓝牙 5.x/LE 音频是否也能改善通话? (HSP/HFP 配置文件)

android - 如何使用任何模拟 Api for junit 模拟蓝牙适配器

ios - iOS BLE特征控制点写入-ATT错误

javascript - 我可以从 JavaScript 动态更改/设置 Google Maps API key 吗?

iphone - 如何在 iPad/iPhone 上录制蓝牙耳机的音频?

IOS 启用指示与通知

ios - 如何从具有相同服务UUID的两个设备中查找特定的BLE 4.0外设