首页 > 解决方案 > 网络蓝牙中没有来自特征值更改的通知

问题描述

我有一个 LM068 BLE 串行适配器,我正在尝试通过 Web 应用程序与之通信。我已经通过 Android 上的 nRF Connect 应用程序对其进行了测试,通信工作正常。

该特征具有属性“notify”和“writeWithoutResponse”。

调用时characteristic.startNotifications()出现错误“GATT 错误:属性长度无效。”。

调用characteristic.writeValue()成功发送数据,我可以在我的串行监视器中看到传入的数据。当从串口终端发送数据时,characteristicvaluechanged 事件永远不会触发。通知可通过 nRF Connect 应用程序运行。

这是我当前代码的一部分:

const characteristic = await service.getCharacteristic(characteristicName)

try {
  await characteristic.startNotifications()
} catch (e) {
  console.log(e.message)
  // GATT Error: invalid attribute length.
}

const encoder = new TextEncoder('utf-8')
characteristic.writeValue(encoder.encode('test')) // Works

characteristic.addEventListener('characteristicvaluechanged', handleValueChanged) // Never gets called

标签: javascriptwebbluetooth

解决方案


所以事实证明,我测试网络应用程序的方式是问题所在。我的工作站没有 BLE 加密狗,所以我使用手机访问我的开发服务器。当然,网络蓝牙需要在 localhost 或 https 上运行,所以我只需在 https 上运行开发服务器并通过手机在网络上访问它(如https://192.168.0.x)。即使 chrome 认为它不安全,我也继续进行,但显然只有部分网络蓝牙以这种方式工作。

配对和 writeWithoutResponse 使用未签名的证书。通知没有。

我把这个留在这里,以防其他人犯同样的错误。


推荐阅读