Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 61179707 authored by Eugene Susla's avatar Eugene Susla Committed by Android (Google) Code Review
Browse files

Merge "StopScan before each new Companion device scan" into oc-dev

parents f4af35a5 a7717e30
Loading
Loading
Loading
Loading
+8 −0
Original line number Diff line number Diff line
@@ -83,6 +83,14 @@ public final class AssociationRequest implements Parcelable {
        return Objects.hash(mSingleDevice, mDeviceFilters);
    }

    @Override
    public String toString() {
        return "AssociationRequest{" +
                "mSingleDevice=" + mSingleDevice +
                ", mDeviceFilters=" + mDeviceFilters +
                '}';
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeByte((byte) (mSingleDevice ? 1 : 0));
+17 −0
Original line number Diff line number Diff line
@@ -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
+95 −74
Original line number Diff line number Diff line
@@ -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);
        if (mBluetoothBroadcastReceiver != null) {
            unregisterReceiver(mBluetoothBroadcastReceiver);
            mBluetoothBroadcastReceiver = null;
        }
        if (shouldScan(mBLEFilters)) {
        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) {
@@ -430,5 +380,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);
                }
            }

        }
    }
}