Skip to content

Add opt-in Background Mode for Android BLE recording - #7577

Merged
mdmohsin7 merged 17 commits into
mainfrom
android-background-mode
Jun 2, 2026
Merged

mdmohsin7 merged 17 commits into
mainfrom
android-background-mode

Conversation

@mdmohsin7

@mdmohsin7 mdmohsin7 commented Jun 1, 2026 •

Copy link
Copy Markdown
Member

closes #5898

Summary

Two related Android BLE changes:

  1. Background Mode — an opt-in setting (default off) that keeps the Omi connected and transcribing when the app is fully closed.
  2. BLE bonding removal — stop requesting a bond the firmware no longer supports, which was causing a reconnect loop.

Background Mode

A new Beta toggle in Settings → Profile → Voice & People (always accessible; Android-only). Tapping it opens a sheet with the toggle, a short description, and a note that it currently works with Omi devices only and is improving.

App state Off (default) On
Foreground / backgrounded (app alive) Records normally Records normally
App fully closed (swiped / killed) Foreground service stops, device disconnects Service stays alive, keeps streaming audio to transcription
Device off / out of range (app closed) — Low-power parked reconnect; auto-resumes streaming when the device returns
  • "Off" restores the pre-existing behavior (service torn down on app close, non-sticky).
  • "On" keeps the service alive (sticky + survives task removal); the native streamer pushes BLE audio to the transcription WebSocket while Flutter is dead.
  • A genuinely off / out-of-range device is handled by a parked autoConnect — low power, and it auto-reconnects when the device returns (no reliance on OS presence events, which fire spuriously for a connected peripheral that has stopped advertising).
  • Gated end-to-end on a backgroundModeEnabled pref read natively; no Pigeon changes.

BLE bonding removal

The Omi firmware no longer uses bonding (no CONFIG_BT_SMP), but the app still requested a bond for Omi devices on connect. Against the non-bonding firmware the bond request failed → status=5 (authentication failure) → a connect/disconnect loop, so the device never stayed connected and wouldn't reconnect when the app was reopened.

Fix: only Limitless devices request bonding now (device_connection.dart).

Notes

  • Android only — iOS background BLE (CoreBluetooth) is a separate mechanism, out of scope here.
  • l10n strings added and translated across all locales.
  • Verified on device: the connection holds, and background streaming resumes after the device is powered back on.

@mdmohsin7
mdmohsin7 marked this pull request as ready for review June 2, 2026 14:28
@mdmohsin7

Copy link
Copy Markdown
Member Author

@greptile-apps review

@kodjima33 kodjima33 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Android opt-in Background Mode for BLE recording (sticky foreground service + parked autoConnect) and removal of bonding requests against the new non-bonding Omi firmware (was causing reconnect loops). Approve only — mobile area.

@greptile-apps

greptile-apps Bot commented Jun 2, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR adds an opt-in Background Mode feature for Android BLE recording that keeps the Omi device connected and transcribing when the app is fully closed, and removes the BLE bonding request for Omi devices that was causing an auth-failure reconnect loop.

  • Background Mode (opt-in, default off): A new toggle in Settings → Profile → Voice & People (Android-only) controls whether OmiBleForegroundService runs as START_STICKY and survives task removal. With mode off, the service stops on close (restoring pre-existing behavior); with it on, it stays alive using a native OmiBackgroundAudioStreamer WebSocket that streams BLE audio while Flutter is dead. The preference is gated by a shared FlutterSharedPreferences key read natively.
  • BLE bonding removal: DeviceConnectionFactory now only requests a bond for DeviceType.limitless; Omi devices are excluded, fixing the status=5 / connect-disconnect loop caused by bond requests against firmware without CONFIG_BT_SMP.

Confidence Score: 4/5

Safe to merge with the two minor issues addressed; neither blocks foreground operation or causes data loss.

The background mode gating logic is well-structured and consistent across MainActivity, OmiBleForegroundService, BleCompanionService, and the Flutter preference layer. The bonding removal is clean. Two issues worth fixing: the settings search drawer exposes the Background Mode entry on iOS where it has no effect, and the START_STICKY decision in onStartCommand is baked in at service-start time, so toggling background mode ON after the service is already running as non-sticky leaves a gap where an OS process-kill won't trigger a service restart.

app/lib/pages/settings/settings_drawer.dart (missing Android guard on search entry) and app/android/app/src/main/kotlin/com/friend/ios/OmiBleForegroundService.kt (sticky-flag lifecycle edge case)

Important Files Changed

