Avoid crash when BLE Scan Service is enabled on zip restore

This commit is contained in:
José Rebelo
2026-04-23 22:29:03 +01:00
parent 7f2255c00f
commit 1b84dd1b54
@@ -231,10 +231,17 @@ public class BLEScanService extends Service {
private void startForeground() { private void startForeground() {
Notification serviceNotification = createNotification(false, 0); Notification serviceNotification = createNotification(false, 0);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { try {
ServiceCompat.startForeground(this, GB.NOTIFICATION_ID_SCAN, serviceNotification, ServiceInfo.FOREGROUND_SERVICE_TYPE_CONNECTED_DEVICE); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
} else { ServiceCompat.startForeground(this, GB.NOTIFICATION_ID_SCAN, serviceNotification, ServiceInfo.FOREGROUND_SERVICE_TYPE_CONNECTED_DEVICE);
ServiceCompat.startForeground(this, GB.NOTIFICATION_ID_SCAN, serviceNotification, 0); } else {
ServiceCompat.startForeground(this, GB.NOTIFICATION_ID_SCAN, serviceNotification, 0);
}
} catch (final SecurityException e) {
// FOREGROUND_SERVICE_TYPE_CONNECTED_DEVICE will fail if the user has not granted the necessary permissions
// this can cause a crash on startup when restoring a zip with BLE scan service enabled
LOG.error("Failed to start foreground service", e);
updateNotification(e.getMessage());
} }
} }