【发布时间】:2020-09-05 19:10:29
【问题描述】:
我尝试使用 WebBluetooth 连接到 BLE 设备(LEGO Powered UP 设备)。我已经在同一个笔记本上使用了 .NET/WinRT(运行良好),现在我尝试编写一个适配器以在 Blazor 中使用它。 LEGO PoweredUp 设备使用带有通知和 WriteValue 的 BLE GATT 特性实现communication protocol。
设备连接后,它会立即发送一系列通知(作为对之前连接的响应),公开我需要的信息。我能够使用 WinRT 在 .NET 中足够快地设置通知接收器。但是,使用 Chrome 的 WebBluetooth,我只收到 - 取决于时间/迭代 - 在最后 3 和 9 条消息之间(预计 9 条消息)。我想这只是一个常规的比赛条件。
我的问题:这是预期的吗?我可以做点什么吗?
低于最小可行测试(连接到 LEGO Technic Control+ Hub 时应返回 9 条消息)。
function writeToLog(x) {
console.log(x.target.value.buffer);
}
async function connectToLwpDevice() {
const serviceUuid = "00001623-1212-EFDE-1623-785FEABCD123".toLowerCase();
const characteristicUuid = "00001624-1212-EFDE-1623-785FEABCD123".toLowerCase();
const device = await navigator.bluetooth.requestDevice({ filters: [{ services: [serviceUuid] }] });
const connectedDevice = await device.gatt.connect();
const service = await connectedDevice.getPrimaryService(serviceUuid);
const characteristic = await service.getCharacteristic(characteristicUuid);
characteristic.addEventListener('characteristicvaluechanged', writeToLog);
await characteristic.startNotifications();
}
【问题讨论】:
标签: google-chrome lego web-bluetooth