WebBluetoothCG / web-bluetooth

Bluetooth support for the Web.
http://www.w3.org/community/web-bluetooth/
Other
1.37k stars 185 forks source link

watchAdvertisements() should return a "sentinel" object #591

Open reillyeon opened 1 year ago

reillyeon commented 1 year ago

The requestLeScan() object returns a "sentinel" object that allows the developer to observe the status of the scan operation and stop it. This is important because some platform Bluetooth stacks enforce a limitation on how long a scanning session can be active and so it is important for a site to know whether to expect to receive more advertisementreceived events or not so it can keep its UI up to date.

Right now watchAdvertisements() just takes an AbortSignal which can be used to stop the scan but this was based on a pattern copied from the Screen Wake Lock API which we eventually decided against, instead adding the WakeLockSentinel interface. We should make this change to the Web Bluetooth API as well.

Right now BluetoothLEScan doesn't provide an event when the scanning session is stopped (only an attribute) but it could be updated to fire a stop event or something similar.

niklasdahlheimer commented 1 year ago

Any news on this topic?

After calling watchAdvertisements() on a selected device (filter has keepRepeatedDevices= true) the advertisementreceived-eventListener stops after a few seconds (tested with Chrome 112.0.5615.137 on MacBook Pro 2021). Afterwards the device object still has watchingAdvertisements = true set, but thats obviously wrong. Further calls to watchAdvertisements() have no effect. So it's not possible to monitor advertisement parameters of bluetooth devices continuously.

reillyeon commented 1 year ago

Thanks for pointing out the problem with the watchingAdvertisements attribute as well.

We're tracking this on the implementation side as Chromium issue 1345995 which is blocking marking watchAdvertisements() as a stable API.

niklasdahlheimer commented 1 year ago

Thanks for referencing the issue @reillyeon !

So, is there currently any way to scan continuously or in regular intervals with this API at all?

I didn't received any scan results using requestLeScan() which I assume is caused by this issue.

reillyeon commented 1 year ago

Scan frequency will always be limited by what the underlying platform allows but the idea of providing a sentinel object is to make that visible to the application so that it can restart the scan session after an interval.