summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author Evan Chen <evanxinchen@google.com> 2022-09-26 16:42:13 +0000
committer Evan Chen <evanxinchen@google.com> 2022-09-29 23:32:27 +0000
commitb4f64701cb6cefd50e15c36056db3683a6adfc4c (patch)
treecc8fa651af8867071e5bfb1f767cfa0add6433f0
parent3fd2b83e1faf09f99e04d8b93b5e51a99d35f070 (diff)
Catch the exception when ble startScan
Fix: 248162096, 245003000 Test: atest CtsCompanionDeviceManagerUiAutomationTestCases atest CtsOsTestCases:CompanionDeviceManagerTest Change-Id: I93c564811910017af4b1a5323b0065ba4104cdb2
-rw-r--r--services/companion/java/com/android/server/companion/presence/BleCompanionDeviceScanner.java36
1 files changed, 27 insertions, 9 deletions
diff --git a/services/companion/java/com/android/server/companion/presence/BleCompanionDeviceScanner.java b/services/companion/java/com/android/server/companion/presence/BleCompanionDeviceScanner.java
index ad09f7cd3dde..33f1b4282e97 100644
--- a/services/companion/java/com/android/server/companion/presence/BleCompanionDeviceScanner.java
+++ b/services/companion/java/com/android/server/companion/presence/BleCompanionDeviceScanner.java
@@ -184,13 +184,21 @@ class BleCompanionDeviceScanner implements AssociationStore.OnChangeListener {
@MainThread
private void startScan() {
enforceInitialized();
- // This method should not be called if scan is already in progress.
- if (mScanning) throw new IllegalStateException("Scan is already in progress.");
- // Neither should this method be called if the adapter is not available.
- if (mBleScanner == null) throw new IllegalStateException("BLE is not available.");
if (DEBUG) Log.i(TAG, "startScan()");
+ // This method should not be called if scan is already in progress.
+ if (mScanning) {
+ Slog.w(TAG, "Scan is already in progress.");
+ return;
+ }
+
+ // Neither should this method be called if the adapter is not available.
+ if (mBleScanner == null) {
+ Slog.w(TAG, "BLE is not available.");
+ return;
+ }
+
// Collect MAC addresses from all associations.
final Set<String> macAddresses = new HashSet<>();
for (AssociationInfo association : mAssociationStore.getAssociations()) {
@@ -221,8 +229,18 @@ class BleCompanionDeviceScanner implements AssociationStore.OnChangeListener {
filters.add(filter);
}
- mBleScanner.startScan(filters, SCAN_SETTINGS, mScanCallback);
- mScanning = true;
+ // BluetoothLeScanner will throw an IllegalStateException if startScan() is called while LE
+ // is not enabled.
+ if (mBtAdapter.isLeEnabled()) {
+ try {
+ mBleScanner.startScan(filters, SCAN_SETTINGS, mScanCallback);
+ mScanning = true;
+ } catch (IllegalStateException e) {
+ Slog.w(TAG, "Exception while starting BLE scanning", e);
+ }
+ } else {
+ Slog.w(TAG, "BLE scanning is not turned on");
+ }
}
private void stopScanIfNeeded() {
@@ -240,11 +258,11 @@ class BleCompanionDeviceScanner implements AssociationStore.OnChangeListener {
if (mBtAdapter.isLeEnabled()) {
try {
mBleScanner.stopScan(mScanCallback);
- } catch (RuntimeException e) {
- // Just to be sure not to crash system server here if BluetoothLeScanner throws
- // another RuntimeException.
+ } catch (IllegalStateException e) {
Slog.w(TAG, "Exception while stopping BLE scanning", e);
}
+ } else {
+ Slog.w(TAG, "BLE scanning is not turned on");
}
mScanning = false;