I try to connect to a BLE device (a LEGO Powered UP device) using WebBluetooth. I did this already using .NET/WinRT on the same notebook (works nicely) and now I try to write an adapter for using it in Blazor. The LEGO PoweredUp device implements a communication protocol using BLE GATT characteristic w/ Notifications and WriteValue.
As soon as the device is connecting, it is instantly sending a series of notifications (as kind of a response to the connect before) exposing information I need. I am able to setup the notification receiver fast enough in .NET using WinRT. However, with Chrome's WebBluetooth I only receive - depending on the timing/iteration - between the last 3 and 9 message (9 messages is expected). I guess this is just a regular race condition.
My Question: Is this expected? Can I do something against it?
Below a minimal viable test (which should return 9 messages when connected to a LEGO Technic Control+ Hub).
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();
}
I don't believe there is anything that can be done about this at the moment. I've filed an issue against the Web Bluetooth specification to track the changes I believe are necessary in order to enable the reception of these notifications.