[web-bluetooth] startNotifications either gives errors or ties up gatt operations (#466)

alexistbell has just created a new issue for https://github.com/WebBluetoothCG/web-bluetooth:

== startNotifications either gives errors or ties up gatt operations ==
Chrome 79.0.3945.130
Mac OSX 10.14.6

I'm trying to use Web Bluetooth to control a Bluetooth device and have been running into issues with .startNotifications(). I either get errors back from it, or in the few cases with no errors it seems to tie up the gattOperation until gatt disconnects (or possibly it's causing the disconnection).

I am successfully able to connect to the device, get services, get characteristics, and write characteristics. However some of what I want to do requires listening for events from the Bluetooth device. I’ve tried setting this up in a couple ways.

My first attempt was using async await like this:
try {
  await device.gatt.connect();
  const commandService = await device.gatt.getPrimaryService(UUIDS.commandService);
  const commandCharacterstic = await commandService.getCharacteristic(UUIDS.commandCharacteristic);
  await commandCharacteristic.startNotifications();
  await commandCharacteristic.addEventListener('characteristicvaluechanged', handleEvent));   
} catch (e) {
  throw Error(e);

const handleEvent = (event) => {

This would give me these errors:
 DOMException: GATT operation failed for unknown reason.
Error: NotSupportedError: GATT operation failed for unknown reason.

When I got to .startNotifications line. If I dont’t try and setup notifications I am able to write to that characteristic just fine, so my connection is there and my UUIDS are correct.

I tried mimic the code [from googles samples](https://googlechrome.github.io/samples/web-bluetooth/notifications.html) more exactly:

.then((service) => {
.then((characteristic) => {
  const myChar = characteristic;
  return myChar.startNotifications()
    .then((_) => {
      console.log('notifications started');
      characteristic.addEventListener('characteristicvaluechanged', handleEvent);
.catch((e) => {
  throw Error(e);

This gives me the error:

`Cannot read property 'startNotifications' of undefined`

This variation at first seemed the most sucessful:

  const commandService = await device.gatt.getPrimaryService(UUIDS.commandService);
  const commandCharacteristic = await commandService.getCharacteristic(UUIDS.commandCharacteristic);

    .then(() => {
      commandCharacteristic.addEventListener('characteristicvaluechanged', handleEvent)
        .then(() => console.log(commandCharacteristic));
    .catch((reject) => {

But I never got the console log from the .then of `notifications started`. I tried sending my command anyway to see if I would get my response back but got the `Gatt Operation in progress` error which lead me to [this thread](https://github.com/WebBluetoothCG/web-bluetooth/issues/188)

After that I started delaying the commands sent after setting up notifications and ran into some interesting behavior. With a delay of 1000ms I would still get the `Gatt Operation in progress` error.

At 1100ms I would get this error:

N`etworkError: GATT Server is disconnected. Cannot retrieve services. (Re)connect first with `device.gatt.connect`.`

[MDN](https://developer.mozilla.org/en-US/docs/Web/API/BluetoothRemoteGATTCharacteristic/startNotifications) still has it labeled as experimental, but implies that if I turn on the flag for `#enable-experimental-web-platform-features` I should be able to use it in Chrome. I’ve tried it both with the flag set and unset, and have seen no difference.

Is this feature still too experimental to use? Is there some key piece I’m missing? Is there some way to know when notifications have been successfully started?

I did also find what might be the same sort of issue [here](https://stackoverflow.com/questions/54315828/web-bluetooth-misses-initial-notifications).

Please view or discuss this issue at https://github.com/WebBluetoothCG/web-bluetooth/issues/466 using your GitHub account

Received on Friday, 24 January 2020 16:37:41 UTC