Skip to content

Commit

Permalink
Fix where scanner thread is quit.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
cupakromer committed May 26, 2017
1 parent 1be324b commit 5f98c55
Showing 1 changed file with 7 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
});
Expand Down

0 comments on commit 5f98c55

Please sign in to comment.