Fix duplicate devices when multiple mediums scanning requested

The dedupuing logic was already in place, but there was a race
due to managin the list of devices from different threads.

Test: using wear app ensure dup device reproduses without CL, and not with it
Fixes: 160870456
Change-Id: I1526199e8e4fb4b8f7d7f306e9e676359cdca516
This commit is contained in:
Eugene Susla
2020-07-30 14:39:50 -07:00
parent afab5784ea
commit b8b53f3b39
2 changed files with 21 additions and 10 deletions

View File

@@ -141,6 +141,16 @@ public final class BluetoothDeviceFilter implements DeviceFilter<BluetoothDevice
return Objects.hash(mNamePattern, mAddress, mServiceUuids, mServiceUuidMasks); return Objects.hash(mNamePattern, mAddress, mServiceUuids, mServiceUuidMasks);
} }
@Override
public String toString() {
return "BluetoothDeviceFilter{"
+ "mNamePattern=" + mNamePattern
+ ", mAddress='" + mAddress + '\''
+ ", mServiceUuids=" + mServiceUuids
+ ", mServiceUuidMasks=" + mServiceUuidMasks
+ '}';
}
@Override @Override
public int describeContents() { public int describeContents() {
return 0; return 0;

View File

@@ -259,18 +259,19 @@ public class DeviceDiscoveryService extends Service {
private void onDeviceFound(@Nullable DeviceFilterPair device) { private void onDeviceFound(@Nullable DeviceFilterPair device) {
if (device == null) return; if (device == null) return;
if (mDevicesFound.contains(device)) {
return;
}
if (DEBUG) Log.i(LOG_TAG, "Found device " + device);
Handler.getMain().sendMessage(obtainMessage( Handler.getMain().sendMessage(obtainMessage(
DeviceDiscoveryService::onDeviceFoundMainThread, this, device)); DeviceDiscoveryService::onDeviceFoundMainThread, this, device));
} }
@MainThread @MainThread
void onDeviceFoundMainThread(@NonNull DeviceFilterPair device) { void onDeviceFoundMainThread(@NonNull DeviceFilterPair device) {
if (mDevicesFound.contains(device)) {
Log.i(LOG_TAG, "Skipping device " + device + " - already among found devices");
return;
}
Log.i(LOG_TAG, "Found device " + device);
if (mDevicesFound.isEmpty()) { if (mDevicesFound.isEmpty()) {
onReadyToShowUI(); onReadyToShowUI();
} }
@@ -432,10 +433,10 @@ public class DeviceDiscoveryService extends Service {
@Override @Override
public String toString() { public String toString() {
return "DeviceFilterPair{" + return "DeviceFilterPair{"
"device=" + device + + "device=" + device + " " + getDisplayName()
", filter=" + filter + + ", filter=" + filter
'}'; + '}';
} }
} }