@@ -1356,7 +1356,11 @@ protected void onHandleIntent(@Nullable final Intent intent) {
1356
1356
final Intent newIntent = new Intent ();
1357
1357
newIntent .fillIn (intent , Intent .FILL_IN_COMPONENT | Intent .FILL_IN_PACKAGE );
1358
1358
newIntent .putExtra (EXTRA_RECONNECTION_ATTEMPT , attempt + 1 );
1359
- startService (newIntent );
1359
+
1360
+ if (foregroundService && Build .VERSION .SDK_INT >= Build .VERSION_CODES .O )
1361
+ startForegroundService (newIntent );
1362
+ else
1363
+ startService (newIntent );
1360
1364
return ;
1361
1365
}
1362
1366
terminateConnection (gatt , mError );
@@ -1415,7 +1419,11 @@ protected void onHandleIntent(@Nullable final Intent intent) {
1415
1419
final Intent newIntent = new Intent ();
1416
1420
newIntent .fillIn (intent , Intent .FILL_IN_COMPONENT | Intent .FILL_IN_PACKAGE );
1417
1421
newIntent .putExtra (EXTRA_DFU_ATTEMPT , attempt + 1 );
1418
- startService (newIntent );
1422
+
1423
+ if (foregroundService && Build .VERSION .SDK_INT >= Build .VERSION_CODES .O )
1424
+ startForegroundService (newIntent );
1425
+ else
1426
+ startService (newIntent );
1419
1427
return ;
1420
1428
}
1421
1429
report (ERROR_DEVICE_DISCONNECTED );
@@ -1914,7 +1922,12 @@ private void startForeground() {
1914
1922
// Any additional configuration?
1915
1923
updateForegroundNotification (builder );
1916
1924
1917
- startForeground (NOTIFICATION_ID , builder .build ());
1925
+ try {
1926
+ startForeground (NOTIFICATION_ID , builder .build ());
1927
+ } catch (final SecurityException e ) {
1928
+ loge ("Service cannot be started in foreground" , e );
1929
+ logi ("Starting DFU service in background instead" );
1930
+ }
1918
1931
}
1919
1932
1920
1933
/**
0 commit comments