Skip to content

Commit

Permalink
Future proof threading / scheduled task flows
Browse files Browse the repository at this point in the history
This adds a guard to ensure that we only attempt any further scans when
the scan cycler is started. This helps ensure the cycler has terminated
scanning for the case where all regions have been removed but the
service is still bound. In this case only `stop` will be called (instead
of both `stop` and `destroy`). When this happens there are two potential
scheduled tasks still queued:

- `scanLeDevice(true)` from `deferScanIfNeeded`
- `scheduleScanCycleStop`

Which potential task is queued is based on if we were in an active scan
period or a between scan period when `stop` was called. In the case of a
between scan period the `deferScanIfNeeded` _will_ cause a faux scanning
cycle to start up again when it ends (scanning won't actually start, but
all flags and internal state appear as if it is running). It will run
for one more scan cycle before it terminates in `finishScanCycle`.
Similarly, if we are in an active scan period the scheduled job for
finishing the scan cycle will continue for the duration of the scan
period.
  • Loading branch information
cupakromer committed May 26, 2017
1 parent 5f98c55 commit 0ab8bee
Showing 1 changed file with 5 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ protected void scanLeDevice(final Boolean enable) {
if (getBluetoothAdapter() == null) {
LogManager.e(TAG, "No Bluetooth adapter. beaconService cannot scan.");
}
if (enable) {
if (mScanningEnabled && enable) {
if (deferScanIfNeeded()) {
return;
}
Expand Down Expand Up @@ -283,6 +283,9 @@ protected void scanLeDevice(final Boolean enable) {
mScanCyclerStarted = false;
stopScan();
mLastScanCycleEndTime = SystemClock.elapsedRealtime();
// Clear any queued schedule tasks as we're done scanning
mScanHandler.removeCallbacksAndMessages(null);
finishScanCycle();
}
}
catch (SecurityException e) {
Expand All @@ -294,7 +297,7 @@ protected void scanLeDevice(final Boolean enable) {
protected void scheduleScanCycleStop() {
// Stops scanning after a pre-defined scan period.
long millisecondsUntilStop = mScanCycleStopTime - SystemClock.elapsedRealtime();
if (millisecondsUntilStop > 0) {
if (mScanningEnabled && millisecondsUntilStop > 0) {
LogManager.d(TAG, "Waiting to stop scan cycle for another %s milliseconds",
millisecondsUntilStop);
if (mBackgroundFlag) {
Expand Down

0 comments on commit 0ab8bee

Please sign in to comment.