Repository navigation
fix(ble): end the link when the Mac's Bluetooth powers off or resets - #2787
matthiasthomas wants to merge 2 commits into
Conversation
CoreBluetooth delivers no didDisconnectPeripheral when the radio powers off or resets, which on a Mac happens on every sleep and on Bluetooth toggles. Nothing tore the link down, so every per-link flag survived into the next link. On poweredOn the app re-attached to the strap macOS still held with didBond and connectHandshakeDone still true, so the connect handshake never ran again: no Set Clock, no GET_DATA_RANGE, no SEND_HISTORICAL_DATA, no battery read, no alarm re-arm. A backfill that was mid-drain stayed backfilling with a frozen frontier, so the UI read "Syncing" indefinitely and the battery shown was the last reading from before the sleep. centralManagerDidUpdateState now runs the ordinary didDisconnectPeripheral teardown for .poweredOff and .resetting when a link was held, marked intentional so it does not feed the bond-loop / drop detectors and does not schedule a reconnect into a dead radio. poweredOn's connectFromSystem -> connectCore clears the flag and connects afresh. This is the Apple twin of Android's onBluetoothRadioOff -> teardownAfterGattFailure (ryanbr#314). Refs ryanbr#2716 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Traced the mechanism end to end and it holds up. Notes below, one of them a real hole. What checks out
The hole The comment says poweredOn's So on an iOS session launched through state restoration, where macOS, the stated target, is unaffected: there is no CoreBluetooth state restoration there, so poweredOn always takes the One line in Minor
|
…ect too
Review follow-up. poweredOn has two connect exits: connectFromSystem ->
connectCore, which clears intentionalDisconnect, and connectRestored, which
did not. On a state-restored iOS session a radio-off teardown therefore left
the flag set for the whole next link, and the next genuine out-of-range drop
took the "Disconnected (intentional)" branch: no reconnect until Connect was
tapped. macOS has no state restoration and was unaffected.
- connectRestored now clears the flag like connectCore.
- Every writer of the flag goes through BLEManager.LinkIntent (connect,
restoredConnect, userDisconnect, forget, radioOff), so the lifecycle is one
table and a future connect entry cannot forget it. LinkIntentTests pins it,
including the radio-off-then-reconnect sequence through either entry.
- .resetting now logs a line ("Bluetooth is restarting …"), so a bluetoothd
restart that ends a link says why. No banner: it resolves on the next
poweredOn.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Thanks for tracing it end to end, and sorry for the slow turnaround. All three points are addressed in 761278c: The hole.
Flag lifecycle coverage.
Verification:
|
… the Mac's Bluetooth powers off or resets ryanbr#2787 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…p from stalling a sync while the strap is connected ryanbr#2919 Conflict: both sides were additive; kept ryanbr#2912's battery rated-life init call and ryanbr#2787's LinkIntent table alongside this PR's App Nap exemption. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
What this PR does
CoreBluetooth delivers no
didDisconnectPeripheralwhen the radio powers off or resets. On a Mac that happens every time it sleeps, and on Bluetooth toggles. Nothing tore the link down, so every per-link flag survived into the next link. OnpoweredOnthe app re-attached to the strap macOS still held, withdidBondandconnectHandshakeDonestill true, so the connect handshake never ran again: no Set Clock, noGET_DATA_RANGE, noSEND_HISTORICAL_DATA, no battery read, no alarm re-arm.What a user sees after a night with the Mac asleep:
backfillingwith a frozen frontier, so the Devices card reads "Syncing…" indefinitely.From the strap log, Mac asleep 21:58 → awake 09:18:
Fix:
centralManagerDidUpdateStatenow runs the ordinarydidDisconnectPeripheralteardown for.poweredOffand.resettingwhenever a link was held. It is marked intentional, so:poweredOn'sconnectFromSystem→connectCoreclears the flag and connects afresh. This is the Apple twin of Android'sonBluetoothRadioOff→teardownAfterGattFailure(#314). The state rule is a pureBLEManager.radioStateEndsLink(_:).Relation to #2716: #2716 re-proves
encryptedBondwhendidConnectre-enters a link that was already bonded. After this PR the radio-off path no longer re-enters with stale state. #2716 still covers the other trigger it documents: the delayed standing-connect retry firing on a live link. The two are independent and can merge in either order.Type of change
How it was tested
Hardware: WHOOP 4.0, firmware 41.17.6.0, macOS 26.4.1. I ran this build overnight on a Mac that sleeps and wakes. Over that run (23:57 → 09:55):
Disconnected (intentional)BONDED (confirmed write acknowledged)on the next poweredOnBONDED again(the fix(ble): stop a reconnected WHOOP 4.0 reading not fully paired #2716 re-entry path)auto-reconnect pausedBond-loop (#617)Each wake ran the full handshake, for example:
The battery and the sync resumed after wake.
Unit tests:
RadioOffTeardownTestspins which states end the link:.poweredOffand.resettingdo;.poweredOn,.unauthorized,.unknownand.unsupporteddon't.Full
StrandTests(macOS): 2,297 tests ran on this branch. Two failures are timezone-dependent and unrelated to this change:SkinTempAbsoluteDisplayTestsandSleepCarriedStampTestsfail in America/New_York and pass withTEST_RUNNER_TZ=UTC.Build and checks:
xcodegen generateplusxcodebuild(macOSStrandscheme) builds.Tools/doc_comment_lint.pyandTools/i18n_audit.py --ci upstream/mainpass.Not verified locally: the iOS compile, because my Xcode lacks the watchOS platform
NOOPiOSembeds. iOS reaches the samecentralManagerDidUpdateStatepath when Bluetooth is toggled. I have only exercised the Mac.Checklist
swift testinPackages/<name>): n/a, no package touchedandroid/: n/a. Android already tears down on radio-off (Fix/297 #314)StrandDesigntokens: n/a, no UI changedocs/CONTRIBUTING.mdStrand.xcodeproj/) or any secrets/keystoresRelated issues
Refs #2716