Repository navigation
fix(android): re-emit device-ready when Background Mode's BLE service outlives the Flutter engine - #10908
Merged
Conversation
This was referenced Aug 2, 2026
…engine Background Mode keeps OmiBleForegroundService and the GATT link alive after the Flutter engine dies. On the next app start Dart calls manageDevice to re-establish its side, but startService routed that to forceReconnect, which returned silently when the peripheral was already connected (#6987). onDeviceReady is Dart's only "connected" signal, so connect() waited out its 60s timeout and settled on disconnected while the link was up: blue LED, "Connected to Omi" in the service notification, no transcription, recoverable only by a manual disconnect. Route already-running connect requests through onConnectRequest, which adopts the live link and re-emits ready when the peripheral is connected and otherwise forces a fresh attempt as before. manageDevice shares the same adopt helper. Also stop gating the native audio subscription on isFlutterAlive, and clear isFlutterAlive/nativeBleForegroundReady on every activity destroy rather than only a finishing one. A system-initiated destroy left both stale, so native deferred the audio characteristic to an engine that no longer existed and nobody subscribed it. Verification: cd app/android && ./gradlew :app:testDevDebugUnitTest # 15 tests, 0 failures cd app && flutter build apk --flavor dev --debug # built Fixes #10847 Failure-Class: new
…lers
A device-ready for a link that is already up must not swap out stream controllers
that still have subscribers: the capture provider's audio subscription would be
orphaned and BLE packets would land where nobody listens — the same silent audio
stop as the native defect, in a new shape. main already preserves open controllers;
this pins that behaviour, which the native resync in the previous commit makes
routinely reachable.
Adds a hostApi seam to NativeBleTransport so the transport can be driven in a test
without a platform channel.
Verification:
cd app && flutter test test/unit/native_ble_transport_resync_test.dart
# 2 passed; the first fails (Expected: [[1, 2, 3]] Actual: []) when the
# controller-preservation branch is removed
The class this PR's fix instantiates: an establish request whose target is already in the desired state must still emit the observation its client depends on. Its guard surface is the BleConnectRouter policy plus the contract test that pins the already-connected branch to a ready re-emit.
mdmohsin7
force-pushed
the
fix/android-bg-mode-ble-resync
branch
from
August 11, 2026 11:51
099d13e to
42f2797
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #10847.
Symptom
On Android with Background Mode on, the app shows the device as disconnected while the
LED is blue, the foreground-service notification says "Connected to Omi", and the
diagnostics page still counts connection uptime. Transcription stops silently. The only
recovery is a manual disconnect + reconnect.
Root cause
Background Mode's whole point is that
OmiBleForegroundService— and the GATT link itowns — outlive the Flutter engine. When the user reopens the app, a fresh engine and a
fresh Dart isolate start at
isConnected = falseand callmanageDeviceto re-establishtheir side of a link that is already up.
startService()routes that call toforceReconnect()when the service instance alreadyexists, and
forceReconnect()returned immediately if the peripheral was connected. The"already connected → re-emit ready" resync (
notifyReadyForConnectedGatt) lives only inmanageDevice(), which that branch bypasses.onDeviceReadyis the only signal Dart hasfor "connected", so
NativeBleTransport.connect()waited out its 60s timeout and settledon
disconnected— permanently, since nothing reconciles Dart state against native.This is a regression from #6987, which replaced
inst.manageDevice(...)withinst.forceReconnect(...)to break stuckautoConnectpassive scans. The already-connectedcase was never the stuck case, and it lost its resync in the swap.
Two adjacent defects turn "wrong badge" into "silent loss of transcription":
ensureBackgroundAudioSubscription()returned early wheneverisFlutterAlivewas true,deferring the audio CCCD subscribe to Dart. With Dart stuck disconnected, any subsequent
BLE drop/reconnect left the audio characteristic subscribed by nobody — link up, zero
packets.
isFlutterAlivewas cleared only when the activity was finishing. A system-initiateddestroy (memory pressure while the foreground service pins the process) left it
truepointing at a dead binary messenger, alongside a stale
nativeBleForegroundReadythatmakes
OmiBackgroundAudioStreamerbail withforeground_ready.Change
startService→onConnectRequest, which adopts the live link and re-emits ready whenthe peripheral is connected, and otherwise forces a fresh attempt exactly as before.
manageDeviceshares the sameadoptConnectedDevicehelper. The decision is a purepolicy function (
BleConnectRouter.kt) so the contract is testable off-device.adoptConnectedDevicedeliberately does not touch the managed-device oruser-disconnected prefs, so a CompanionDeviceManager appear event can never resurrect a
device the user explicitly disconnected.
MainActivity.onDestroyclearsisFlutterAliveandnativeBleForegroundReadywhetheror not the activity is finishing.
configureFlutterEnginere-arms both on attach.still have subscribers. The native resync makes a repeated
onDeviceReadyroutinelyreachable, and replacing live controllers orphans every subscriber — the same silent
audio stop in a new shape.
mainlanded the controller-preservation branch itself whilethis PR was open (b0e2b2b, d5b35a4), so this commit is now the test plus a
hostApiseam that lets the transport be driven without a platform channel.Verification
The new Dart test is a real guard: removing the controller-preservation branch makes it
fail with
Expected: [[1, 2, 3]] Actual: [].The single
./test.shfailure isconversation_detail_provider_selection_test.dart: conversation getter resolves when startedAt and createdAt fall on different days, a date-boundary test unrelated to thischange. Confirmed pre-existing: it fails identically on
origin/mainwith these commitsstashed.
Not verified by me: the on-device repro (Background Mode on, close the app, reopen, confirm
the app adopts the live link and resumes transcription) needs the pendant.
BleConnectRouterTestis a contract test on new code rather than a regression test, and Android unit tests are not
currently run by any CI workflow — a pre-existing gap, worth wiring into
mobile-app-checks.ymlin its own change.Failure-Class: new
Rebased on
main, and re-verified there:./gradlew :app:testDevDebugUnitTest,flutter test test/unit/native_ble_transport_resync_test.dart,flutter analyze, andflutter build apk --flavor dev --debugall pass on the rebased branch.The branch originally carried a fourth commit fixing
${resolved,,}(a bash 4+ expansion)in
scripts/dev-harness/_resolve_python.sh, which crashedpre-push-singleflighton macOSbash 3.2.
mainfixed the same defect in 5c8f8f6 / cd997c0, so that commit is dropped.🤖 Generated with Claude Code