Skip to content

Commit

Permalink
Merge pull request Fitbit#17 from Fitbit/irvin/bugfix/fix-max-client-…
Browse files Browse the repository at this point in the history
…if-bug

PI scans must be stopped
  • Loading branch information
irvinowens authored Oct 11, 2019
2 parents ce78c08 + ffe5c85 commit bd54b12
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
9 changes: 7 additions & 2 deletions src/main/java/com/fitbit/bluetooth/fbgatt/FitbitGatt.java
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ public class FitbitGatt implements PeripheralScanner.TrackerScannerListener, Blu
@VisibleForTesting
AtomicBoolean isStarted = new AtomicBoolean(false);
private Handler connectionCleanup;
@Nullable
private LooperWatchdog asyncOperationThreadWatchdog;
// this should be max priority so as to not affect performance
private HandlerThread fitbitGattAsyncOperationThread = new HandlerThread("FitbitGatt Async Operation Thread", Thread.MAX_PRIORITY);
Expand Down Expand Up @@ -831,7 +832,9 @@ private synchronized boolean startSimple(Context context) {
addConnectedDevices(context);
// will start the cleanup process
decrementAndInvalidateClosedConnections();
asyncOperationThreadWatchdog.startProbing();
if(asyncOperationThreadWatchdog != null) {
asyncOperationThreadWatchdog.startProbing();
}
return true;
}

Expand Down Expand Up @@ -891,7 +894,9 @@ void shutdown() {
radioStatusListener.stopListening();
radioStatusListener.removeListener();
}
this.asyncOperationThreadWatchdog.stopProbing();
if(asyncOperationThreadWatchdog != null) {
this.asyncOperationThreadWatchdog.stopProbing();
}
}

@VisibleForTesting
Expand Down
20 changes: 14 additions & 6 deletions src/main/java/com/fitbit/bluetooth/fbgatt/PeripheralScanner.java
Original file line number Diff line number Diff line change
Expand Up @@ -466,12 +466,13 @@ public boolean isPeriodicalScanEnabled() {

synchronized void cancelPendingIntentBasedBackgroundScan() {
if (atLeastSDK(Build.VERSION_CODES.O)) {
Context appContext = FitbitGatt.getInstance().getAppContext();
if (!scanner.isBluetoothEnabled()) {
Timber.v("No scanners can be started while bluetooth is off");
// must release the scanner here so that the system can clean it up since
// we can't access it with bt off
synchronized (PeripheralScanner.class) {
scanner = new BitgattLeScanner(FitbitGatt.getInstance().getAppContext());
scanner = new BitgattLeScanner(appContext);
}
boolean oldValue = pendingIntentIsScanning.getAndSet(false);
Timber.v("Stopping scan, changing from %b to %b", oldValue, false);
Expand All @@ -480,6 +481,9 @@ synchronized void cancelPendingIntentBasedBackgroundScan() {
}
if (backgroundIntentBasedScanIntent != null) {
scanner.stopScan(backgroundIntentBasedScanIntent);
} else {
Timber.i("No existing pending intent, cancelling using system intent just in case ...");
scanner.stopScan(getSystemPendingIntent(appContext));
}
stopPeriodicalScan = false;
backgroundIntentBasedScanIntent = null;
Expand Down Expand Up @@ -535,11 +539,7 @@ synchronized boolean startPendingIntentBasedBackgroundScan(@NonNull List<ScanFil
listener.onPendingIntentScanStatusChanged(pendingIntentIsScanning.get());
return false;
}
Intent broadcastIntent = new Intent(context, HandleIntentBasedScanResult.class);
broadcastIntent.setAction(SCANNED_DEVICE_ACTION);
broadcastIntent.setClass(context, HandleIntentBasedScanResult.class);
backgroundIntentBasedScanIntent = PendingIntent.getBroadcast(context,
BACKGROUND_SCAN_REQUEST_CODE, broadcastIntent, PendingIntent.FLAG_UPDATE_CURRENT);
backgroundIntentBasedScanIntent = getSystemPendingIntent(context);
int didStart;
try {
didStart = scanner.startScan(scanFilters, null, backgroundIntentBasedScanIntent);
Expand Down Expand Up @@ -589,6 +589,14 @@ synchronized boolean startPendingIntentBasedBackgroundScan(@NonNull List<ScanFil
}
}

private PendingIntent getSystemPendingIntent(Context context){
Intent broadcastIntent = new Intent(context, HandleIntentBasedScanResult.class);
broadcastIntent.setAction(SCANNED_DEVICE_ACTION);
broadcastIntent.setClass(context, HandleIntentBasedScanResult.class);
return PendingIntent.getBroadcast(context,
BACKGROUND_SCAN_REQUEST_CODE, broadcastIntent, PendingIntent.FLAG_UPDATE_CURRENT);
}

/**
* Will start a background scan that will continue to run even if our process is killed.
*
Expand Down

0 comments on commit bd54b12

Please sign in to comment.