From 5f98c55aeff9cac25c8db038f467497a0e82307d Mon Sep 17 00:00:00 2001 From: Aaron Kromer Date: Wed, 24 May 2017 14:09:32 -0400 Subject: [PATCH] Fix where scanner thread is quit. This fixes what I believe was a simply copy-paste error regarding how the scanner thread is quit. This uses the scanner thread handler, instead of the main thread handler, to schedule the `quit` operation. This way any queued scan jobs (such as a stop) complete before we quit the thread. This is what the comment states, but wasn't what the code implemented. The `mHandler.removeCallbacksAndMessages` is removed from the background job as `destroy` is already on the main thread. So we want to clear any other jobs still in the queue to run later now as we are in the process of destroying everything. --- .../beacon/service/scanner/CycledLeScanner.java | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/altbeacon/beacon/service/scanner/CycledLeScanner.java b/src/main/java/org/altbeacon/beacon/service/scanner/CycledLeScanner.java index a176da53d..fb8126ca8 100644 --- a/src/main/java/org/altbeacon/beacon/service/scanner/CycledLeScanner.java +++ b/src/main/java/org/altbeacon/beacon/service/scanner/CycledLeScanner.java @@ -16,6 +16,7 @@ import android.os.SystemClock; import android.support.annotation.MainThread; import android.support.annotation.NonNull; +import android.support.annotation.WorkerThread; import org.altbeacon.beacon.BeaconManager; import org.altbeacon.beacon.logging.LogManager; @@ -198,17 +199,19 @@ public void setDistinctPacketsDetectedPerScan(boolean detected) { @MainThread public void destroy() { LogManager.d(TAG, "Destroying"); + + // Remove any postDelayed Runnables queued for the next scan cycle + mHandler.removeCallbacksAndMessages(null); + // We cannot quit the thread used by the handler until queued Runnables have been processed, // because the handler is what stops scanning, and we do not want scanning left on. // So we stop the thread using the handler, so we make sure it happens after all other // waiting Runnables are finished. - mHandler.post(new Runnable() { - @MainThread + mScanHandler.post(new Runnable() { + @WorkerThread @Override public void run() { LogManager.d(TAG, "Quitting scan thread"); - // Remove any postDelayed Runnables queued for the next scan cycle - mHandler.removeCallbacksAndMessages(null); mScanThread.quit(); } });