Repository navigation
fix(ios): reconnect stale BLE peripherals on app foreground - #6674
Conversation
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 SummaryThis PR adds a foreground-resume sweep ( Confidence Score: 4/5Mostly 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
Sequence DiagramsequenceDiagram
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)
Reviews (1): Last reviewed commit: "fix(ios): wire applicationWillEnterForeg..." | Re-trigger Greptile |
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.
…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)
Summary
OmiBleManager.reconnectStalePeripherals()that re-issuesconnect()on any known peripheral whose state isn'tconnected/connectingand wasn't manually disconnected.applicationWillEnterForegroundinAppDelegateto call it every time the app returns to the foreground.Why
The iOS auto-reconnect path in
didDisconnectPeripheralschedules a singlecentral.connect(peripheral, nil)200 ms after a drop. That succeeds when the peripheral reappears shortly after. But if CoreBluetooth instead routes the attempt intodidFailToConnect(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 ondisconnecteduntil 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
🤖 Generated with Claude Code