Skip to content

Revert "fix(ios): stop background audio keep-alive and back off BLE timeouts" - #11567

Merged
mdmohsin7 merged 1 commit into
mainfrom
revert-11547-fix/ios-background-battery-keepalive
Aug 14, 2026
Merged

mdmohsin7 merged 1 commit into
mainfrom
revert-11547-fix/ios-background-battery-keepalive

Conversation

@mdmohsin7

@mdmohsin7 mdmohsin7 commented Aug 14, 2026 •

Copy link
Copy Markdown
Member

Reverts #11547.

What changed and why

#11547 was premised on flutter_foreground_task holding AVAudioSession on iOS ("green mic all day"). It does not. The plugin's iOS implementation is a BGTaskScheduler refresh task (com.pravera.flutter_foreground_task.refresh, registered in app/ios/Runner/Info.plist) and contains no AVAudioSession reference of any kind — grep -rin "avaudio\|audio" ios/Classes in flutter_foreground_task-9.1.0 returns nothing. Background execution on iOS comes from UIBackgroundModes (audio, location, bluetooth-central) in Info.plist, independent of the plugin. Every downstream decision in that PR — when to hold, when to release, the 5s stale-audio window, the startup grace, the liveness timer — was tuned against a mechanism that does not exist, so the policy cannot be reviewed on its merits.

Four grounds for reverting rather than patching forward:

  1. The stated cause is contradicted by the platform sources (above). ~700 of the 1231 added lines are control logic over a no-op on iOS.

  2. It moves against the defect it cites. iOS background BLE timeouts leave live transcription unavailable for minutes to hours #11307 reports background reconnect gaps from 3.9s to 7,335s and asks for prompt re-establishment of the BLE audio path. fix(ios): stop background audio keep-alive and back off BLE timeouts #11547 adds escalating backoff (200ms → 2s → 10s → 30s → 60s) and states in its own body that iOS background BLE timeouts leave live transcription unavailable for minutes to hours #11307 stays open. The premise behind the backoff is also a misread: iOS central.connect has no timeout — it parks a pending connect at the chipset level (the deleted comment said exactly this), so the 200ms was a delay before parking one, not a poll interval. There was no retry storm to back off from; the attempt rate is set by how fast CoreBluetooth reports failures.

  3. It shipped a duplication that already drifted into a live defect. app/lib/services/devices/ble_reconnect_policy.dart declares itself the "testable SoT" for logic that only executes in Swift, has zero production callers, and is kept in sync by comments alone. Inside fix(ios): stop background audio keep-alive and back off BLE timeouts #11547's own follow-up commits the halves diverged: the Dart audio-UUID set gained Bee (b189a505…), Fieldy (82a48422…) and Plaud (00002bb0…); OmiBleManager.swift audioCharUuids still lists three. Since reconnectAttempt only resets on those three, Bee/Fieldy/Plaud devices never clear the counter and pin at the 60s backoff cap. The whole suite stayed green through that drift, which is the point. capture_keepalive_policy.dart has the same shape at smaller scale: shouldStartForegroundTaskFromLocationPermission() => false has no production caller and its only test asserts false is false.

  4. It repurposed a service across a category boundary silently. The Android service being started and stopped from BLE audio frames is com.pravera.flutter_foreground_task.service.ForegroundService, declared foregroundServiceType="location" — the 5-minute Geolocator poll feeding _onReceiveTaskData in app/lib/pages/home/page.dart. After fix(ios): stop background audio keep-alive and back off BLE timeouts #11547, background location collection is gated on live capture with frames flowing. That is a user-visible data-collection change that appears nowhere in the PR description.

The reported symptom is also still unexplained by the merged change. The only iOS setActive(true) sites are configureForBluetooth in AppDelegate.swift (called from voice_recorder_provider.dart:141, with no matching deactivate), phone-mic, CallKit, and Ray-Ban. The new deactivateForBluetooth is wired only to the capture-controller release path, which the voice recorder never enters — so a session left active there is as stuck after #11547 as before it.

What is worth re-landing separately

The incidental cleanups in that PR are real, live in Swift plus one transport function, and are independently verifiable — they do not need the Dart policy layer:

  • removing the 3s readRSSI timer (it read a cached controller value and never prevented a supervision timeout; the Android keep-alive in OmiBleManager.kt is a different mechanism and is untouched here)
  • everConnected.insert for state-restored peripherals
  • the battery-history write throttle
  • the CCCD re-subscribe after 4s of post-reconnect silence in native_ble_transport.dart

