-
Notifications
You must be signed in to change notification settings - Fork 188
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Generate 'characteristicvaluechanged' events without waiting for a CCCD update #514
Comments
👍 for having this API match reality. Do you think it'd make sense to ask developers to explicitly opt into unsolicited notifications and indications? For example: const device = await navigator.bluetooth.requestDevice({ filters: [{ services: [serviceUuid] }] });
device.addEventListener('characteristicvaluechanged', () => { ... });
device.gatt.deliverUnsolicitedNotifications = true;
const server = await device.gatt.connect();
const service = await server.getPrimaryService(serviceUuid);
const characteristic = await service.getCharacteristic(characteristicUuid);
await characteristic.startNotifications(); Without that, I worry a bit that a device could attack a page by sending unexpected notifications ... maybe if the device runs multiple apps each constrained to their own service... |
That seems like a reasonable mitigation to put in place. What do you think about a property on the |
I don't have a strong opinion. Putting it in |
If an event handler is attached after a device is connected, then there is a race condition where notifications could be missed, so +1 for passing it to the |
@reillyeon @jyasskin Has this ended up being on someone's agenda 😀. I do not know exactly how this WG works in relation to the browser vendors. |
In this StackOverflow question a developer explains that they are trying to use Web Bluetooth to communicate with a device that appears to generate a number of Characteristic Value Notifications immediately after the GATT connection has been established. If they perform the following sequence of calls there is a high probability that some of the notifications will be lost.
The issue is that each of these is an asynchronous operation which takes some time to complete and the
startNotifications()
call in particular depends on being able to update the Client Characteristic Configuration descriptor in order to enable notifications. While the device is not complying with the Bluetooth specification by sending notifications before requested the reality is that other Bluetooth APIs pass on these unsolicited notifications.Properly fixing this seems to require two changes. First, the active notification context set must be removed and the steps for responding to notifications and indications updated to deliver events to all active Bluetooth globals regardless of whether
startNotifications()
orstopNotifications()
have been called. Second, the bubbling of events through the Bluetooth tree needs to be stabilized so that the code above can be rewritten to the following.In this example an attempt to update the CCCD is made by calling
startNotifications()
but an event listener is added to the BluetoothDevice object to catch all characteristic value change notifications that are delivered before that process is complete.The text was updated successfully, but these errors were encountered: