fix: Stop retrying auto-connect after SecurityException

BluetoothHeadset.connect() requires MODIFY_PHONE_STATE on modern Android, which is a system-only permission. Detect the SecurityException and stop further attempts instead of retrying every second.
This commit is contained in:
darken
2026-03-23 11:24:23 +00:00
committed by Matthias Urhahn
parent db759bac7c
commit 53447f3fb9
3 changed files with 16 additions and 1 deletions
@@ -304,6 +304,9 @@ class BluetoothManager2 @Inject constructor(
emit(wrappedDevices)
}
private var _isNudgeAvailable: Boolean = true
val isNudgeAvailable: Boolean get() = _isNudgeAvailable
suspend fun nudgeConnection(device: BluetoothDevice2): Boolean = getBluetoothProfile().map { bluetoothProfile ->
try {
log(TAG) { "Nudging Android connection to $device" }
@@ -317,6 +320,12 @@ class BluetoothManager2 @Inject constructor(
log(TAG) { "Nudged connection to $device" }
true
} catch (e: Exception) {
val isSecurityException = e is SecurityException ||
(e is java.lang.reflect.InvocationTargetException && e.cause is SecurityException)
if (isSecurityException) {
log(TAG, ERROR) { "nudgeConnection is permanently unavailable: missing MODIFY_PHONE_STATE permission" }
_isNudgeAvailable = false
}
Bugs.report(tag = TAG, "BluetoothHeadset.connect(device) is unavailable", exception = e)
false
}
@@ -99,6 +99,12 @@ class AutoConnect @Inject constructor(
log(TAG) { "Auto connect condition ($condition) is not fullfilled: ${decision.reason}" }
return@map
}
if (!bluetoothManager.isNudgeAvailable) {
log(TAG, WARN) { "nudgeConnection is not available on this device, skipping" }
return@map
}
val result = bluetoothManager.nudgeConnection(bondedDevice)
log(TAG) { "nudgeConnection($bondedDevice) returned $result" }
}
+1 -1
View File
@@ -61,7 +61,7 @@
<string name="settings_signal_minimum_label">Minimum signal quality</string>
<string name="settings_signal_minimum_description">The minimum signal quality that a device needs to have to be considered yours.</string>
<string name="settings_autoconnect_label">Auto connect</string>
<string name="settings_autoconnect_description">If Android does not automatically connect, we can ask it too. This will set the monitor mode setting to \'Always\'.</string>
<string name="settings_autoconnect_description">If Android does not automatically connect, we can ask it too. This will set the monitor mode setting to \'Always\'. May not work on newer Android versions.</string>
<string name="settings_autoconnect_condition_label">Auto connect condition</string>
<string name="settings_autoconnect_condition_description">When should we try to connect to your device?</string>
<string name="settings_devices_label">Devices</string>