网络蓝牙从设备GATT

时间:2019-04-08 07:34:13

标签: javascript bluetooth-lowenergy gatt bluetooth-gatt web-bluetooth

我的目标是获取存储在设备中的数据。

像正在测量温度或任何温度的设备并将其存储到内存中。我需要通过记录访问控制点(RACP)查询所有这些数据设备。

  1. 实现这一目标的第一个想法是

    • 获取特征

    • 开始通知

    • 将代码写入描述符

    • 通过eventListener获取所有数据

结果:启动通知时引发错误 使用的示例:

https://googlechrome.github.io/samples/web-bluetooth/notifications-async-await.html https://bugs.chromium.org/p/chromium/issues/detail?id=664863

  1. 接下来的想法是由于特征是 指示,写类型。 因此,正在考虑添加侦听器并写入设备文档中的描述符代码,其中指出:
  

操作码:   1 –报告存储的记录

即使删除了startNotifications行,也会引发错误 所以我的代码示例是:

const mainService = 'my correct service uuid';
    const characteristicUUID1 = 'my correct char uuid';
    const characteristicUUID2 = 'my correct char uuid';
    const descriptorUUID = '00002902-0000-1000-8000-00805f9b34fb';
    let deviceCache = null;
    let serverCache = null;
    let serviceCache = null;
    let characteristicCacheA = null;
    let characteristicCacheB = null;
    let descriptorCache = null;

    try {
      deviceCache = await navigator.bluetooth.requestDevice({ filters: [{ name: 'my device' }] });

      console.log('Connecting to GATT Server...');
      serverCache = await deviceCache.gatt.connect();

      console.log('Getting Services...');
      serviceCache = await serverCache.getPrimaryService(mainService);

      console.log('Getting Characteristics A...');
      characteristicCacheA = await serviceCache.getCharacteristic(characteristicUUID1);

      console.log('Start Notifications A...');
      await characteristicCacheA.startNotifications();

      console.log('Getting Characteristics B...');
      characteristicCacheB = await serviceCache.getCharacteristic(characteristicUUID2);

      console.log('Start Notifications B...');
      await characteristicCacheB.startNotifications();

      console.log('Add event listener...');
      characteristicCacheA.addEventListener('characteristicvaluechanged', this.handleNotifications);

      console.log('Getting Descriptor...');
      descriptorCache = await characteristicCacheA.getDescriptor(descriptorUUID);

      console.log('Write value to descr...');
      await descriptorCache.writeValue(new Uint8Array([1]));


    } catch (error) {
      console.log(error.message, 'error');
    }

通知错误是(具有实验性chrome功能,不会引发错误):

  

错误:GATT操作由于未知原因失败。

描述符错误为:

  

在标记为排除-写入的被列入黑名单的对象上调用writeValue()。

我的设备也在要求输入密码,但是网络在连接时未提示任何内容。因此,也许它说阻止写入描述符。

如何处理图钉输入-不知道(一旦启用了chrome实验功能后,系统提示我输入图钉,不确定是否相关)。

我的逻辑正确吗? -不要这样。

有什么建议吗?

到目前为止我已经调查了什么?

  1. https://googlechrome.github.io/samples/web-bluetooth/
  2. https://www.oreilly.com/library/view/getting-started-with/9781491900550/ch04.html
  3. https://webbluetoothcg.github.io/web-bluetooth/

编辑:阅读本文后-https://medium.com/@devdevcharlie/experimenting-with-web-bluetooth-1f1176047ddd

我认为正确的逻辑应该写入所需的命令特征命令(例如获取所有数据)。之后,从设备文档中找到负责此数据的正确特征,并启动通知并添加eventListener并接收数据。

2 个答案:

答案 0 :(得分:0)

The call to writeValue() is failing because access to the CCCD is on the blocklist. The call to startNotifications() will write to the descriptor as necessary to enable notifications.

We need to investigate this "unknown reason" for startNotifications() failing. What operating system are you using? Please follow the instructions for reporting Web Bluetooth bugs and file an issue in the Chromium project's issue tracker.

答案 1 :(得分:0)

就目前而言,Chrome无法或无法在Windows 10上与受保护的特征进行通信,在macOS上,Chrome可以正常运行。如果有人正在观看,我已经在铬臭虫追踪器上发布了问题。 https://bugs.chromium.org/p/chromium/issues/detail?id=960258#c6