StopScan before each new Companion device scan
Fixes: 37356792 Test: Call associate many times rapidly with alternaring request value Ensure no stale result is displayed Change-Id: Icaa230d9ad468119e20b3de89f19c36531c2c60f
This commit is contained in:
@@ -75,17 +75,21 @@ public class DeviceDiscoveryService extends Service {
|
||||
|
||||
private BluetoothAdapter mBluetoothAdapter;
|
||||
private WifiManager mWifiManager;
|
||||
private BluetoothLeScanner mBLEScanner;
|
||||
private ScanSettings mDefaultScanSettings = new ScanSettings.Builder().build();
|
||||
|
||||
private List<DeviceFilter<?>> mFilters;
|
||||
private List<BluetoothLEDeviceFilter> mBLEFilters;
|
||||
private List<BluetoothDeviceFilter> mBluetoothFilters;
|
||||
private List<WifiDeviceFilter> mWifiFilters;
|
||||
private List<ScanFilter> mBLEScanFilters;
|
||||
|
||||
AssociationRequest mRequest;
|
||||
List<DeviceFilterPair> mDevicesFound;
|
||||
DeviceFilterPair mSelectedDevice;
|
||||
DevicesAdapter mDevicesAdapter;
|
||||
IFindDeviceCallback mFindCallback;
|
||||
|
||||
ICompanionDeviceDiscoveryServiceCallback mServiceCallback;
|
||||
|
||||
private final ICompanionDeviceDiscoveryService mBinder =
|
||||
@@ -107,65 +111,9 @@ public class DeviceDiscoveryService extends Service {
|
||||
}
|
||||
};
|
||||
|
||||
private final ScanCallback mBLEScanCallback = new ScanCallback() {
|
||||
@Override
|
||||
public void onScanResult(int callbackType, ScanResult result) {
|
||||
if (DEBUG) {
|
||||
Log.i(LOG_TAG,
|
||||
"BLE.onScanResult(callbackType = " + callbackType + ", result = " + result
|
||||
+ ")");
|
||||
}
|
||||
final DeviceFilterPair<ScanResult> deviceFilterPair
|
||||
= DeviceFilterPair.findMatch(result, mBLEFilters);
|
||||
if (deviceFilterPair == null) return;
|
||||
if (callbackType == ScanSettings.CALLBACK_TYPE_MATCH_LOST) {
|
||||
onDeviceLost(deviceFilterPair);
|
||||
} else {
|
||||
onDeviceFound(deviceFilterPair);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
private BluetoothLeScanner mBLEScanner;
|
||||
|
||||
private BroadcastReceiver mBluetoothDeviceFoundBroadcastReceiver = new BroadcastReceiver() {
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
if (DEBUG) {
|
||||
Log.i(LOG_TAG,
|
||||
"BL.onReceive(context = " + context + ", intent = " + intent + ")");
|
||||
}
|
||||
final BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
|
||||
final DeviceFilterPair<BluetoothDevice> deviceFilterPair
|
||||
= DeviceFilterPair.findMatch(device, mBluetoothFilters);
|
||||
if (deviceFilterPair == null) return;
|
||||
if (intent.getAction().equals(BluetoothDevice.ACTION_FOUND)) {
|
||||
onDeviceFound(deviceFilterPair);
|
||||
} else {
|
||||
onDeviceLost(deviceFilterPair);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
private BroadcastReceiver mWifiDeviceFoundBroadcastReceiver = new BroadcastReceiver() {
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
if (intent.getAction().equals(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)) {
|
||||
List<android.net.wifi.ScanResult> scanResults = mWifiManager.getScanResults();
|
||||
|
||||
if (DEBUG) {
|
||||
Log.i(LOG_TAG, "Wifi scan results: " + TextUtils.join("\n", scanResults));
|
||||
}
|
||||
|
||||
for (int i = 0; i < scanResults.size(); i++) {
|
||||
DeviceFilterPair<android.net.wifi.ScanResult> deviceFilterPair =
|
||||
DeviceFilterPair.findMatch(scanResults.get(i), mWifiFilters);
|
||||
if (deviceFilterPair != null) onDeviceFound(deviceFilterPair);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
};
|
||||
private ScanCallback mBLEScanCallback;
|
||||
private BluetoothBroadcastReceiver mBluetoothBroadcastReceiver;
|
||||
private WifiBroadcastReceiver mWifiBroadcastReceiver;
|
||||
|
||||
@Override
|
||||
public IBinder onBind(Intent intent) {
|
||||
@@ -211,16 +159,19 @@ public class DeviceDiscoveryService extends Service {
|
||||
intentFilter.addAction(BluetoothDevice.ACTION_FOUND);
|
||||
intentFilter.addAction(BluetoothDevice.ACTION_DISAPPEARED);
|
||||
|
||||
registerReceiver(mBluetoothDeviceFoundBroadcastReceiver, intentFilter);
|
||||
mBluetoothBroadcastReceiver = new BluetoothBroadcastReceiver();
|
||||
registerReceiver(mBluetoothBroadcastReceiver, intentFilter);
|
||||
mBluetoothAdapter.startDiscovery();
|
||||
}
|
||||
|
||||
if (shouldScan(mBLEFilters)) {
|
||||
mBLEScanCallback = new BLEScanCallback();
|
||||
mBLEScanner.startScan(mBLEScanFilters, mDefaultScanSettings, mBLEScanCallback);
|
||||
}
|
||||
|
||||
if (shouldScan(mWifiFilters)) {
|
||||
registerReceiver(mWifiDeviceFoundBroadcastReceiver,
|
||||
mWifiBroadcastReceiver = new WifiBroadcastReceiver();
|
||||
registerReceiver(mWifiBroadcastReceiver,
|
||||
new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
|
||||
mWifiManager.startScan();
|
||||
}
|
||||
@@ -232,6 +183,7 @@ public class DeviceDiscoveryService extends Service {
|
||||
|
||||
private void reset() {
|
||||
if (DEBUG) Log.i(LOG_TAG, "reset()");
|
||||
stopScan();
|
||||
mDevicesFound.clear();
|
||||
mSelectedDevice = null;
|
||||
mDevicesAdapter.notifyDataSetChanged();
|
||||
@@ -244,20 +196,18 @@ public class DeviceDiscoveryService extends Service {
|
||||
}
|
||||
|
||||
private void stopScan() {
|
||||
if (DEBUG) Log.i(LOG_TAG, "stopScan() called");
|
||||
if (DEBUG) Log.i(LOG_TAG, "stopScan()");
|
||||
|
||||
if (shouldScan(mBluetoothFilters)) {
|
||||
mBluetoothAdapter.cancelDiscovery();
|
||||
unregisterReceiver(mBluetoothDeviceFoundBroadcastReceiver);
|
||||
mBluetoothAdapter.cancelDiscovery();
|
||||
if (mBluetoothBroadcastReceiver != null) {
|
||||
unregisterReceiver(mBluetoothBroadcastReceiver);
|
||||
mBluetoothBroadcastReceiver = null;
|
||||
}
|
||||
if (shouldScan(mBLEFilters)) {
|
||||
mBLEScanner.stopScan(mBLEScanCallback);
|
||||
mBLEScanner.stopScan(mBLEScanCallback);
|
||||
if (mWifiBroadcastReceiver != null) {
|
||||
unregisterReceiver(mWifiBroadcastReceiver);
|
||||
mWifiBroadcastReceiver = null;
|
||||
}
|
||||
if (shouldScan(mWifiFilters)) {
|
||||
unregisterReceiver(mWifiDeviceFoundBroadcastReceiver);
|
||||
}
|
||||
|
||||
stopSelf();
|
||||
}
|
||||
|
||||
private void onDeviceFound(@Nullable DeviceFilterPair device) {
|
||||
@@ -265,8 +215,7 @@ public class DeviceDiscoveryService extends Service {
|
||||
return;
|
||||
}
|
||||
|
||||
if (DEBUG) Log.i(LOG_TAG, "Found device " + device.getDisplayName() + " "
|
||||
+ getDeviceMacAddress(device.device));
|
||||
if (DEBUG) Log.i(LOG_TAG, "Found device " + device);
|
||||
|
||||
if (mDevicesFound.isEmpty()) {
|
||||
onReadyToShowUI();
|
||||
@@ -306,6 +255,7 @@ public class DeviceDiscoveryService extends Service {
|
||||
}
|
||||
|
||||
void onCancel() {
|
||||
if (DEBUG) Log.i(LOG_TAG, "onCancel()");
|
||||
try {
|
||||
mServiceCallback.onDeviceSelectionCancel();
|
||||
} catch (RemoteException e) {
|
||||
@@ -427,5 +377,76 @@ public class DeviceDiscoveryService extends Service {
|
||||
public int hashCode() {
|
||||
return Objects.hash(getDeviceMacAddress(device));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "DeviceFilterPair{" +
|
||||
"device=" + device +
|
||||
", filter=" + filter +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
|
||||
private class BLEScanCallback extends ScanCallback {
|
||||
|
||||
public BLEScanCallback() {
|
||||
if (DEBUG) Log.i(LOG_TAG, "new BLEScanCallback() -> " + this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onScanResult(int callbackType, ScanResult result) {
|
||||
if (DEBUG) {
|
||||
Log.i(LOG_TAG,
|
||||
"BLE.onScanResult(callbackType = " + callbackType + ", result = " + result
|
||||
+ ")");
|
||||
}
|
||||
final DeviceFilterPair<ScanResult> deviceFilterPair
|
||||
= DeviceFilterPair.findMatch(result, mBLEFilters);
|
||||
if (deviceFilterPair == null) return;
|
||||
if (callbackType == ScanSettings.CALLBACK_TYPE_MATCH_LOST) {
|
||||
onDeviceLost(deviceFilterPair);
|
||||
} else {
|
||||
onDeviceFound(deviceFilterPair);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private class BluetoothBroadcastReceiver extends BroadcastReceiver {
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
if (DEBUG) {
|
||||
Log.i(LOG_TAG,
|
||||
"BL.onReceive(context = " + context + ", intent = " + intent + ")");
|
||||
}
|
||||
final BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
|
||||
final DeviceFilterPair<BluetoothDevice> deviceFilterPair
|
||||
= DeviceFilterPair.findMatch(device, mBluetoothFilters);
|
||||
if (deviceFilterPair == null) return;
|
||||
if (intent.getAction().equals(BluetoothDevice.ACTION_FOUND)) {
|
||||
onDeviceFound(deviceFilterPair);
|
||||
} else {
|
||||
onDeviceLost(deviceFilterPair);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private class WifiBroadcastReceiver extends BroadcastReceiver {
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
if (intent.getAction().equals(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)) {
|
||||
List<android.net.wifi.ScanResult> scanResults = mWifiManager.getScanResults();
|
||||
|
||||
if (DEBUG) {
|
||||
Log.i(LOG_TAG, "Wifi scan results: " + TextUtils.join("\n", scanResults));
|
||||
}
|
||||
|
||||
for (int i = 0; i < scanResults.size(); i++) {
|
||||
DeviceFilterPair<android.net.wifi.ScanResult> deviceFilterPair =
|
||||
DeviceFilterPair.findMatch(scanResults.get(i), mWifiFilters);
|
||||
if (deviceFilterPair != null) onDeviceFound(deviceFilterPair);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user