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

> Hello, I am facing the same problem, any news ?

I've had to deal with this and i've found it's easiest to wrap the `startNotifications` call with a promise and setTimeout to try again if it fails:

```ts
 /** Wrapper around {@link BluetoothRemoteGATTCharacteristic.startNotifications} to try again if the device is busy */
 private async _startNotifications(characteristic: BluetoothRemoteGATTCharacteristic): Promise<void> {
  return new Promise(async (resolve) => {
   let startNotifications = async () => {
    try {
     await characteristic.startNotifications();
     resolve();
    } catch (error) {
     if (!(error instanceof Error) || error.name != 'NotSupportedError')
      throw error;
     console.debug(`Device busy, could not subscribe to characteristic ${characteristic.uuid}. Retrying...`);
     setTimeout(startNotifications, 100);
    }
   };
   await startNotifications();
  });
 }
```

-- 
GitHub Notification of comment by TequinDragon
Please view or discuss this issue at https://github.com/WebBluetoothCG/web-bluetooth/issues/466#issuecomment-2649296499 using your GitHub account


-- 
Sent via github-notify-ml as configured in https://github.com/w3c/github-notify-ml-config

Received on Monday, 10 February 2025 21:37:03 UTC