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

Commit 08e9b191 authored by Himanshu Rawat's avatar Himanshu Rawat
Browse files

Remove device properties after BOND_NONE is processed

aosp/2974117 and aosp/3038273 introduced a race condition in processing
of the BOND_NONE state change. These changes ensured that the device
mapping and device properties were removed when the bond was removed.
However, BondStateMachine relies on the device properties to decide if
the bond state change should be broadcasted. If the device properties
are removed before the BondStateMahcine handles the BONDING_STATE_CHANGE
message for the BOND_NONE state, the state change is not broadcasted.

Test: mmm packages/modules/Bluetooth
Test: Manual | Unpair a device
Flag: com.android.bluetooth.flags.remove_bond_with_address_map
Bug: 335354378
Bug: 332626602
Bug: 326294532
Bug: 335465028
Change-Id: I1e23f5d6250d9fd478262742b87ae02ab517392b
parent ab06891d
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -489,6 +489,9 @@ final class BondStateMachine extends StateMachine {
            infoLog("Invalid bond state " + newState);
            return;
        }

        mRemoteDevices.onBondStateChange(device, newState);

        if (devProp != null) {
            oldState = devProp.getBondState();
        }
+0 −5
Original line number Diff line number Diff line
@@ -19,8 +19,6 @@ package com.android.bluetooth.btservice;
import android.bluetooth.OobData;
import android.bluetooth.UidTraffic;

import com.android.bluetooth.flags.Flags;

class JniCallbacks {

    private RemoteDevices mRemoteDevices;
@@ -68,9 +66,6 @@ class JniCallbacks {

    void bondStateChangeCallback(int status, byte[] address, int newState, int hciReason) {
        mBondStateMachine.bondStateChangeCallback(status, address, newState, hciReason);
        if (Flags.removeBondWithAddressMap()) {
            mRemoteDevices.onBondStateChange(address, newState);
        }
    }

    void addressConsolidateCallback(byte[] mainAddress, byte[] secondaryAddress) {
+9 −10
Original line number Diff line number Diff line
@@ -1140,7 +1140,7 @@ public class RemoteDevices {
                mAdapterService.sendBroadcast(
                        intent, BLUETOOTH_CONNECT, Utils.getTempBroadcastOptions().toBundle());
            } else if (device.getBondState() == BluetoothDevice.BOND_NONE) {
                removeAddressMapping(address);
                removeAddressMapping(Utils.getAddressStringFromByte(address));
            }
            if (state == BluetoothAdapter.STATE_ON || state == BluetoothAdapter.STATE_TURNING_OFF) {
                mAdapterService.notifyAclDisconnected(device, transportLinkType);
@@ -1223,21 +1223,20 @@ public class RemoteDevices {
        }
    }

    private void removeAddressMapping(byte[] address) {
        String key = Utils.getAddressStringFromByte(address);
    private void removeAddressMapping(String address) {
        synchronized (mDevices) {
            mDevices.remove(key);
            mDeviceQueue.remove(key); // Remove from LRU cache
            mDevices.remove(address);
            mDeviceQueue.remove(address); // Remove from LRU cache

            // Remove from dual mode device mappings
            mDualDevicesMap.values().remove(key);
            mDualDevicesMap.remove(key);
            mDualDevicesMap.values().remove(address);
            mDualDevicesMap.remove(address);
        }
    }

    void onBondStateChange(byte[] address, int newState) {
        if (newState == BluetoothDevice.BOND_NONE) {
            removeAddressMapping(address);
    void onBondStateChange(BluetoothDevice device, int newState) {
        if (Flags.removeAddressMapOnUnbond() && newState == BluetoothDevice.BOND_NONE) {
            removeAddressMapping(device.getAddress());
        }
    }