Any re-land should include Bee/Fieldy/Plaud in the Swift audioCharUuids set and drop the Dart mirror.

Product invariants affected

none — no locked invariant path glob in docs/product/invariants/ matches the reverted files.

How it was verified

Source-level verification of the claims above, against the merged tree at b5b77f5fa1:

grep -rin "avaudio|audio" ~/.pub-cache/hosted/pub.dev/flutter_foreground_task-9.1.0/ios/Classes   # no matches
grep -n "foregroundServiceType" app/android/app/src/main/AndroidManifest.xml                      # pravera service = "location"
git grep -n "shouldStartForegroundTaskFromLocationPermission" -- app                              # 1 definition, 1 test, 0 callers
git grep -n "ble_reconnect_policy" -- app                                                         # 0 production callers

OmiBleManager.swift audioCharUuids vs isBleAudioCharacteristicUuid in capture_keepalive_policy.dart: 3 UUIDs vs 6, confirming the drift in ground 3.

This is a mechanical git revert -m 1 b5b77f5fa1; no commit merged after #11547 touches app/, so it applies without conflict and restores the exact tree that shipped before it. Not exercised on a physical device — the revert restores previously shipped behavior rather than introducing new behavior. CI (Dart Analyze & Tests, Android Compile Smoke, Formatting) is the gate for the restored state.

Tests

No new tests. The revert removes the four test files added by #11547 (ble_reconnect_policy_test.dart, capture_keepalive_policy_test.dart, capture_foreground_keepalive_sync_test.dart, native_ble_transport_resync_test.dart) along with the code they cover. Two of them asserted pure functions with no production callers; a regression test for #11307 belongs with a fix that actually addresses it, which #11547 did not claim to be.

Failure class (fixes)

Failure-Class: none

Follow-ups

  • iOS background BLE timeouts leave live transcription unavailable for minutes to hours #11307 stays open and its cause is still unidentified — the reverted change never addressed it.
  • Stuck mic indicator: the candidate is configureForBluetooth with no matching deactivate in voice_recorder_provider.dart, not the foreground task.
  • The 5-minute Geolocator.getCurrentPosition() poll restored by this revert is worth removing on its own merit, as a one-line change with its own evidence.

@mdmohsin7
mdmohsin7 merged commit 27261a6 into main Aug 14, 2026
35 checks passed
@mdmohsin7
mdmohsin7 deleted the revert-11547-fix/ios-background-battery-keepalive branch August 14, 2026 10:47
Git-on-my-level added a commit that referenced this pull request Aug 14, 2026
Stop the 3s readRSSI keep-alive (poll only while diagnostics is
subscribed), mark CoreBluetooth-restored peripherals as everConnected,
throttle battery-history writes, and retry a silent audio CCCD once
after reconnect. Audio notify UUIDs live next to the constants in
models.dart; no Dart reconnect-policy mirror.

Salvaged from #11547 as called out in #11567.

Failure-Class: none
Co-authored-by: Cursor <[email protected]>
Git-on-my-level added a commit that referenced this pull request Aug 20, 2026
…1576)

* fix(ios): salvage incidental BLE cleanups from reverted keepalive

Stop the 3s readRSSI keep-alive (poll only while diagnostics is
subscribed), mark CoreBluetooth-restored peripherals as everConnected,
throttle battery-history writes, and retry a silent audio CCCD once
after reconnect. Audio notify UUIDs live next to the constants in
models.dart; no Dart reconnect-policy mirror.

Salvaged from #11547 as called out in #11567.

Failure-Class: none
Co-authored-by: Cursor <[email protected]>

* fix: address review — restore battery throttle baseline and cancel liveness timer on disconnect

- Rehydrate lastPersistedBatteryLevel/AtMs from the newest persisted
  history entry so the first battery notification after a process
  restart is throttled against the prior persisted sample.
- Cancel _audioLivenessTimer and reset _audioSilenceResubscribes at the
  top of disconnect(), matching dispose() and _handleConnectionState(false),
  so a stale watch cannot fire into a later connection.

---------

Co-authored-by: Cursor <[email protected]>
Co-authored-by: David Zhang <[email protected]>
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