Skip to content

fix(tandem): stabilize the Mobi BLE link across reconnects and busy operations - #153

Merged
jwoglom merged 8 commits into
jwoglom:devfrom
haarisedhi102:tandem-fix-v190
Sep 25, 2026
Merged

jwoglom merged 8 commits into
jwoglom:devfrom
haarisedhi102:tandem-fix-v190

Conversation

@haarisedhi102

@haarisedhi102 haarisedhi102 commented Sep 11, 2026 •

Copy link
Copy Markdown
Contributor

What this fixes

This is the driver-layer half of the Tandem Mobi disconnection work; the app-side
half is andyrozman/AndroidAPS#220.

The Mobi renegotiates the BLE link down to its power-saving profile (interval
30 ms, latency 29, timeout 2 s) about 15 s after connect. The 2 s supervision
timeout causes frequent CONNECTION_TIMEOUT drops. This PR keeps the link on the
HIGH profile, and stops that maintenance from interfering with operations the
pump rejects parameter updates during.

After these changes I have had no disconnections for more than a week.

Changes (all in TandemBluetoothHandler)

Connection priority

  • Re-assert CONNECTION_PRIORITY_HIGH when onConnectionUpdated reports a
    degraded profile (checked on interval/latency/timeout, not latency alone).
  • Add a periodic re-assert (~10 s) as a safety net for degradations the event
    hook misses. Started when authentication completes, stopped on disconnect.

Don't re-assert while the pump is busy — the pump rejects the parameter
update with INSUFFICIENT_AUTHORIZATION mid-operation:

  • Bolus in progress, tracked from BolusPermissionResponse /
    CurrentBolusStatusResponse (DELIVERING/REQUESTING refresh it), cleared on
    BOLUS_PERMISSION_REVOKED and after a 10-minute stale expiry so a missed
    terminal response cannot pin the link to the power-saving profile.
  • Fill cannula / fill tubing / change cartridge, tracked from
    FillCannulaStateStreamResponse and LoadStatusResponse, with a 10-minute
    stale expiry.
  • History-log stream, tracked from HistoryLogResponse / HistoryLogStreamResponse,
    with a 30-second stale expiry (a re-assert mid-download made the pump drop the
    link and wedged the host's history retriever).

Reconnect and teardown hygiene

  • resetInstance() now tears the outgoing handler down (stop()) before dropping
    it; nulling the field alone left abandoned instances' pending autoConnects and
    periodic work running, producing duplicate connections and multiplied logs.
  • getInstance() rebinds the live host so callbacks reach it.
  • Race the background autoConnect (can take minutes) with a low-latency
    address-filtered scan (discovers in seconds); onDiscoveredPeripheral then
    connects directly.
  • Retry immediateConnectToPeripheral 5 s after a failed connection; blessed's
    single internal retry could leave no pending connection.
  • stopScan() on connect and on disconnect to clear a lingering scanner.
  • Ignore onConnectionUpdated failures while not connected (expected noise during
    teardown, not an emergency).

Robustness

  • Pre-flight the notification setup: skip setNotify for characteristics the
    firmware does not expose (e.g. older t:slim X2) instead of waiting forever on a
    callback that will never come.
  • Plant the Timber tree idempotently (uproot first) — resetInstance()/recreate
    cycles were multiplying every log line.
  • Clear the in-progress bolus when the pump revokes bolus permission.

Testing

Used with AAPS on a Tandem Mobi over ~2 weeks of logs, exercising pairing,
reconnect, bolus, fill and history-download flows. No disconnections observed
after the changes.

…iates down

The Mobi renegotiates the BLE connection down to its power-saving profile
(interval=30ms, latency=29, timeout=2s) ~15s after connect. The 2s supervision
timeout causes frequent CONNECTION_TIMEOUT drops.

Re-assert CONNECTION_PRIORITY_HIGH when onConnectionUpdated reports a degraded
profile (latency > 0), but skip while a bolus is in flight (the pump rejects the
connection-parameter update with INSUFFICIENT_AUTHORIZATION mid-bolus). The
in-progress bolus is tracked via PumpStateSupplier.inProgressBolusId.
@haarisedhi102

Copy link
Copy Markdown
Contributor Author

@haarisedhi102 haarisedhi102 changed the title Tandem fix v190 persistent high gates an states Sep 16, 2026
@haarisedhi102 haarisedhi102 changed the title persistent high gates an states persistent high, gates and states Sep 16, 2026
@haarisedhi102 haarisedhi102 changed the title persistent high, gates and states fix(tandem): re-assert HIGH BLE priority without disrupting pump operations Sep 23, 2026
@haarisedhi102 haarisedhi102 changed the title fix(tandem): re-assert HIGH BLE priority without disrupting pump operations fix(tandem): stabilize the Mobi BLE link across reconnects and busy operations Sep 23, 2026
@jwoglom

jwoglom commented Sep 25, 2026

Copy link
Copy Markdown
Owner

Going to merge this but make it configurable behind a flag

@jwoglom
jwoglom merged commit b378e33 into jwoglom:dev Sep 25, 2026
1 check passed
jwoglom pushed a commit that referenced this pull request Sep 25, 2026
All behavior changes from #153 are now disabled by default and enabled via
TandemPump.enablePeriodicConnectionPriorityReassert(). This includes the
periodic and onConnectionUpdated HIGH priority re-asserts, the busy-state
tracking (including PumpStateSupplier.inProgressBolusId updates), and the
related connection lifecycle changes (address-filtered scan race, scan
stops on connect/disconnect, connection-failure retry, ignoring failed
connection updates while disconnected, getInstance rebind, resetInstance
teardown, and Timber tree uproot).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01K4GJkadU6xRduN5FPLmcYT
jwoglom pushed a commit that referenced this pull request Sep 25, 2026
Only the connection-priority re-assert behavior (periodic timer,
onConnectionUpdated re-assert, and the busy-state tracking that feeds it)
stays behind enablePeriodicConnectionPriorityReassert(). The scan race,
scan stops, connection-failure retry, ignoring failed connection updates
while disconnected, getInstance rebind, resetInstance teardown, and Timber
uproot are unconditional again.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01K4GJkadU6xRduN5FPLmcYT
jwoglom added a commit that referenced this pull request Sep 25, 2026
fix(tandem): stabilize the Mobi BLE link across reconnects and busy operations
jwoglom pushed a commit that referenced this pull request Sep 25, 2026
All behavior changes from #153 are now disabled by default and enabled via
TandemPump.enablePeriodicConnectionPriorityReassert(). This includes the
periodic and onConnectionUpdated HIGH priority re-asserts, the busy-state
tracking (including PumpStateSupplier.inProgressBolusId updates), and the
related connection lifecycle changes (address-filtered scan race, scan
stops on connect/disconnect, connection-failure retry, ignoring failed
connection updates while disconnected, getInstance rebind, resetInstance
teardown, and Timber tree uproot).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01K4GJkadU6xRduN5FPLmcYT
jwoglom pushed a commit that referenced this pull request Sep 25, 2026
Only the connection-priority re-assert behavior (periodic timer,
onConnectionUpdated re-assert, and the busy-state tracking that feeds it)
stays behind enablePeriodicConnectionPriorityReassert(). The scan race,
scan stops, connection-failure retry, ignoring failed connection updates
while disconnected, getInstance rebind, resetInstance teardown, and Timber
uproot are unconditional again.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01K4GJkadU6xRduN5FPLmcYT
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.

2 participants