Skip to content

Keep Bluetooth connections alive on their own - #9

Merged
alexholzreiter merged 2 commits into
mainfrom
fix/ble-reliability
Nov 23, 2026
Merged

alexholzreiter merged 2 commits into
mainfrom
fix/ble-reliability

Conversation

@alexholzreiter

Copy link
Copy Markdown
Collaborator

Why

Bluetooth was the shakiest part of the app:

  • after every launch the strap had to be found and connected by hand
  • a short drop left it disconnected for good unless "Auto-reconnect" had been switched on (it was off by default)
  • a strap that stayed connected but sent nothing went unnoticed: the overlay froze on the last value while the status still said "Connected"
  • sleep and Bluetooth being switched off and on were not handled

What changes

src/ble-manager.js 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 disconnected by hand stays disconnected
  • auto-reconnect is on unless switched off; waits of 3, 6, 12, then 15 s between attempts; a drop goes straight to "reconnecting"
  • a connection that delivers no measurement within 10 s is rebuilt, and so is one silent for 30 s
  • 15 s 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 is written to ble.log; "Copy diagnostics" in the Bluetooth card puts a summary on the clipboard

Driver fixes:

  • macOS (src/ble.js): the scan that looks for a remembered strap left a timer that stopped whatever scan was running ten seconds later; a stateChange listener 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.
  • Windows (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.js against a simulated strap and clock, test/ble-noble.test.js against a stand-in for noble). The noble test fails on the old ble.js at the stopped-scan bug.
  • By hand on macOS with a Polar H10: 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, the link surviving sleep and unlock.
  • Not tested: Windows on real hardware, Bluetooth switched off and on, "Disconnect" followed by a restart.

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.
@alexholzreiter
alexholzreiter merged commit 3088816 into main Nov 23, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant