Device: Tune Bluetooth scanning power automatically#560
Merged
Conversation
…ections Replaces the user-facing scanner mode setting with an automatic policy that picks LOW_LATENCY when a profile-paired device is connected, BALANCED in the foreground, and LOW_POWER in the background. The TroubleShooter scopes a temporary LOW_LATENCY override via a refcounted withTemporaryOverride block so overlapping callers stay correct. Fixes a regression where the controller could block BLE scanning entirely if BLUETOOTH_CONNECT was missing or the HEADSET profile proxy stalled, and adds a reactive bondedDeviceAddresses flow so bond changes propagate without waiting for an unrelated input. Cleans up the now-dead scanner mode strings across all locales and unused ScannerMode fields.
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.
What changed
The app now picks the right Bluetooth scanning power automatically based on the situation:
The "Scanner mode" setting in General Settings is gone — the app decides based on context. The TroubleShooter still bumps to Low Latency while it's actively scanning.
Also fixes a case where scanning could fail to start for users who hadn't granted the Bluetooth Connect permission, or where Android's headset profile took a while to come up.
Refs #41, #124, #137
Technical Context
BleScanModeController.scannerModecombine includesbluetoothManager.connectedDevices, which isstateIn(initialValue = null).filterNotNull()— it never emits if the HEADSET profile proxy stalls orBLUETOOTH_CONNECTis missing. The combine then never produced a mode andBlePodMonitor.createBleScanner()blocked forever. Wrapped that source with.onStart { emit(emptyList()) }.catch { emit(emptyList()) }so the controller emits within one tick and upgrades when real data arrives.setTemporaryOverride / clearTemporaryOverridewithsuspend fun <T> withTemporaryOverride(mode, block): T. Internal state isMutableStateFlow<Map<ScannerMode, Int>>; exposed mode picks the highest-priority active override (LOW_LATENCY > BALANCED > LOW_POWER). Eliminates the race where one caller'sfinallycould clear another caller's override.BluetoothManager2.bondedDeviceAddresses: Flow<Set<BluetoothAddress>>driven byACTION_BOND_STATE_CHANGED, replacing a one-shotbondedDevices().first()query insidemapLatest. Receiver-path query is wrapped in try/catch so a thrownSecurityException(e.g. permission revoked at runtime) doesn't escapeonReceive.withTemporaryOverride(LOW_LATENCY) override@{ ... }.return@overridereplaces the oldreturn@launchto keep early-exit paths legal through the non-inline lambda.ScannerMode.{identifier, labelRes}removed (dead after UI deletion). Fivesettings_scanner_mode_*keys removed across all 76 locale files. The DataStore keycore.scanner.moderetains stale values silently — no migration needed.AppleDeviceProfiles, which the new policy can't match for LOW_LATENCY until the user links a paired address. Pre-existing behavior, deferred.Review checklist
BLUETOOTH_CONNECTis granted (regression guard)Bluetooth/ScannerModelog showsBALANCEDin foreground without connected profile,LOW_POWERin background,LOW_LATENCYonce profiled+bonded headphones connectR.string.settings_scanner_mode_*orgeneralSettings.scannerMode