fix: Guard MonitorService start against missing Bluetooth permissions

This commit is contained in:
darken
2026-02-09 14:24:45 +01:00
committed by Matthias Urhahn
parent 1af8743533
commit cfed733fa7
2 changed files with 15 additions and 5 deletions
@@ -6,6 +6,7 @@ import eu.darken.capod.common.debug.logging.Logging.Priority.VERBOSE
import eu.darken.capod.common.debug.logging.Logging.Priority.WARN
import eu.darken.capod.common.debug.logging.log
import eu.darken.capod.common.debug.logging.logTag
import eu.darken.capod.common.permissions.Permission
import eu.darken.capod.common.startServiceCompat
import javax.inject.Inject
import javax.inject.Singleton
@@ -19,11 +20,21 @@ class MonitorControl @Inject constructor(
forceStart: Boolean = false,
) {
log(TAG, VERBOSE) { "startMonitor(forceStart=$forceStart)" }
val hasBluetoothPermission =
Permission.BLUETOOTH.isGranted(context) || Permission.BLUETOOTH_CONNECT.isGranted(context)
if (!hasBluetoothPermission) {
log(TAG, WARN) { "Missing Bluetooth permission, not starting monitor service." }
return
}
try {
context.startServiceCompat(MonitorService.intent(context, forceStart))
log(TAG) { "Monitor start request sent." }
} catch (e: IllegalStateException) {
log(TAG, WARN) { "Failed to start monitor service: ${e.message}" }
} catch (e: SecurityException) {
log(TAG, WARN) { "Failed to start monitor service, permission issue: ${e.message}" }
}
}