Skip to content

Commit 9d83df6

Browse files
committed
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.
1 parent 39a98f4 commit 9d83df6

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import android.os.SystemClock;
1717
import android.support.annotation.MainThread;
1818
import android.support.annotation.NonNull;
19+
import android.support.annotation.WorkerThread;
1920

2021
import org.altbeacon.beacon.BeaconManager;
2122
import org.altbeacon.beacon.logging.LogManager;
@@ -198,17 +199,19 @@ public void setDistinctPacketsDetectedPerScan(boolean detected) {
198199
@MainThread
199200
public void destroy() {
200201
LogManager.d(TAG, "Destroying");
202+
203+
// Remove any postDelayed Runnables queued for the next scan cycle
204+
mHandler.removeCallbacksAndMessages(null);
205+
201206
// We cannot quit the thread used by the handler until queued Runnables have been processed,
202207
// because the handler is what stops scanning, and we do not want scanning left on.
203208
// So we stop the thread using the handler, so we make sure it happens after all other
204209
// waiting Runnables are finished.
205-
mHandler.post(new Runnable() {
206-
@MainThread
210+
mScanHandler.post(new Runnable() {
211+
@WorkerThread
207212
@Override
208213
public void run() {
209214
LogManager.d(TAG, "Quitting scan thread");
210-
// Remove any postDelayed Runnables queued for the next scan cycle
211-
mHandler.removeCallbacksAndMessages(null);
212215
mScanThread.quit();
213216
}
214217
});

0 commit comments

Comments
 (0)