Device: Fix AirPods features not working until app restart after granting permission#531
Merged
Merged
Conversation
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
Fixed a bug where AirPods feature support (ANC toggle, stem controls, learned settings, firmware-level state) would stay broken until the app was force-stopped and relaunched, if the user granted the Bluetooth permission after first launch.
Typical repro: install the app, launch it, create a profile, then grant BLUETOOTH_CONNECT when prompted. BLE scanning started working but the Apple protocol layer never came up — the main screen and widget kept showing battery but no firmware-level features. Killing the app and reopening it fixed it.
Technical Context
start()was called once fromDeviceMonitor.initand subscribed a merged flow inappScope. On first launch without BLUETOOTH_CONNECT, the innerbondedDevices().first()threwSecurityException; the existing.catch {}absorbed it and the merged flow completed — the launched Job was done forever, with no resubscription when permission was granted. Fix: wrap the merge inpermissionTool.missingPermissions.flatMapLatestgated onPermission.BLUETOOTH_CONNECT, plus an innerretryWhenwith capped backoff for mid-run transient failures. Mirrors the established pattern inMonitorService.doMonitor().retryWhengave up after 3 attempts and.catch { emit(emptyList()) }terminated the flow. The backingstateInStateFlow then served its cached empty list to any new subscriber indefinitely — so even after the AAP lifecycle fix re-subscribed,connectedDevicesremained frozen at[]and no AAP connect was ever attempted. Fix: inretryWhen, treatSecurityExceptionas non-terminal and keep retrying with a 3s backoff; the next attempt succeeds as soon as the user grants the permission. The 3-attempt limit still applies to other causes.BLUETOOTH_CONNECTdoesn't exist inPermission.entries) becausePermissionTool.missingPermissionsalready filters byPermission.isRequired(context)which enforces the API-level bound.initialConnect→SecurityExceptionatBluetoothManager2.kt:295→aapActive.onCompletion(first bug), and after the first fix, (2) AAP lifecycle starts but every combine emission seesconnectedDevices=[]because the BluetoothManager2 StateFlow stayed frozen from its earlier retry exhaustion (second bug).