How to avoid reconnecting an already paired device via WebBluetooth on every page reload. #1457
Replies: 19 comments
-
Posted at 2021-04-30 by Robin Thr 2021.04.29
Although I haven't tried this, what about using the Html
or possibly using the onload() event, set a variable within the page Global scope that bypasses the Html section that loads the PuckJs code.
|
Beta Was this translation helpful? Give feedback.
-
Posted at 2021-04-30 by @fanoush
The WebIDE at https://www.espruino.com/ide/ can keep previous connection info and reconnect, however it is also lost when you reload the page. I think this is by design and is some sort of security so devices are not silently accessed across different pages. |
Beta Was this translation helpful? Give feedback.
-
Posted at 2021-04-30 by @gfwilliams Hi! This is something I have been wondering about actually. I think realistically to do it nicely, it could do with being integrated into the Puck.js/UART.js library itself (at https://github.com/espruino/EspruinoWebTools). I think long term the best thing would be to introduce the functionality to UART.js (which already has the ability to display a menu for >1 device), and then maybe just make Puck.js a stripped down version of UART.js that doesn't do Web Serial (it's getting stupid having two extremely similar libraries).
Yes, I was wondering about that. On Web Serial it works totally as expected, but it seems it doesn't on Web Bluetooth. Having said that, this example from the Chrome team themselves doesn't appear to remember devices: https://googlechrome.github.io/samples/web-bluetooth/watch-advertisements-and-connect.html But this one does - at least for me: https://googlechrome.github.io/samples/web-bluetooth/watch-advertisements-and-connect-async-await.html But apart from the usage of promises vs. async/await I'm having trouble figuring out why! However if we can figure that out I'd love to get the changes into the IDE so it does remember, and maybe get the relevant stuff added to UART.js |
Beta Was this translation helpful? Give feedback.
-
Posted at 2021-04-30 by user128009 Using an iframe could work for dev page reload, I think it may be possible to even achieve reconnect across a tab/window reload (getDevices() will return an already paired device), but I will keep this workaround in mind, thanks! |
Beta Was this translation helpful? Give feedback.
-
Posted at 2021-04-30 by user128009 Thanks for the link. https://googlechrome.github.io/samples/web-bluetooth/watch-advertisements-and-connect-async-await.html also gives me the However, the scanning/request dialog is still shown (and it doesn't disappear when a device is connected). Is there any option to close the scanning/request dialog programatically? It does not support AbortController signals. |
Beta Was this translation helpful? Give feedback.
-
Posted at 2021-04-30 by @fanoush
Oh, it does for me. Clicked 'request device' selected Espruino device. Clicked 'connect to bluetooth devices' and it connected. Then I reloaded the page and clicked connect again and it remembered previous device and connected again. EDIT: and btw about that security effort, here is issue WebBluetoothCG/web-bluetooth#358 where they are pretty anal about showing the dialog with no way to circumvent/customize/prevent it but that is for first connection, here WebBluetoothCG/web-bluetooth#164 it clearly says this should work |
Beta Was this translation helpful? Give feedback.
-
Posted at 2021-04-30 by user128009 @fanoush: Interesting, could you post details on your OS/Bluetooth/Browser/Espruino combo? Definitely doesn't work for me (macOS 10.15/Chrome 90 + flag:#enable-web-bluetooth-new-permissions-backend/Puck.js v2.09)
It seems like for some reason your Espruino is still sending advertisement packets even after it is connected? Do you have any Bluetooth device scanning tool running in the background/in parallel? |
Beta Was this translation helpful? Give feedback.
-
Posted at 2021-04-30 by @fanoush it is recent Chrome (89) on windows 10, device is nrf52840 dongle with SDK15 based Espruino, however it does not matter, I just tried DK08 watch (sdk11 based firmware) and it worked too. even closed the window, opened new, clicked connect and it even tried both remembered devices and picked up the watch
BTW the dongle is named Magic3 since it is name of some other 52840 hackable watch I was testing FW for. No scanning tool running in the backround AFAICT. EDIT: |
Beta Was this translation helpful? Give feedback.
-
Posted at 2021-04-30 by @fanoush now I closed window, removed and reconnected the dongle (=power cycled it, cannot do it with the watch), reopened https://googlechrome.github.io/samples/web-bluetooth/watch-advertisements-and-connect.html page, clicked request device but cancelled dialog, then clicked connect and it connected to both of them
so now I have opposite problem, don't have easy way to clear the list of remembered devices :-) |
Beta Was this translation helpful? Give feedback.
-
Posted at 2021-04-30 by user128009 I can confirm your experience on Windows 10/CSR8510 (Win10 Bluetooth Driver)/Chrome 90:
Result:
On macOS I don't get a reconnect because no advertisement packets are received unless I explicitly trigger a scan or request a device. |
Beta Was this translation helpful? Give feedback.
-
Posted at 2021-05-01 by user128009 I connected the CSR8510 to my MacBook Pro 16 to check if the difference is caused by Windows or the Bluetooth chip; as a result Bluetooth on my MacBook Pro 16 is now bricked and apparently needs a Logic Board replacement (https://discussions.apple.com/thread/250944058). 🤷 |
Beta Was this translation helpful? Give feedback.
-
Posted at 2021-05-04 by @gfwilliams Wow, you can actually permanently brick on-board Bluetooth just by inserting a USB stick? that's crazy! |
Beta Was this translation helpful? Give feedback.
-
Posted at 2021-05-04 by user128009 It seems like the power requirement for the USB dongle permanently overwrites the (lower) power requirement for the integrated Bluetooth module. As a result the Bluetooth module thinks it is not supplied with sufficient power and shuts down. Apparently there is a workaround with certain old Bluetooth 2.0 dongles with similar power requirements to the internal Bluetooth module, 2 I had lying around, which didn't work - I now bought a third one from eBay that I hope will fix this. Can't use the Puck.js until then and can't afford the time to have my MacBook shipped for repairs. In the thread they are even shipping around old Bluetooth dongles via post to fix this. |
Beta Was this translation helpful? Give feedback.
-
Posted at 2021-05-04 by user128009 @gfwilliams: What is interesting is that the automatic reconnect works on Windows/CSR8510. Can you clarify which Browser/OS/Bluetooth combo you have where it doesn't? Still trying to find out if this is specific to OS or the Bluetooth chip. |
Beta Was this translation helpful? Give feedback.
-
Posted at 2021-05-04 by @gfwilliams I'm using Linux here... Honestly I'd think that changes between OS were most likely due to Chrome's implementation. It's something I imagine we could flag up with them. I don't suppose this is related to whether the 'Experimental Web Platform features' flag is enabled? |
Beta Was this translation helpful? Give feedback.
-
Posted at 2021-05-04 by user128009 I don't think this is related to the flag, as I can't run the examples without it, IIRC Interesting that Linux/macOS behave similarly, how can we check if this is a Chrome implementation issue? https://github.com/GoogleChrome/samples/issues does not appear to be monitoring new issues. |
Beta Was this translation helpful? Give feedback.
-
Posted at 2021-05-05 by @gfwilliams I think issues need to be filed in https://bugs.chromium.org/p/chromium/issues/list - there's a special bug type for Web Bluetooth I believe |
Beta Was this translation helpful? Give feedback.
-
Posted at 2021-05-05 by @fanoush it is discussed here https://bugs.chromium.org/p/chromium/issues/detail?id=577953&q=bluetooth%20permissions%20getDevices&can=2 or there are more issues related also the https://developer.mozilla.org/en-US/docs/Web/API/Web_Bluetooth_API is not very green for |
Beta Was this translation helpful? Give feedback.
-
Posted at 2021-05-05 by user128009 Thanks for the link to the issue tracker. So to summarize, reconnecting to already paired devices should be supported in Chrome when using
However, at least on Linux and macOS, the event |
Beta Was this translation helpful? Give feedback.
-
Posted at 2021-04-29 by user128009
I use https://www.puck-js.com/puck.js and
to connect to Puck.js (v2.09) from my website. While developing, my website will reload upon code changes, which requires me to reselect my already paired Puck.js which is cumbersome.
I noticed that the WebIDE is capable of connecting to an already paired device without showing the navigator.bluetooth dialog, however it relies on EspruinoTools which I couldn't get to integrate nicely with my webpage.
will provide currently paired devices, however forwarding such a device to the
connect()
method replacing the call tonavigator.bluetooth.requestDevice
was not successful, resulting in a "Bluetooth device is out of range error". I suspect this happens because the last advertisement packet is too old, so I waited onprepairedDevice.watchAdvertisements()
to fire first which took care of the "out of range error" but somehow introduced emptytxItem.data
items inpuck.writeProgress(txItem.maxLength - txItem.data.length, txItem.maxLength);
Is there a solution that avoids "re-pairing" my Puck.js on every page load, i.e. by adapting the puck.js file or somehow integrating parts of the WebIDE/EspruinoTools?
Thanks a lot for your help!
Beta Was this translation helpful? Give feedback.
All reactions