Repository navigation
Keep Bluetooth connections alive on their own - #9
Merged
Merged
Conversation
A Bluetooth strap had to be found and connected by hand after every launch, a short drop left it disconnected for good unless "Auto-reconnect" had been switched on, and a strap that stayed connected but sent nothing went unnoticed: the overlay froze on the last value while the status still said "Connected". src/ble-manager.js now sits above the noble (macOS) and WinRT (Windows) drivers and decides how a connection survives, the same way on both: - the last strap is remembered and connected again at launch; one that was disconnected by hand stays disconnected - auto-reconnect is on unless switched off, waiting 3, 6, 12, 24, then 30s between attempts; a drop goes straight to "reconnecting" - a connection that delivers no measurement within 10s is rebuilt, and so is one that goes silent for 30s; 15s without a heart rate shows "No heart rate", or "No skin contact" when the strap keeps sending 0 BPM; the overlay shows "--" instead of a frozen number - sleep, screen unlock and Bluetooth coming back on trigger a check - every step goes to ble.log, and "Copy diagnostics" in the Bluetooth card puts a summary on the clipboard Driver fixes: on macOS the scan that looks for a remembered strap left a timer behind that stopped whatever scan was running ten seconds later, a stateChange listener was added on every scan request, and power changes were not reported. The noble driver now reports every measurement with its skin contact flag. On Windows the helper restarted after every failure from the third on and showed "Bluetooth unavailable" each time; it now restarts once every three failures, quietly. Linux still connects through Web Bluetooth in the settings window and is unchanged. Verified with a Polar H10 on macOS: automatic connect at launch, recovery after leaving and re-entering range, "No skin contact" with the strap taken off and the heart rate returning on its own, and the link surviving sleep and unlock. 144 of 144 tests pass, including 32 new ones against a simulated strap and a stand-in for noble. Windows is not tested on hardware.
The wait between reconnect attempts grew to 30 seconds. In a test with a Polar H10 carried out of range and back, the strap was in reach again well before the next attempt came round. The steps are now 3, 6, 12 and 15 seconds, and 15 repeats, so a returning strap reconnects within about 15 seconds. 144 of 144 tests pass.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
Bluetooth was the shakiest part of the app:
What changes
src/ble-manager.jssits above the noble (macOS) and WinRT (Windows) drivers and decides how a connection survives, the same way on both:--instead of a frozen numberble.log; "Copy diagnostics" in the Bluetooth card puts a summary on the clipboardDriver fixes:
src/ble.js): the scan that looks for a remembered strap left a timer that stopped whatever scan was running ten seconds later; astateChangelistener was added on every scan request; power changes were not reported; "Device not found" was logged when Bluetooth simply was not ready yet. Every measurement is now reported with its skin contact flag.src/ble-win.js): the helper restarted after every failure from the third on and showed "Bluetooth unavailable" each time; it now restarts once every three failures, quietly.Linux still connects through Web Bluetooth in the settings window and is unchanged.
Testing
npm test: 144 of 144 pass, 32 of them new (test/ble-manager.test.jsagainst a simulated strap and clock,test/ble-noble.test.jsagainst a stand-in for noble). The noble test fails on the oldble.jsat the stopped-scan bug.