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

Commit 77723b5b authored by Łukasz Rymanowski's avatar Łukasz Rymanowski
Browse files

LeAudioService: Fix bonding with lead device

This patch adds some additional logs around lead device handling.
This patch also make sure to disconnect lead device if the disconnection
was due to unbonding process

Bug: 242645864
Test: atest BluetoothInstrumentationTests
Tag: #feature
Change-Id: If860e8aeea608fc78d33e89b0e2b544bdf7a84d2
parent d3ceab99
Loading
Loading
Loading
Loading
+33 −12
Original line number Diff line number Diff line
@@ -1112,20 +1112,24 @@ public class LeAudioService extends ProfileService {
    }

    private void clearLostDevicesWhileStreaming(LeAudioGroupDescriptor descriptor) {
        synchronized (mGroupLock) {
            if (DBG) {
            Log.d(TAG, " lost dev: " + descriptor.mLostLeadDeviceWhileStreaming);
                Log.d(TAG, "Clearing lost dev: " + descriptor.mLostLeadDeviceWhileStreaming);
            }

        LeAudioStateMachine sm = mStateMachines.get(descriptor.mLostLeadDeviceWhileStreaming);
            LeAudioStateMachine sm =
                    mStateMachines.get(descriptor.mLostLeadDeviceWhileStreaming);
            if (sm != null) {
                LeAudioStackEvent stackEvent =
                new LeAudioStackEvent(LeAudioStackEvent.EVENT_TYPE_CONNECTION_STATE_CHANGED);
                        new LeAudioStackEvent(
                                LeAudioStackEvent.EVENT_TYPE_CONNECTION_STATE_CHANGED);
                stackEvent.device = descriptor.mLostLeadDeviceWhileStreaming;
                stackEvent.valueInt1 = LeAudioStackEvent.CONNECTION_STATE_DISCONNECTED;
                sm.sendMessage(LeAudioStateMachine.STACK_EVENT, stackEvent);
            }
            descriptor.mLostLeadDeviceWhileStreaming = null;
        }
    }

    // Suppressed since this is part of a local process
    @SuppressLint("AndroidFrameworkRequiresPermission")
@@ -1153,10 +1157,14 @@ public class LeAudioService extends ProfileService {
                        switch (stackEvent.valueInt1) {
                            case LeAudioStackEvent.CONNECTION_STATE_DISCONNECTING:
                            case LeAudioStackEvent.CONNECTION_STATE_DISCONNECTED:
                                boolean disconnectDueToUnbond =
                                        (BluetoothDevice.BOND_NONE
                                                == mAdapterService.getBondState(device));
                                if (descriptor != null && (Objects.equals(device,
                                        mActiveAudioOutDevice)
                                        || Objects.equals(device, mActiveAudioInDevice))
                                        && (getConnectedPeerDevices(groupId).size() > 1)) {
                                        && (getConnectedPeerDevices(groupId).size() > 1)
                                        && !disconnectDueToUnbond) {

                                    if (DBG) Log.d(TAG, "Adding to lost devices : " + device);
                                    descriptor.mLostLeadDeviceWhileStreaming = device;
@@ -1501,6 +1509,8 @@ public class LeAudioService extends ProfileService {
                return;
            }
            if (sm.getConnectionState() != BluetoothProfile.STATE_DISCONNECTED) {
                Log.w(TAG, "Device is not disconnected yet.");
                disconnect(device);
                return;
            }
            removeStateMachine(device);
@@ -1839,6 +1849,10 @@ public class LeAudioService extends ProfileService {

    private void handleGroupNodeAdded(BluetoothDevice device, int groupId) {
        synchronized (mGroupLock) {
            if (DBG) {
                Log.d(TAG, "Device " + device + " added to group " + groupId);
            }

            mDeviceGroupIdMap.put(device, groupId);
            LeAudioGroupDescriptor descriptor = mGroupDescriptors.get(groupId);
            if (descriptor == null) {
@@ -1863,7 +1877,14 @@ public class LeAudioService extends ProfileService {
    }

    private void handleGroupNodeRemoved(BluetoothDevice device, int groupId) {
        if (DBG) {
            Log.d(TAG, "Removing device " + device + " grom group " + groupId);
        }

        LeAudioGroupDescriptor descriptor = getGroupDescriptor(groupId);
        if (DBG) {
            Log.d(TAG, "Lost lead device is " + descriptor.mLostLeadDeviceWhileStreaming);
        }
        if (Objects.equals(device, descriptor.mLostLeadDeviceWhileStreaming)) {
            clearLostDevicesWhileStreaming(descriptor);
        }
+1 −0
Original line number Diff line number Diff line
@@ -1322,6 +1322,7 @@ public class LeAudioServiceTest {
        verify(mAudioManager, times(1)).handleBluetoothActiveDeviceChanged(eq(leadDevice), any(),
                        any(BluetoothProfileConnectionInfo.class));

        doReturn(BluetoothDevice.BOND_BONDED).when(mAdapterService).getBondState(leadDevice);
        verifyActiveDeviceStateIntent(TIMEOUT_MS, leadDevice);
        injectNoVerifyDeviceDisconnected(leadDevice);