OSDN Git Service

StopScan before each new Companion device scan
authorEugene Susla <eugenesusla@google.com>
Tue, 18 Apr 2017 02:13:31 +0000 (19:13 -0700)
committerEugene Susla <eugenesusla@google.com>
Wed, 19 Apr 2017 18:20:06 +0000 (11:20 -0700)
Fixes: 37356792
Test: Call associate many times rapidly with alternaring request value
Ensure no stale result is displayed
Change-Id: Icaa230d9ad468119e20b3de89f19c36531c2c60f

core/java/android/companion/AssociationRequest.java
core/java/android/companion/BluetoothLEDeviceFilter.java
packages/CompanionDeviceManager/src/com/android/companiondevicemanager/DeviceDiscoveryService.java

index 919f4ba..922224a 100644 (file)
@@ -84,6 +84,14 @@ public final class AssociationRequest implements Parcelable {
     }
 
     @Override
+    public String toString() {
+        return "AssociationRequest{" +
+                "mSingleDevice=" + mSingleDevice +
+                ", mDeviceFilters=" + mDeviceFilters +
+                '}';
+    }
+
+    @Override
     public void writeToParcel(Parcel dest, int flags) {
         dest.writeByte((byte) (mSingleDevice ? 1 : 0));
         dest.writeParcelableList(mDeviceFilters, flags);
index 76051d7..e5ea4e9 100644 (file)
@@ -225,6 +225,23 @@ public final class BluetoothLEDeviceFilter implements DeviceFilter<ScanResult> {
         return 0;
     }
 
+    @Override
+    public String toString() {
+        return "BluetoothLEDeviceFilter{" +
+                "mNamePattern=" + mNamePattern +
+                ", mScanFilter=" + mScanFilter +
+                ", mRawDataFilter=" + Arrays.toString(mRawDataFilter) +
+                ", mRawDataFilterMask=" + Arrays.toString(mRawDataFilterMask) +
+                ", mRenamePrefix='" + mRenamePrefix + '\'' +
+                ", mRenameSuffix='" + mRenameSuffix + '\'' +
+                ", mRenameBytesFrom=" + mRenameBytesFrom +
+                ", mRenameBytesTo=" + mRenameBytesTo +
+                ", mRenameNameFrom=" + mRenameNameFrom +
+                ", mRenameNameTo=" + mRenameNameTo +
+                ", mRenameBytesReverseOrder=" + mRenameBytesReverseOrder +
+                '}';
+    }
+
     public static final Creator<BluetoothLEDeviceFilter> CREATOR
             = new Creator<BluetoothLEDeviceFilter>() {
         @Override
index 246bd2b..90ed224 100644 (file)
@@ -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);
+                }
+            }
+
+        }
     }
 }