javascript - 网络蓝牙中没有来自特征值更改的通知
问题描述
我有一个 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
解决方案
所以事实证明,我测试网络应用程序的方式是问题所在。我的工作站没有 BLE 加密狗,所以我使用手机访问我的开发服务器。当然,网络蓝牙需要在 localhost 或 https 上运行,所以我只需在 https 上运行开发服务器并通过手机在网络上访问它(如https://192.168.0.x)。即使 chrome 认为它不安全,我也继续进行,但显然只有部分网络蓝牙以这种方式工作。
配对和 writeWithoutResponse 使用未签名的证书。通知没有。
我把这个留在这里,以防其他人犯同样的错误。
推荐阅读
- macos - 在没有 ./ 的情况下在 zsh 中运行脚本之前在 /usr/local/bin 中有效,但在 ~/bin 中无效
- scala - 用户类抛出异常:scala.ScalaReflectionException:classxyxz
- json - NewtonSoft.Json 为所有人返回 null
- python - ImportError:导入_sqlite3时DLL加载失败:找不到指定的模块
- javascript - 在 JS 中装饰函数或对象
- nginx - 带有拆分客户端的 nginx A/B 测试
- python - 如果我使用网格搜索 cv,是否需要做 CV?
- variadic-functions - 通过 JNA 从 Java 调用 C varargs 函数
- python - Django rest 框架 - 包括一个用于访问授权的 api 密钥
- java - Azure/IntelliJ 使用 Cucumber 显示 runScenario 而不是实际测试名称