Filename Overview
app/android/app/src/main/kotlin/com/friend/ios/OmiBleForegroundService.kt Core background mode logic: adds isBackgroundModeEnabled() helper, switches onStartCommand to START_NOT_STICKY when mode is off, and stops on task removal when mode is off. The sticky-flag decision is captured once at service start, which creates a minor gap if the user enables background mode while the service is already running as non-sticky.
app/android/app/src/main/kotlin/com/friend/ios/BleCompanionService.kt Companion service correctly gates both onCreate and onDeviceAppeared behind the background mode pref; handleDeviceDisappeared made a deliberate no-op with clear comment.
app/android/app/src/main/kotlin/com/example/my_project/MainActivity.kt onDestroy now stops the foreground service when background mode is off, correctly restoring the pre-existing non-persistent behavior.
app/lib/providers/capture_provider.dart _saveNativeBleStreamConfig now sets nativeBleStreamingEnabled to backgroundModeEnabled instead of always true; the native audio streamer is thereby disabled by default, which is intentional and correct.
app/lib/pages/settings/profile.dart Adds the Background Mode bottom sheet and the Android-guarded profile list item. State management correctly re-reads the pref on each rebuild of the StatefulBuilder.
app/lib/pages/settings/settings_drawer.dart Adds Background Mode to the global settings search index without a PlatformService.isAndroid guard, so the entry is visible on iOS where the setting does not exist.
app/lib/services/devices/device_connection.dart Bonding is now limited to DeviceType.limitless only; Omi devices (including OmiGlass) no longer request bonding, fixing the auth-failure reconnect loop.
app/lib/backend/preferences.dart Adds backgroundModeEnabled getter/setter using the standard getBool/saveBool pattern with false as the default, consistent with the Kotlin side's getBoolean("flutter.backgroundModeEnabled", false).

Sequence Diagram

sequenceDiagram
    participant U as User
    participant FL as Flutter (profile.dart)
    participant SP as SharedPreferences
    participant MA as MainActivity (onDestroy)
    participant SVC as OmiBleForegroundService
    participant STR as OmiBackgroundAudioStreamer

    U->>FL: Toggle Background Mode ON
    FL->>SP: "backgroundModeEnabled = true"
    FL->>SP: "nativeBleStreamingEnabled = true"

    U->>U: Close app (task removed)
    MA->>SP: isBackgroundModeEnabled? → true
    MA-->>SVC: (does NOT stopService)
    SVC->>SVC: onTaskRemoved → keep alive

    Note over SVC,STR: Flutter dead, service alive
    SVC->>STR: handleCharacteristic (BLE audio)
    STR->>SP: "loadConfig() → nativeBleStreamingEnabled=true"
    STR->>STR: stream audio to WebSocket

    U->>U: Toggle Background Mode OFF
    FL->>SP: "backgroundModeEnabled = false"
    FL->>SP: "nativeBleStreamingEnabled = false"
    U->>U: Close app
    MA->>SP: isBackgroundModeEnabled? → false
    MA->>SVC: stopService()
    SVC->>STR: stop(service_destroyed)
Loading

Reviews (1): Last reviewed commit: "feat(app): translate Background Mode str..." | Re-trigger Greptile

_SearchableItem(title: context.l10n.speechProfile, icon: profileIcon, onTap: goToProfile),
_SearchableItem(title: context.l10n.identifyingOthers, icon: profileIcon, onTap: goToProfile),
_SearchableItem(title: context.l10n.voiceResponseMode, icon: profileIcon, onTap: goToProfile),
_SearchableItem(title: context.l10n.backgroundModeTitle, icon: profileIcon, onTap: goToProfile),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 The Background Mode search entry is added unconditionally, but the setting itself is guarded by PlatformService.isAndroid in profile.dart. iOS users who search for "Background Mode" will see the result, tap it, navigate to Profile, and find nothing — a confusing dead-end.

Suggested change
_SearchableItem(title: context.l10n.backgroundModeTitle, icon: profileIcon, onTap: goToProfile),
if (PlatformService.isAndroid) _SearchableItem(title: context.l10n.backgroundModeTitle, icon: profileIcon, onTap: goToProfile),

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

Comment on lines 611 to +634
@@ -616,13 +625,22 @@ class OmiBleForegroundService : Service() {
Log.i(TAG, "onStartCommand: no device address or saved device, stopping")
stopSelf()
}
} else {
Log.i(TAG, "onStartCommand: background mode off and no device address, stopping")
stopSelf()
}

return START_STICKY
// Background mode off ⇒ non-sticky, so the OS won't resurrect the service after the app is closed.
return if (backgroundMode) START_STICKY else START_NOT_STICKY

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Sticky-flag not updated when background mode changes at runtime

backgroundMode is read once at the top of onStartCommand and the return value (START_STICKY / START_NOT_STICKY) is fixed for the lifetime of that service invocation. Because OmiBleForegroundService.startService short-circuits when instance != null, onStartCommand is never called again for an already-running service, so the flag never refreshes.

Concrete failure: user opens the app (background mode off → service starts as START_NOT_STICKY), then enables background mode while the app is open. onTaskRemoved will correctly keep the service alive on task removal, but if the OS subsequently kills the process due to memory pressure, it will not restart the service because START_NOT_STICKY was already committed. Background mode is silently broken until the user opens the app again.

@mdmohsin7
mdmohsin7 merged commit 7807d9b into main Jun 2, 2026
1 check passed
@mdmohsin7
mdmohsin7 deleted the android-background-mode branch June 2, 2026 15:06
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.

android works without app in bg

2 participants