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

Commit 9a25064a authored by Jakub Tyszkowski's avatar Jakub Tyszkowski
Browse files

LeAudio: Fix reconnecting LeAudio when profile is disabled

When BT is enabled we should set the BT device autoconnect
feature according to the profile connection policy to avoid the
unnecessary and endless LE link establishment and disconnection cycle.

Bug: 369784445
Test: atest GoogleBluetoothInstrumentationTests
Flag: Exempt; Bug fix covered with unit test
Change-Id: I069227b70c6d66978a7bb1a32968fd47989ac3a7
parent 472ac7e7
Loading
Loading
Loading
Loading
+4 −1
Original line number Diff line number Diff line
@@ -4671,9 +4671,12 @@ public class LeAudioService extends ProfileService {
                return;
            }
            for (BluetoothDevice device : mDeviceDescriptors.keySet()) {
                if (getConnectionPolicy(device) != BluetoothProfile.CONNECTION_POLICY_FORBIDDEN) {
                int connection_policy = getConnectionPolicy(device);
                if (connection_policy != BluetoothProfile.CONNECTION_POLICY_FORBIDDEN) {
                    setAuthorizationForRelatedProfiles(device, true);
                }
                setEnabledState(
                        device, connection_policy != BluetoothProfile.CONNECTION_POLICY_FORBIDDEN);
            }
        } finally {
            mGroupReadLock.unlock();
+26 −0
Original line number Diff line number Diff line
@@ -72,8 +72,10 @@ import com.android.bluetooth.btservice.ServiceFactory;
import com.android.bluetooth.btservice.storage.DatabaseManager;
import com.android.bluetooth.csip.CsipSetCoordinatorService;
import com.android.bluetooth.flags.Flags;
import com.android.bluetooth.gatt.GattService;
import com.android.bluetooth.hap.HapClientService;
import com.android.bluetooth.hfp.HeadsetService;
import com.android.bluetooth.le_scan.TransitionalScanHelper;
import com.android.bluetooth.mcp.McpService;
import com.android.bluetooth.tbs.TbsService;
import com.android.bluetooth.vc.VolumeControlService;
@@ -133,6 +135,8 @@ public class LeAudioServiceTest {
    @Rule public MockitoRule mockitoRule = MockitoJUnit.rule();

    @Mock private AdapterService mAdapterService;
    @Mock private GattService mGattService;
    @Mock private TransitionalScanHelper mTransitionalScanHelper;
    @Mock private ActiveDeviceManager mActiveDeviceManager;
    @Mock private AudioManager mAudioManager;
    @Mock private DatabaseManager mDatabaseManager;
@@ -220,6 +224,8 @@ public class LeAudioServiceTest {
        doAnswer(invocation -> mBondedDevices.toArray(new BluetoothDevice[] {}))
                .when(mAdapterService)
                .getBondedDevices();
        doReturn(mGattService).when(mAdapterService).getBluetoothGattService();
        doReturn(mTransitionalScanHelper).when(mGattService).getTransitionalScanHelper();

        LeAudioBroadcasterNativeInterface.setInstance(mLeAudioBroadcasterNativeInterface);
        LeAudioNativeInterface.setInstance(mNativeInterface);
@@ -386,6 +392,26 @@ public class LeAudioServiceTest {
        assertThat(mService).isEqualTo(LeAudioService.getLeAudioService());
    }

    /** Test enabling disabling device autoconnections when connection policy is set */
    @Test
    public void testEnableDisableProfile() {
        // Make sure the device is known to the service and is not forbidden to connect
        mService.createDeviceDescriptor(mSingleDevice, true);
        when(mDatabaseManager.getProfileConnectionPolicy(mSingleDevice, BluetoothProfile.LE_AUDIO))
                .thenReturn(BluetoothProfile.CONNECTION_POLICY_UNKNOWN);

        // Verify the device is enabled in the service when policy is not FORBIDDEN during BT Enable
        mService.handleBluetoothEnabled();
        verify(mNativeInterface).setEnableState(eq(mSingleDevice), eq(true));

        // Verify the device is disabled in the service when policy is set to FORBIDDEN
        when(mDatabaseManager.setProfileConnectionPolicy(
                        eq(mSingleDevice), eq(BluetoothProfile.LE_AUDIO), anyInt()))
                .thenReturn(true);
        mService.setConnectionPolicy(mSingleDevice, BluetoothProfile.CONNECTION_POLICY_FORBIDDEN);
        verify(mNativeInterface).setEnableState(eq(mSingleDevice), eq(false));
    }

    /** Test stop LeAudio Service */
    @Test
    public void testStopLeAudioService() {