Merge "Catch the exception when ble startScan" into tm-qpr-dev
This commit is contained in:
@@ -184,13 +184,21 @@ class BleCompanionDeviceScanner implements AssociationStore.OnChangeListener {
|
|||||||
@MainThread
|
@MainThread
|
||||||
private void startScan() {
|
private void startScan() {
|
||||||
enforceInitialized();
|
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()");
|
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.
|
// Collect MAC addresses from all associations.
|
||||||
final Set<String> macAddresses = new HashSet<>();
|
final Set<String> macAddresses = new HashSet<>();
|
||||||
for (AssociationInfo association : mAssociationStore.getAssociations()) {
|
for (AssociationInfo association : mAssociationStore.getAssociations()) {
|
||||||
@@ -221,8 +229,18 @@ class BleCompanionDeviceScanner implements AssociationStore.OnChangeListener {
|
|||||||
filters.add(filter);
|
filters.add(filter);
|
||||||
}
|
}
|
||||||
|
|
||||||
mBleScanner.startScan(filters, SCAN_SETTINGS, mScanCallback);
|
// BluetoothLeScanner will throw an IllegalStateException if startScan() is called while LE
|
||||||
mScanning = true;
|
// 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() {
|
private void stopScanIfNeeded() {
|
||||||
@@ -240,11 +258,11 @@ class BleCompanionDeviceScanner implements AssociationStore.OnChangeListener {
|
|||||||
if (mBtAdapter.isLeEnabled()) {
|
if (mBtAdapter.isLeEnabled()) {
|
||||||
try {
|
try {
|
||||||
mBleScanner.stopScan(mScanCallback);
|
mBleScanner.stopScan(mScanCallback);
|
||||||
} catch (RuntimeException e) {
|
} catch (IllegalStateException e) {
|
||||||
// Just to be sure not to crash system server here if BluetoothLeScanner throws
|
|
||||||
// another RuntimeException.
|
|
||||||
Slog.w(TAG, "Exception while stopping BLE scanning", e);
|
Slog.w(TAG, "Exception while stopping BLE scanning", e);
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
Slog.w(TAG, "BLE scanning is not turned on");
|
||||||
}
|
}
|
||||||
|
|
||||||
mScanning = false;
|
mScanning = false;
|
||||||
|
|||||||
Reference in New Issue
Block a user