From 7816e131e99cb609cbba15cdc544bd1476fb97a1 Mon Sep 17 00:00:00 2001 From: Daniel Dakhno Date: Mon, 19 Feb 2024 02:38:19 +0100 Subject: [PATCH] Device connection: stop ScanService when quitting application --- .../service/btle/BLEScanService.java | 26 ++++++++++++++----- 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/btle/BLEScanService.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/btle/BLEScanService.java index db3823adb..93d1086ca 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/btle/BLEScanService.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/btle/BLEScanService.java @@ -289,14 +289,25 @@ public class BLEScanService extends Service { BroadcastReceiver deviceStateUpdateReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { - GBDevice.DeviceUpdateSubject subject = - (GBDevice.DeviceUpdateSubject) - intent.getSerializableExtra(GBDevice.EXTRA_UPDATE_SUBJECT); + if(GBDevice.ACTION_DEVICE_CHANGED.equals(intent.getAction())) { + GBDevice.DeviceUpdateSubject subject = + (GBDevice.DeviceUpdateSubject) + intent.getSerializableExtra(GBDevice.EXTRA_UPDATE_SUBJECT); - if(subject != GBDevice.DeviceUpdateSubject.CONNECTION_STATE){ + if (subject != GBDevice.DeviceUpdateSubject.CONNECTION_STATE) { + return; + } + restartScan(true); + return; + } + if(GBApplication.ACTION_QUIT.equals(intent.getAction())){ + LOG.debug("stopping scan service..."); + if(currentState.isDoingAnyScan()){ + scanner.stopScan(scanCallback); + } + stopSelf(); return; } - restartScan(true); } }; @@ -320,9 +331,12 @@ public class BLEScanService extends Service { }; private void registerReceivers(){ + IntentFilter filter = new IntentFilter(); + filter.addAction(GBDevice.ACTION_DEVICE_CHANGED); + filter.addAction(GBApplication.ACTION_QUIT); localBroadcastManager.registerReceiver( deviceStateUpdateReceiver, - new IntentFilter(GBDevice.ACTION_DEVICE_CHANGED) + filter ); registerReceiver(