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

Commit a31d9df6 authored by weichinweng's avatar weichinweng
Browse files

Delay sends BONDED intent for the service discovery finish

* Add 3 sec delay waiting for service discovery.
* Add test case into BondStateMachineTest

Tag: #feature
Bug: 220970000
Test: atest BluetoothInstrumentationTests
Change-Id: I05453f573003cd745b56e368475a66feb17e818b
parent 3d794a25
Loading
Loading
Loading
Loading
+44 −8
Original line number Diff line number Diff line
@@ -69,10 +69,13 @@ final class BondStateMachine extends StateMachine {
    static final int SSP_REQUEST = 5;
    static final int PIN_REQUEST = 6;
    static final int UUID_UPDATE = 10;
    static final int BONDED_INTENT_DELAY = 11;
    static final int BOND_STATE_NONE = 0;
    static final int BOND_STATE_BONDING = 1;
    static final int BOND_STATE_BONDED = 2;

    static int sPendingUuidUpdateTimeoutMillis = 3000; // 3s

    private AdapterService mAdapterService;
    private AdapterProperties mAdapterProperties;
    private RemoteDevices mRemoteDevices;
@@ -152,15 +155,20 @@ final class BondStateMachine extends StateMachine {
                        transitionTo(mPendingCommandState);
                    } else if (newState == BluetoothDevice.BOND_NONE) {
                    /* if the link key was deleted by the stack */
                        sendIntent(dev, newState, 0);
                        sendIntent(dev, newState, 0, false);
                    } else {
                        Log.e(TAG, "In stable state, received invalid newState: "
                                + state2str(newState));
                    }
                    break;
                case BONDED_INTENT_DELAY:
                    if (mPendingBondedDevices.contains(dev)) {
                        sendIntent(dev, BluetoothDevice.BOND_BONDED, 0, true);
                    }
                    break;
                case UUID_UPDATE:
                    if (mPendingBondedDevices.contains(dev)) {
                        sendIntent(dev, BluetoothDevice.BOND_BONDED, 0);
                        sendIntent(dev, BluetoothDevice.BOND_BONDED, 0, false);
                    }
                    break;
                case CANCEL_BOND:
@@ -216,7 +224,7 @@ final class BondStateMachine extends StateMachine {
                            && reason == BluetoothDevice.BOND_SUCCESS) {
                        reason = BluetoothDevice.UNBOND_REASON_REMOVED;
                    }
                    sendIntent(dev, newState, reason);
                    sendIntent(dev, newState, reason, false);
                    if (newState != BluetoothDevice.BOND_BONDING) {
                        // This is either none/bonded, remove and transition, and also set
                        // result=false to avoid adding the device to mDevices.
@@ -341,7 +349,8 @@ final class BondStateMachine extends StateMachine {
                        BluetoothDevice.BOND_NONE, BluetoothProtoEnums.BOND_SUB_STATE_UNKNOWN,
                        BluetoothDevice.UNBOND_REASON_REPEATED_ATTEMPTS);
                // Using UNBOND_REASON_REMOVED for legacy reason
                sendIntent(dev, BluetoothDevice.BOND_NONE, BluetoothDevice.UNBOND_REASON_REMOVED);
                sendIntent(dev, BluetoothDevice.BOND_NONE, BluetoothDevice.UNBOND_REASON_REMOVED,
                        false);
                return false;
            } else if (transition) {
                transitionTo(mPendingCommandState);
@@ -368,7 +377,8 @@ final class BondStateMachine extends StateMachine {
    }

    @VisibleForTesting
    void sendIntent(BluetoothDevice device, int newState, int reason) {
    void sendIntent(BluetoothDevice device, int newState, int reason,
            boolean isTriggerFromDelayMessage) {
        DeviceProperties devProp = mRemoteDevices.getDeviceProperties(device);
        int oldState = BluetoothDevice.BOND_NONE;
        if (newState != BluetoothDevice.BOND_NONE
@@ -380,6 +390,14 @@ final class BondStateMachine extends StateMachine {
        if (devProp != null) {
            oldState = devProp.getBondState();
        }
        if (isTriggerFromDelayMessage && (oldState != BluetoothDevice.BOND_BONDED
                || newState != BluetoothDevice.BOND_BONDED
                || !mPendingBondedDevices.contains(device))) {
            infoLog("Invalid state when doing delay send bonded intent, oldState: " + oldState
                    + ", newState: " + newState + ", in PendingBondedDevices list? "
                    + mPendingBondedDevices.contains(device));
            return;
        }
        if (mPendingBondedDevices.contains(device)) {
            mPendingBondedDevices.remove(device);
            if (oldState == BluetoothDevice.BOND_BONDED) {
@@ -406,12 +424,14 @@ final class BondStateMachine extends StateMachine {
                mAdapterService.getMetricId(device));
        mAdapterProperties.onBondStateChanged(device, newState);

        if (devProp != null && ((devProp.getDeviceType() == BluetoothDevice.DEVICE_TYPE_CLASSIC
                || devProp.getDeviceType() == BluetoothDevice.DEVICE_TYPE_DUAL)
                && newState == BluetoothDevice.BOND_BONDED && devProp.getUuids() == null)) {
        if (!isTriggerFromDelayMessage && newState == BluetoothDevice.BOND_BONDED
                && devProp != null && devProp.getUuids() == null) {
            infoLog(device + " is bonded, wait for SDP complete to broadcast bonded intent");
            if (!mPendingBondedDevices.contains(device)) {
                mPendingBondedDevices.add(device);
                Message msg = obtainMessage(BONDED_INTENT_DELAY);
                msg.obj = device;
                sendMessageDelayed(msg, sPendingUuidUpdateTimeoutMillis);
            }
            if (oldState == BluetoothDevice.BOND_NONE) {
                // Broadcast NONE->BONDING for NONE->BONDED case.
@@ -542,6 +562,22 @@ final class BondStateMachine extends StateMachine {
        sendMessage(msg);
    }

    /*
     * Check whether has the specific message in message queue
     */
    @VisibleForTesting
    public boolean hasMessage(int what) {
        return hasMessages(what);
    }

    /*
     * Remove the specific message from message queue
     */
    @VisibleForTesting
    public void removeMessage(int what) {
        removeMessages(what);
    }

    @RequiresPermission(allOf = {
            android.Manifest.permission.BLUETOOTH_PRIVILEGED,
            android.Manifest.permission.MODIFY_PHONE_STATE,
+240 −135

File changed.

Preview size limit exceeded, changes collapsed.