Skip to content

fix(ios): reconnect stale BLE peripherals on app foreground - #6674

Merged
mdmohsin7 merged 3 commits into
mainfrom
fix/ios-ble-foreground-reconnect
Apr 16, 2026
Merged

mdmohsin7 merged 3 commits into
mainfrom
fix/ios-ble-foreground-reconnect

Conversation

@mdmohsin7

Copy link
Copy Markdown
Member

Summary

  • Add OmiBleManager.reconnectStalePeripherals() that re-issues connect() on any known peripheral whose state isn't connected/connecting and wasn't manually disconnected.
  • Hook applicationWillEnterForeground in AppDelegate to call it every time the app returns to the foreground.

Why

The iOS auto-reconnect path in didDisconnectPeripheral schedules a single central.connect(peripheral, nil) 200 ms after a drop. That succeeds when the peripheral reappears shortly after. But if CoreBluetooth instead routes the attempt into didFailToConnect (peripheral out of range too long, chipset-level error, etc.), the handler currently just cleans up — no further retry is scheduled, and the peripheral ends up forgotten by the central manager. The app then stays stuck on disconnected until the user force-quits.

This most commonly shows up as: user backgrounds the app, walks out of BLE range (or the device dies/reboots), comes back into range later, opens the app — and has to force-quit to reconnect.

A foreground-resume sweep is a cheap, idempotent safety net: iOS pending connects cost nothing while waiting at the chipset level, so re-issuing them on every foreground recovers from any silently-terminated retry chain.

Test plan

  • Connect Omi, background app, move device out of range for several minutes until connection is lost
  • Bring device back into range, foreground the app — verify it reconnects within a few seconds without needing a force-quit
  • Verify a manually-disconnected peripheral stays disconnected after foregrounding (no auto-reconnect regression)
  • Verify no regressions in the normal in-range foreground/background cycle

🤖 Generated with Claude Code

mdmohsin7 and others added 2 commits April 15, 2026 22:00
Add reconnectStalePeripherals() that re-issues connect() on any known
peripheral not currently connected (and not manually disconnected).
Guards against the case where the auto-reconnect chain in
didDisconnectPeripheral terminates in didFailToConnect while
backgrounded, leaving the peripheral forgotten by the central manager
with no path back to connected without a force-quit.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
Invoke OmiBleManager.reconnectStalePeripherals() whenever the app
returns to the foreground so any peripheral that dropped while
backgrounded gets a fresh connect() attempt instead of staying stuck
until force-quit.
@greptile-apps

greptile-apps Bot commented Apr 15, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR adds a foreground-resume sweep (reconnectStalePeripherals) to recover BLE peripherals that become silently stuck after didFailToConnect terminates the retry chain. The hook in applicationWillEnterForeground and the manuallyDisconnected guard are both correct, but the sweep iterates the full peripherals dict, which also contains scan-discovered-but-never-connected devices — these could be unintentionally connected on every foreground.

Confidence Score: 4/5

Mostly safe; one P1 scope issue (missing everConnected guard) should be fixed before merging to avoid connecting to unintended scan-discovered peripherals.

The core fix is well-reasoned and the AppDelegate hook is correct, but reconnectStalePeripherals operates on all discovered peripherals rather than only previously-connected ones, which is a present correctness issue on the changed code path.

app/ios/Runner/OmiBleManager.swift — reconnectStalePeripherals needs the everConnected guard

Important Files Changed

Filename Overview
app/ios/Runner/OmiBleManager.swift Adds reconnectStalePeripherals() but iterates the full peripherals dict (which includes scan-discovered-but-never-connected devices), missing the everConnected guard that limits reconnect attempts to intentionally-paired peripherals.
app/ios/Runner/AppDelegate.swift Hooks applicationWillEnterForeground to call reconnectStalePeripherals(); placement and super call are correct.

Sequence Diagram

sequenceDiagram
    participant App as iOS App
    participant AD as AppDelegate
    participant BLE as OmiBleManager
    participant CB as CoreBluetooth

    Note over App: User backgrounds app, device goes out of range
    CB->>BLE: didDisconnectPeripheral
    BLE->>CB: connect(peripheral) [after 200ms]
    CB->>BLE: didFailToConnect (device still out of range)
    Note over BLE: peripheral stuck — no further retry scheduled

    Note over App: User brings device back in range, foregrounds app
    App->>AD: applicationWillEnterForeground
    AD->>BLE: reconnectStalePeripherals()
    BLE->>BLE: guard centralManager.state == .poweredOn
    BLE->>BLE: iterate peripherals (skip manuallyDisconnected)
    BLE->>CB: connect(peripheral) [re-issued for disconnected/failed]
    CB->>BLE: didConnect
    BLE->>BLE: discoverServices
    BLE->>App: onDeviceReady (Flutter notified)
Loading

Reviews (1): Last reviewed commit: "fix(ios): wire applicationWillEnterForeg..." | Re-trigger Greptile

Comment thread app/ios/Runner/OmiBleManager.swift Resolved
The peripherals dict also caches scan-discovered devices from
didDiscover, so iterating the full dict on foreground could attempt
connects to unrelated BLE devices picked up during a scan. Limit the
sweep to entries in everConnected, which is only populated in
didConnect.
@mdmohsin7
mdmohsin7 merged commit c04c300 into main Apr 16, 2026
2 checks passed
@mdmohsin7
mdmohsin7 deleted the fix/ios-ble-foreground-reconnect branch April 16, 2026 15:32
Glucksberg pushed a commit to Glucksberg/omi-local that referenced this pull request Apr 28, 2026
…dware#6674)

## Summary

- Add `OmiBleManager.reconnectStalePeripherals()` that re-issues
`connect()` on any known peripheral whose state isn't
`connected`/`connecting` and wasn't manually disconnected.
- Hook `applicationWillEnterForeground` in `AppDelegate` to call it
every time the app returns to the foreground.

## Why

The iOS auto-reconnect path in `didDisconnectPeripheral` schedules a
single `central.connect(peripheral, nil)` 200 ms after a drop. That
succeeds when the peripheral reappears shortly after. But if
CoreBluetooth instead routes the attempt into `didFailToConnect`
(peripheral out of range too long, chipset-level error, etc.), the
handler currently just cleans up — no further retry is scheduled, and
the peripheral ends up forgotten by the central manager. The app then
stays stuck on `disconnected` until the user force-quits.

This most commonly shows up as: user backgrounds the app, walks out of
BLE range (or the device dies/reboots), comes back into range later,
opens the app — and has to force-quit to reconnect.

A foreground-resume sweep is a cheap, idempotent safety net: iOS pending
connects cost nothing while waiting at the chipset level, so re-issuing
them on every foreground recovers from any silently-terminated retry
chain.

## Test plan

- [ ] Connect Omi, background app, move device out of range for several
minutes until connection is lost
- [ ] Bring device back into range, foreground the app — verify it
reconnects within a few seconds without needing a force-quit
- [ ] Verify a manually-disconnected peripheral stays disconnected after
foregrounding (no auto-reconnect regression)
- [ ] Verify no regressions in the normal in-range foreground/background
cycle

🤖 Generated with [Claude Code](https://claude.com/claude-code)
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