Repository navigation
fix: BLE reliability — reconnect races, CompanionService crash, characteristic guards - #6086
Conversation
CompanionDeviceService can be started by the OS even when the app is fully dead. Accessing OmiBleManager.instance before initialization caused a fatal IllegalStateException. Add isInitialized check — if the app hasn't started, there's no Flutter engine to handle connections, so skip reconnect.
Check discovered services before calling native read/write/subscribe. Prevents noisy exceptions when a characteristic doesn't exist on a device variant (e.g. image capture on Omi vs Omi Glass).
Greptile SummaryThis PR addresses two BLE reliability problems on Android: a fatal Key changes:
Issues found:
Confidence Score: 4/5Safe to merge with minor follow-up: two resource-management edge cases should be addressed soon but don't block normal connection flows. All three P2 findings are non-blocking in normal operation. The transport-leak occurs only on the DFU path after manual disconnect, the reconnect-guard issue is limited to the single-device architecture, and the read/write inconsistency is cosmetic for current callers. Score 4 rather than 5 because the transport disposal regression is a real resource leak introduced by this PR. app/lib/services/devices.dart (disconnectDevice disposal gap) and app/android/app/src/main/kotlin/com/friend/ios/OmiBleManager.kt (pendingReconnectRunnable address scoping). Important Files Changed
Reviews (2): Last reviewed commit: "fix: _bleDisconnectDevice uses disconnec..." | Re-trigger Greptile |
Prevents Dart from canceling native's autoConnect=true reconnect (e.g. after status=5 bond removal) with a competing autoConnect=false connection that fails.
…vice If a transport exists for the device (even if disconnected), native owns reconnection. Only create a new transport on first connect or device switch. Dart stops racing native's auto-reconnect.
Previously called connection.disconnect() directly, leaving _connection set. This prevented ensureConnection from creating a new transport on manual reconnect.
|
@greptile-apps re-review |
|
test? |
tested locally |
…cteristic guards (BasedHardware#6086) ## Summary - **Stop Dart from racing native auto-reconnect** — `ensureConnection` no longer disposes and recreates the transport for the same device. If a transport exists (even disconnected), native owns reconnection. Only creates a new transport on first connect or device switch. - **Native `connectPeripheral` skips when pending reconnect exists** — prevents a competing `autoConnect=false` from canceling native's `autoConnect=true` recovery (e.g. after status=5 stale bond removal) - **`_bleDisconnectDevice` properly nulls `_connection`** — uses `DeviceService.disconnectDevice()` so manual disconnect → reconnect works correctly - **Prevent BleCompanionService crash when app process is dead** — added `isInitialized` check before accessing `OmiBleManager.instance` - **Guard characteristic operations against missing characteristics** — check discovered services before calling native read/write/subscribe. Prevents noisy exceptions for device variants (e.g. image capture on Omi vs Omi Glass) ## Root cause After a transient BLE disconnect (e.g. status=5 stale bond), native schedules auto-reconnect with `autoConnect=true`. But Dart's `ensureConnection(force=true)` would dispose the transport, create a new one, and fire `connectPeripheral` with `autoConnect=false` — canceling native's patient reconnect with an impatient direct connection that fails. ## Test plan - [x] Fresh connect — works - [x] Manual disconnect → reconnect — works - [x] Device power cycle → auto-reconnect - [x] Kill app completely, have paired device nearby — no crash - [x] Connect Omi device — no `Characteristic not found` exceptions 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Summary
ensureConnectionno longer disposes and recreates the transport for the same device. If a transport exists (even disconnected), native owns reconnection. Only creates a new transport on first connect or device switch.connectPeripheralskips when pending reconnect exists — prevents a competingautoConnect=falsefrom canceling native'sautoConnect=truerecovery (e.g. after status=5 stale bond removal)_bleDisconnectDeviceproperly nulls_connection— usesDeviceService.disconnectDevice()so manual disconnect → reconnect works correctlyisInitializedcheck before accessingOmiBleManager.instanceRoot cause
After a transient BLE disconnect (e.g. status=5 stale bond), native schedules auto-reconnect with
autoConnect=true. But Dart'sensureConnection(force=true)would dispose the transport, create a new one, and fireconnectPeripheralwithautoConnect=false— canceling native's patient reconnect with an impatient direct connection that fails.Test plan
Characteristic not foundexceptions🤖 Generated with Claude Code