diff options
author | 2024-04-16 21:25:52 +0000 | |
---|---|---|
committer | 2024-04-17 16:34:45 +0000 | |
commit | bfc69cd32ef3a539ff399562f7592bb10d1cfc0c (patch) | |
tree | 219b68dc8d37fbf17aa61577ea4cb765e576c3b4 /packages/CompanionDeviceManager/src | |
parent | b83de0bcf7f9aa4544dc3a2408e7cc642244585d (diff) |
Do not need to startDiscovery if a bound device need to be assoicated.
Do not need to startDiscovery if a bound device need to be assoicated
for singleDevice dialog.
Test: cts
Bug: 331747809
Change-Id: I0cbb6415a057336c302f35e90918cfbb72b16536
Diffstat (limited to 'packages/CompanionDeviceManager/src')
-rw-r--r-- | packages/CompanionDeviceManager/src/com/android/companiondevicemanager/CompanionDeviceDiscoveryService.java | 29 |
1 files changed, 19 insertions, 10 deletions
diff --git a/packages/CompanionDeviceManager/src/com/android/companiondevicemanager/CompanionDeviceDiscoveryService.java b/packages/CompanionDeviceManager/src/com/android/companiondevicemanager/CompanionDeviceDiscoveryService.java index 65bbb6fc8821..a0ebbfefcef1 100644 --- a/packages/CompanionDeviceManager/src/com/android/companiondevicemanager/CompanionDeviceDiscoveryService.java +++ b/packages/CompanionDeviceManager/src/com/android/companiondevicemanager/CompanionDeviceDiscoveryService.java @@ -197,7 +197,12 @@ public class CompanionDeviceDiscoveryService extends Service { filter(allFilters, BluetoothLeDeviceFilter.class); final List<WifiDeviceFilter> wifiFilters = filter(allFilters, WifiDeviceFilter.class); - checkBoundDevicesIfNeeded(request, btFilters); + // No need to startDiscovery if the device is already bound or connected for + // singleDevice dialog. + if (checkBoundDevicesIfNeeded(request, btFilters)) { + stopSelf(); + return; + } // If no filters are specified: look for everything. final boolean forceStartScanningAll = isEmpty(allFilters); @@ -257,33 +262,37 @@ public class CompanionDeviceDiscoveryService extends Service { stopSelf(); } - private void checkBoundDevicesIfNeeded(@NonNull AssociationRequest request, + private boolean checkBoundDevicesIfNeeded(@NonNull AssociationRequest request, @NonNull List<BluetoothDeviceFilter> btFilters) { // If filtering to get single device by mac address, also search in the set of already // bonded devices to allow linking those directly - if (btFilters.isEmpty() || !request.isSingleDevice()) return; + if (btFilters.isEmpty() || !request.isSingleDevice()) return false; final BluetoothDeviceFilter singleMacAddressFilter = find(btFilters, filter -> !TextUtils.isEmpty(filter.getAddress())); - if (singleMacAddressFilter == null) return; + if (singleMacAddressFilter == null) return false; - findAndReportMatches(mBtAdapter.getBondedDevices(), btFilters); - findAndReportMatches(mBtManager.getConnectedDevices(BluetoothProfile.GATT), btFilters); - findAndReportMatches( - mBtManager.getConnectedDevices(BluetoothProfile.GATT_SERVER), btFilters); + return findAndReportMatches(mBtAdapter.getBondedDevices(), btFilters) + || findAndReportMatches(mBtManager.getConnectedDevices( + BluetoothProfile.GATT), btFilters) + || findAndReportMatches(mBtManager.getConnectedDevices( + BluetoothProfile.GATT_SERVER), btFilters); } - private void findAndReportMatches(@Nullable Collection<BluetoothDevice> devices, + private boolean findAndReportMatches(@Nullable Collection<BluetoothDevice> devices, @NonNull List<BluetoothDeviceFilter> filters) { - if (devices == null) return; + if (devices == null) return false; for (BluetoothDevice device : devices) { final DeviceFilterPair<BluetoothDevice> match = findMatch(device, filters); if (match != null) { onDeviceFound(match); + return true; } } + + return false; } private BluetoothBroadcastReceiver startBtScanningIfNeeded( |