Skip to content

Commit

Permalink
Fix issue on phones that don't stop scanning properly when toggling b…
Browse files Browse the repository at this point in the history
…luetooth
  • Loading branch information
weliem committed Oct 22, 2021
1 parent 55bbbef commit d29b82f
Showing 1 changed file with 15 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,10 @@ private void startScan(@NotNull final List<ScanFilter> filters, @NotNull final S

if (bluetoothScanner == null) {
bluetoothScanner = bluetoothAdapter.getBluetoothLeScanner();

// On some phones like Nokia 8, this scanner may still have an older active scan from us
// This happens when bluetooth is toggled. So make sure it is gone.
bluetoothScanner.stopScan(scanCallback);
}

if (bluetoothScanner != null) {
Expand Down Expand Up @@ -567,6 +571,7 @@ public void stopScan() {
}
currentCallback = null;
currentFilters = null;
bluetoothScanner = null;
scannedPeripherals.clear();
}

Expand Down Expand Up @@ -1135,13 +1140,19 @@ private void handleAdapterState(final int state) {
break;
case BluetoothAdapter.STATE_TURNING_OFF:
// Stop all scans so that we are back in a clean state
// Note that we can't call stopScan if the adapter is off
if (isScanning()) {
stopScan();
// Note that we can't call stopScan if the adapter is off
// On some phones like the Nokia 8, the adapter will be already off at this point
// So add a try/catch to handle any exceptions
try {
stopScan();
} catch (Exception ignored) { }
}

if(isAutoScanning()) {
stopAutoconnectScan();
try {
stopAutoconnectScan();
} catch (Exception ignored) { }
}

expectingBluetoothOffDisconnects = true;
Expand All @@ -1151,6 +1162,7 @@ private void handleAdapterState(final int state) {
currentCallback = null;
currentFilters = null;
autoConnectScanner = null;
bluetoothScanner = null;
Logger.d(TAG,"bluetooth turning off");
break;
case BluetoothAdapter.STATE_ON:
Expand Down

0 comments on commit d29b82f

Please sign in to comment.