-
Notifications
You must be signed in to change notification settings - Fork 840
Fix failure to stop scans #507
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
@@ -157,6 +157,8 @@ public void stop() { | |||
mScanningEnabled = false; | |||
if (mScanCyclerStarted) { | |||
scanLeDevice(false); | |||
LogManager.d(TAG, "forcing scanning to stop even for devices capable of multiple detections per cycle"); | |||
stopScan(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is stopScan()
required here?
The call to scanLeDevice(false)
will call it.
I think the issue is potentially in the Toggling bind/unbind a few times seemed to also put it into a continuous scan state but there is too much noise here to get a cleaner debug log output at the moment. |
So the original fix in this PR worked only by altering a race condition. The issue is that starting/stopping scanning is done on a dedicated Thread using a Handler, and when the CycledLeScanner is shut down, that thread may be terminated before it can stop the scan. Whether or not this succeeds is a race condition. The revised solution is to allow the latest Runnable posted to the Handler to complete before shutting down the Thread. Logs after update (Note that "Quitting scan thread" appears only after "Stopping LE scan on scan handler". This is now deterministic.):
|
This fixes a failure to stop BLE scanning in two scenarios caused by #491:
This was reported here: http://stackoverflow.com/questions/43835705/how-to-properly-stop-scanning-beacon-using-android-beacon-library-altbeacon/43836157#43836157
Testing on Nexus 5X, showing logs after a call to
BeaconReferenceApplication.this.unbindService(connection);
Without change (note beacon packets are detected after service stops):
With change (note no beacon packets are detected after service stops):