Skip to content

android: harden heart rate monitoring - #914

Open
Dardrai wants to merge 1 commit into
librepods-org:android/rewritefrom
Dardrai:port/heart-rate
Open

Dardrai wants to merge 1 commit into
librepods-org:android/rewritefrom
Dardrai:port/heart-rate

Conversation

@Dardrai

@Dardrai Dardrai commented Nov 13, 2026

Copy link
Copy Markdown

Ports the heart rate handling from Ripthulhu/librepods onto android/rewrite. Only the core protocol code changes. The UI is untouched: startHr() / stopHr() keep their signatures.

What changes

  • Service discovery: after init the app sends RequestAllDescriptors and uses the heart rate service number the buds announce. A service counts only if its HID descriptor dictionary matches the known layout exactly (SensorDescriptorLayout). Legacy numbers 19/20/84 are used only until a descriptor arrives.
  • Start: open services 0 and 4 → HRM_STATE (control 0x30) = 1 → SensorDataWX setting (log type 2, report 1, heartRateInterval) to HEARTRATE_COMMAND and HEARTRATE.
  • Stop: interval 0 to both, then HRM_STATE = 2.
  • Watchdog: if no valid sample arrives for 12 s, the stream restarts once, then gives up and reports INACTIVE instead of staying in WAITING forever.
  • Sample validation: 18 bytes, 30–220 bpm, confidence ≥ 0x80, and one of the six status tails seen in captures. This replaces the payload[17] == 0 check.
  • Framing: RTBuddy records are reassembled across socket reads (SensorFrames), and the read buffer goes from 1 KiB to 64 KiB, so descriptor announcements arrive whole.
  • Proto: sensor_descriptor is now repeated, because the buds announce several in one message.
  • Per-connection state: one HeartRateSession per socket, so a stale socket can't write or update state after a reconnect.
  • Motion: the head-tracking path is unchanged. BUDDY_COMMAND records now arrive through the reassembler before handleSensorData().

Tests

Adds JUnit to :core with 9 unit tests: descriptor parsing (including every truncation), sample validation, service remapping and restart, framing across every read split, and the start/stop wire sequence. ./gradlew :core:testDebugUnitTest passes.

Not done yet

  • Not run on hardware on top of rewrite; the logic comes from the fork, where it runs on AirPods Pro 3. I'll mark this ready once it's verified here.
  • :app not built locally; only :core was compiled and tested.

🤖 Generated with Claude Code

Port the heart rate stream handling from Ripthulhu/librepods.

- Ask the buds for their sensor descriptors after init and use the
  announced heart rate service number; fall back to 19/20/84 only until
  a descriptor was seen. A service counts as heart rate only when its HID
  descriptor dictionary matches the known layout exactly (SensorDescriptorLayout).
- Start sequence: open services 0 and 4, HRM_STATE (0x30) on, then the
  SensorDataWX setting (log type 2, 1 s) to HEARTRATE_COMMAND and HEARTRATE.
  Stop sends interval 0 and HRM_STATE off.
- Watchdog: no valid sample for 12 s restarts the stream once, then
  gives up and reports INACTIVE instead of staying in WAITING.
- Samples need 18 bytes, 30-220 bpm, confidence >= 0x80 and one of the
  captured status tails.
- Reassemble RTBuddy records across socket reads and raise the read
  buffer from 1 KiB, so descriptor announcements arrive whole.
- sensor_descriptor is repeated in the proto.
- One HeartRateSession per socket, so a stale connection cannot write.
- Unit tests for parsing, framing and the start/stop wire sequence.
@Dardrai
Dardrai marked this pull request as ready for review November 13, 2026 08:23
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