Repository navigation
New sensor pairing on the watch: the pairing request waits until Loop is on screen - #139
Draft
jeremybarnum wants to merge 12 commits into
Draft
jeremybarnum wants to merge 12 commits into
jeremybarnum wants to merge 12 commits into
Conversation
A pairing sent each 20-byte chunk of the certificates and then slept 40 ms. On a watch with the app in the background the sleep stretched to about 130 ms, and a pairing took 11 s from link-up to the keep-alive, spending part of the system's allowance for a background app's Bluetooth use. The Dexcom app sends the same chunks about 1 ms apart; this sends them back to back the same way. Tested against real sensors: on a watch in the background, link-up to keep-alive in 2.9 s; on a phone, the certificates in under a second. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A G7 asks each new phone and watch for its own Bluetooth bond. watchOS shows the pairing request
only with Loop on screen during a sensor connection; in the background the request goes
unanswered, the sensor closes the link, and the watch never gets readings. On real sensors:
6 of 6 requests shown with Loop on screen, 0 of 24 from the background.
On a watch with Loop not on screen, when the sensor says this device has no bond, the handshake
stops before the bond request and asks the sensor to keep the link (about 250 s). The request
goes out when Loop comes on screen, and the system's Pair alert follows. After three held
connections with no answer, the watch stops pairing from the background until Loop is on
screen. A sensor that says the bond is intact (a new key on a watch it already paired with) is
never held.
The watch always reads the sensor directly, as a display of its own with its own key and
Bluetooth bond, whether the phone runs alongside the Dexcom app or connects directly. Alongside
the Dexcom app the phone has no key to pass on, so the watch needs the pairing code entered in
Loop's G7 settings on the phone. A phone never holds: it pairs on Loop's setup screen, with Loop
in front.
How Loop takes part: while the link is held, the manager reports `directReadNeed` as
`pairingAcceptance`. Loop's watch app, seeing that with Loop not on screen, posts a notification
("Sensor ready to pair"). Tapping it brings Loop to the front; the kit sees the app become
active, sends the held request, and the system's Pair alert appears. Loop calls nothing in the
kit to release the hold. Without such a notification, the request still goes out whenever the
person happens to open Loop while the link is held.
The manager records whether the bond is pending, and keeps that across restarts. It reports
`directReadNeed` from that, from the code and from the link, so the watch can show what to do.
It also answers `readIsFromThisSensor` from the sensor name in a reading's sync identifier.
Tested on a real G7 with a watch: held, notice tapped, then paired; three unanswered holds, then
no background pairing until Loop was on screen; a reinstall with the bond intact paired with no
hold and no alert. G7SensorKit tests pass (217).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This was referenced Nov 27, 2026
While the Dexcom app is connected, it reads a data log of its own from the sensor over the backfill characteristic about every two hours: some 900 frames of up to 20 bytes that are not glucose records. Each frame wrote its bytes and an error to the device log, about 12,600 lines a night. Such frames are now counted and logged as one line when the backfill buffer is flushed. Record frames are logged as before. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… are not glucose records Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
jeremybarnum
pushed a commit
to jeremybarnum/G7SensorKit
that referenced
this pull request
Nov 28, 2026
…cords) into the testers' integration branch Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
shortStatusNote says what the watch needs. "Sensor ready to pair" is an alert, raised when the sensor asks for the watch's bond and withdrawn once paired or when the link closes. A manager passed its sensor by another controller raises no other alert. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The note is read in the app, where the pairing request appears by itself. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- When the sensor is forgotten in the watch's Bluetooth settings, the system cancels the connection waiting for it. The manager took that for any other close, and canReadSensor stayed true until the next connection. - It now counts the bond as missing until the sensor says otherwise, and keeps that across restarts. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A G7 manager that was handed its sensor by another device, as the watch is by the phone, now reports `hasValidSensorSession` as false until it has its own key and Bluetooth pairing. A manager set up on the phone answers as before. This replaces `canReadSensor`, so LoopKit needs no change. The watch's status note also says when the sensor has expired or failed. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
- The hold was chosen at build time: the watch held the pairing request until the app came on screen, and the phone's presence always said yes. It is now decided at run time: a manager passed its sensor by another controller holds it while the app is not on screen; one set up here never does. Behaviour is unchanged on both today. - The platform check is left only where it bridges the app's state (WatchKit on the watch, UIKit's notifications on the phone). canShowPairingRequest is renamed isOnScreen, for what it reports. - Test: only a passed manager holds, and only off screen. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- Its only reader was the watch's status note, which now comes from the passed sensor's need. The sensor keeps the value for its status changes. searchingNote, read only inside the kit, is internal. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.
One of three PRs for pairing a new sensor on the watch. Merge order: #137 (certificates go out without a pause) and this one, then LoopKit/Loop#2707 (the watch shows its CGM's alerts). This branch is built on #137's, so its diff includes that commit until #137 merges.
What this changes
On a watch with Loop not on screen, when the sensor says this device has no bond, the handshake stops before the bond request and asks the sensor to keep the link (about 250 s: 250.0 s after the keep-alive, in one capture of Dexcom's own app). The request goes out when Loop comes on screen, and the system's Pair alert follows. After three held connections with no answer, the watch stops pairing from the background until Loop is on screen. A sensor that says the bond is intact (a new key on a watch it already paired with) is never held.
Only a manager passed its sensor by another controller holds, decided at run time; one set up on the phone pairs on Loop's setup screen and never holds.
The manager records whether the bond is pending, and keeps that across restarts. A manager that was handed its sensor by another device, as the watch is by the phone, reports
hasValidSensorSessionas false until it has its own key and Bluetooth pairing, andshortStatusNotesays what is missing. The note also says when the sensor has expired or failed. A manager set up on the phone answers as before. LoopKit needs no change.When the sensor is forgotten in the watch's Bluetooth settings, watchOS cancels the app's waiting connection. The manager takes that cancel as the bond possibly gone and counts it as pending until the sensor confirms it, so Start is refused at once instead of at the next connection.
At the bond step the manager issues an alert ("Sensor ready to pair"), withdrawn once paired or when the link closes. It is issued at every bond step; Loop's watch app shows it as a notification only when Loop is not on screen (LoopKit/Loop#2707). The sensor's other alerts stay the phone's.
Why
A G7 asks each new phone and watch for its own Bluetooth bond. watchOS shows the pairing request only with Loop on screen during a sensor connection; in the background the request goes unanswered, the sensor closes the link, and the watch never gets readings. On real sensors: 6 of 6 requests shown with Loop on screen, 0 of 24 from the background.
The watch always reads the sensor directly, as a display of its own with its own key and Bluetooth bond, whether the phone runs alongside the Dexcom app or connects directly. Alongside the Dexcom app the phone has no key to pass on, so the watch needs the pairing code entered in Loop's G7 settings on the phone.
How Loop takes part: opening Loop, from the notification or by hand, brings it to the front; the kit sees the app become active, sends the held request, and the system's Pair alert appears. Loop calls nothing in the kit to release the hold.
Worth scrutiny
Tested
hasValidSensorSessioncarrying the answer: sensor forgotten, Start held, paired again from the notification, Start offered again.🤖 Generated with Claude Code