Skip to content

New sensor pairing on the watch: the pairing request waits until Loop is on screen - #139

Draft
jeremybarnum wants to merge 12 commits into
LoopKit:next-devfrom
jeremybarnum:sportmode/watch-sensor-pairing
Draft

jeremybarnum wants to merge 12 commits into
LoopKit:next-devfrom
jeremybarnum:sportmode/watch-sensor-pairing

Conversation

@jeremybarnum

@jeremybarnum jeremybarnum commented Nov 27, 2026 •

Copy link
Copy Markdown

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 hasValidSensorSession as false until it has its own key and Bluetooth pairing, and shortStatusNote says 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

  • The phone's handshake changes too: the wait for the bond's confirmation goes from 20 s to 40 s, and no confirmation now counts as unconfirmed.

Tested

  • Real G7 and a watch, builds 1358–1361: held, notification opened, then paired (four times, about 4 s from opening to bonded); Loop already on screen: Pair at once; code rejected: the note asked for the code again; sensor forgotten with Loop in the background: Start refused at once, and the next connection asked for the pairing again; three unanswered holds, then no background pairing until Loop was on screen.
  • Not tested on a device: a forget while Loop is not running, or in the ~35 s after a reading when no connection is waiting; there the forget is found at the next connection, as before.
  • Build 1362, with hasValidSensorSession carrying the answer: sensor forgotten, Start held, paired again from the notification, Start offered again.
  • G7SensorKit tests pass (226).

🤖 Generated with Claude Code

Jeremy Barnum and others added 2 commits November 26, 2026 09:15
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>
Jeremy Barnum and others added 2 commits November 27, 2026 14:40
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>
Jeremy Barnum and others added 5 commits December 1, 2026 12:31
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>
Jeremy Barnum and others added 3 commits December 1, 2026 23:29
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>
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