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

Commit a101dd17 authored by Evan Chen's avatar Evan Chen Committed by Android (Google) Code Review
Browse files

Merge "Stop the BLE scan only if all the devices are connected" into main

parents 33525392 ff2cd5ab
Loading
Loading
Loading
Loading
+15 −11
Original line number Diff line number Diff line
@@ -64,6 +64,8 @@ class BleDeviceProcessor implements AssociationStore.OnChangeListener {
        void onBleCompanionDeviceLost(int associationId, int userId);
    }

    @NonNull
    private final DevicePresenceProcessor mDevicePresenceProcessor;
    @NonNull
    private final AssociationStore mAssociationStore;
    @NonNull
@@ -78,8 +80,10 @@ class BleDeviceProcessor implements AssociationStore.OnChangeListener {
    // Only accessed from the Main thread.
    private boolean mScanning = false;

    BleDeviceProcessor(@NonNull AssociationStore associationStore, @NonNull Callback callback) {
    BleDeviceProcessor(@NonNull AssociationStore associationStore,
            @NonNull DevicePresenceProcessor devicePresenceProcessor, @NonNull Callback callback) {
        mAssociationStore = associationStore;
        mDevicePresenceProcessor = devicePresenceProcessor;
        mCallback = callback;
    }

@@ -160,23 +164,23 @@ class BleDeviceProcessor implements AssociationStore.OnChangeListener {
        }

        // Collect MAC addresses from all associations.
        final Set<String> macAddresses = new HashSet<>();
        final Set<String> macAddressesToScan = new HashSet<>();
        for (AssociationInfo association : mAssociationStore.getActiveAssociations()) {
            if (!association.isNotifyOnDeviceNearby()) continue;

            // Beware that BT stack does not consider low-case MAC addresses valid, while
            // MacAddress.toString() return a low-case String.
            final String macAddress = association.getDeviceMacAddressAsString();
            if (macAddress != null) {
                macAddresses.add(macAddress);
            if (association.isNotifyOnDeviceNearby()
                    && !mDevicePresenceProcessor.isBtConnected(association.getId())
                    && macAddress != null) {
                macAddressesToScan.add(macAddress);
                Slog.i(TAG, "Adding " + macAddress + " to BLE scan filters.");
            }
        }
        if (macAddresses.isEmpty()) {
        if (macAddressesToScan.isEmpty()) {
            Slog.i(TAG, "No devices require BLE scanning. BLE scan remains stopped.");
            return;
        }

        final List<ScanFilter> filters = new ArrayList<>(macAddresses.size());
        for (String macAddress : macAddresses) {
        final List<ScanFilter> filters = new ArrayList<>(macAddressesToScan.size());
        for (String macAddress : macAddressesToScan) {
            final ScanFilter filter = new ScanFilter.Builder()
                    .setDeviceAddress(macAddress)
                    .build();
+5 −4
Original line number Diff line number Diff line
@@ -165,7 +165,7 @@ public class DevicePresenceProcessor implements AssociationStore.OnChangeListene
        mUserManager = userManager;
        mBluetoothDeviceProcessor = new BluetoothDeviceProcessor(associationStore,
                mObservableUuidStore, this);
        mBleDeviceProcessor = new BleDeviceProcessor(associationStore, this);
        mBleDeviceProcessor = new BleDeviceProcessor(associationStore, this, this);
        mPowerManagerInternal = powerManagerInternal;
        mCompanionExemptionProcessor = companionExemptionProcessor;
    }
@@ -1109,17 +1109,18 @@ public class DevicePresenceProcessor implements AssociationStore.OnChangeListene

    /**
     * The BLE scan can be only stopped if all the devices have been reported
     * BT connected and BLE presence and are not pending to report BLE lost.
     * BT connected and are not pending to report BLE lost.
     */
    private boolean canStopBleScan() {
        for (AssociationInfo ai : mAssociationStore.getActiveAssociations()) {
            int id = ai.getId();
            synchronized (mBtDisconnectedDevices) {
                if (ai.isNotifyOnDeviceNearby() && !(isBtConnected(id)
                        && isBlePresent(id) && mBtDisconnectedDevices.isEmpty())) {
                        && mBtDisconnectedDevices.isEmpty())) {

                    Slog.i(TAG, "The BLE scan cannot be stopped, "
                            + "device( " + id + " ) is not yet connected "
                            + "OR the BLE is not current present Or is pending to report BLE lost");
                            + "Or it is pending to report BLE lost");
                    return false;
                }
            }