Skip to content

Commit

Permalink
Merge pull request #507 from AltBeacon/fix-failure-to-stop-scans
Browse files Browse the repository at this point in the history
Fix failure to stop scans
  • Loading branch information
davidgyoung authored May 13, 2017
2 parents 2fb0c0b + 61a7133 commit 52435b1
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ Enhancements:

Bug Fixes:

- TODO
- Fix failure to stop scanning when unbinding from service or when the between scan period
is nonzero. (#507, David G. Young)

### 2.10 / 2017-04-21

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,20 @@ public void setDistinctPacketsDetectedPerScan(boolean detected) {
}

public void destroy() {
mScanThread.quit();
LogManager.d(TAG, "Destroying");
// 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() {
@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();
}
});
}

protected abstract void stopScan();
Expand Down Expand Up @@ -285,7 +298,7 @@ private void finishScanCycle() {
// so it is best avoided. If we know the device has detected to distinct
// packets in the same cycle, we will not restart scanning and just keep it
// going.
if (!getDistinctPacketsDetectedPerScan()) {
if (!getDistinctPacketsDetectedPerScan() || mBetweenScanPeriod != 0) {
long now = SystemClock.elapsedRealtime();
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.N &&
mBetweenScanPeriod+mScanPeriod < ANDROID_N_MIN_SCAN_CYCLE_MILLIS &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ private void postStopLeScan() {
@Override
public void run() {
try {
LogManager.d(TAG, "Stopping LE scan on scan handler");
scanner.stopScan(scanCallback);
} catch (IllegalStateException e) {
LogManager.w(TAG, "Cannot stop scan. Bluetooth may be turned off.");
Expand Down

0 comments on commit 52435b1

Please sign in to comment.