Skip to content

Commit 52435b1

Browse files
authored
Merge pull request #507 from AltBeacon/fix-failure-to-stop-scans
Fix failure to stop scans
2 parents 2fb0c0b + 61a7133 commit 52435b1

File tree

3 files changed

+18
-3
lines changed

3 files changed

+18
-3
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ Enhancements:
88

99
Bug Fixes:
1010

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

1314
### 2.10 / 2017-04-21
1415

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

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,20 @@ public void setDistinctPacketsDetectedPerScan(boolean detected) {
171171
}
172172

173173
public void destroy() {
174-
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+
});
175188
}
176189

177190
protected abstract void stopScan();
@@ -285,7 +298,7 @@ private void finishScanCycle() {
285298
// so it is best avoided. If we know the device has detected to distinct
286299
// packets in the same cycle, we will not restart scanning and just keep it
287300
// going.
288-
if (!getDistinctPacketsDetectedPerScan()) {
301+
if (!getDistinctPacketsDetectedPerScan() || mBetweenScanPeriod != 0) {
289302
long now = SystemClock.elapsedRealtime();
290303
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.N &&
291304
mBetweenScanPeriod+mScanPeriod < ANDROID_N_MIN_SCAN_CYCLE_MILLIS &&

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)