Skip to content

Commit 61a7133

Browse files
committed
Wait to shut down Handler Thread used start/stop scans to avoid leaving scanning on
1 parent 243f790 commit 61a7133

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

src/main/java/org/altbeacon/beacon/service/scanner/CycledLeScanner.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,6 @@ public void stop() {
157157
mScanningEnabled = false;
158158
if (mScanCyclerStarted) {
159159
scanLeDevice(false);
160-
LogManager.d(TAG, "forcing scanning to stop even for devices capable of multiple detections per cycle");
161-
stopScan();
162160
} else {
163161
LogManager.d(TAG, "scanning already stopped");
164162
}
@@ -173,7 +171,20 @@ public void setDistinctPacketsDetectedPerScan(boolean detected) {
173171
}
174172

175173
public void destroy() {
176-
mScanThread.quit();
174+
LogManager.d(TAG, "Destroying");
175+
// We cannot quit the thread used by the handler until queued Runnables have been processed,
176+
// because the handler is what stops scanning, and we do not want scanning left on.
177+
// So we stop the thread using the handler, so we make sure it happens after all other
178+
// waiting Runnables are finished.
179+
mHandler.post(new Runnable() {
180+
@Override
181+
public void run() {
182+
LogManager.d(TAG, "Quitting scan thread");
183+
// Remove any postDelayed Runnables queued for the next scan cycle
184+
mHandler.removeCallbacksAndMessages(null);
185+
mScanThread.quit();
186+
}
187+
});
177188
}
178189

179190
protected abstract void stopScan();

src/main/java/org/altbeacon/beacon/service/scanner/CycledLeScannerForLollipop.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ private void postStopLeScan() {
223223
@Override
224224
public void run() {
225225
try {
226+
LogManager.d(TAG, "Stopping LE scan on scan handler");
226227
scanner.stopScan(scanCallback);
227228
} catch (IllegalStateException e) {
228229
LogManager.w(TAG, "Cannot stop scan. Bluetooth may be turned off.");

0 commit comments

Comments
 (0)