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

Commit 4be42fa7 authored by Yiyi Shen's avatar Yiyi Shen
Browse files

[Audiosharing] Add getGroupId to LeAudioProfile

Test: manual
Bug: 305620450
Bug: 319723439
Change-Id: I30c41fdbb731b549eec094a5349c3db79be1219e
parent e27e4e3a
Loading
Loading
Loading
Loading
+59 −47
Original line number Diff line number Diff line
@@ -23,6 +23,7 @@ import static android.bluetooth.BluetoothProfile.CONNECTION_POLICY_FORBIDDEN;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothClass;
import android.bluetooth.BluetoothCsipSetCoordinator;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothLeAudio;
import android.bluetooth.BluetoothProfile;
@@ -30,6 +31,7 @@ import android.content.Context;
import android.os.Build;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;

@@ -57,8 +59,7 @@ public class LeAudioProfile implements LocalBluetoothProfile {
    private static final int ORDINAL = 1;

    // These callbacks run on the main thread.
    private final class LeAudioServiceListener
            implements BluetoothProfile.ServiceListener {
    private final class LeAudioServiceListener implements BluetoothProfile.ServiceListener {

        @RequiresApi(Build.VERSION_CODES.S)
        public void onServiceConnected(int profile, BluetoothProfile proxy) {
@@ -78,8 +79,7 @@ public class LeAudioProfile implements LocalBluetoothProfile {
                    }
                    device = mDeviceManager.addDevice(nextDevice);
                }
                device.onProfileStateChanged(LeAudioProfile.this,
                        BluetoothProfile.STATE_CONNECTED);
                device.onProfileStateChanged(LeAudioProfile.this, BluetoothProfile.STATE_CONNECTED);
                device.refresh();
            }

@@ -105,7 +105,9 @@ public class LeAudioProfile implements LocalBluetoothProfile {
        return BluetoothProfile.LE_AUDIO;
    }

    LeAudioProfile(Context context, CachedBluetoothDeviceManager deviceManager,
    LeAudioProfile(
            Context context,
            CachedBluetoothDeviceManager deviceManager,
            LocalBluetoothProfileManager profileManager) {
        mContext = context;
        mDeviceManager = deviceManager;
@@ -113,8 +115,7 @@ public class LeAudioProfile implements LocalBluetoothProfile {

        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        mBluetoothAdapter.getProfileProxy(
                context, new LeAudioServiceListener(),
                BluetoothProfile.LE_AUDIO);
                context, new LeAudioServiceListener(), BluetoothProfile.LE_AUDIO);
    }

    public boolean accessProfileEnabled() {
@@ -126,18 +127,22 @@ public class LeAudioProfile implements LocalBluetoothProfile {
    }

    public List<BluetoothDevice> getConnectedDevices() {
        return getDevicesByStates(new int[] {
        return getDevicesByStates(
                new int[] {
                    BluetoothProfile.STATE_CONNECTED,
                    BluetoothProfile.STATE_CONNECTING,
                BluetoothProfile.STATE_DISCONNECTING});
                    BluetoothProfile.STATE_DISCONNECTING
                });
    }

    public List<BluetoothDevice> getConnectableDevices() {
        return getDevicesByStates(new int[] {
        return getDevicesByStates(
                new int[] {
                    BluetoothProfile.STATE_DISCONNECTED,
                    BluetoothProfile.STATE_CONNECTED,
                    BluetoothProfile.STATE_CONNECTING,
                BluetoothProfile.STATE_DISCONNECTING});
                    BluetoothProfile.STATE_DISCONNECTING
                });
    }

    private List<BluetoothDevice> getDevicesByStates(int[] states) {
@@ -174,6 +179,14 @@ public class LeAudioProfile implements LocalBluetoothProfile {
        return mService.getConnectionState(device);
    }

    /** Get group id for {@link BluetoothDevice}. */
    public int getGroupId(@NonNull BluetoothDevice device) {
        if (mService == null) {
            return BluetoothCsipSetCoordinator.GROUP_ID_INVALID;
        }
        return mService.getGroupId(device);
    }

    public boolean setActiveDevice(BluetoothDevice device) {
        if (mBluetoothAdapter == null) {
            return false;
@@ -193,20 +206,19 @@ public class LeAudioProfile implements LocalBluetoothProfile {
    /**
     * Get Lead device for the group.
     *
     * Lead device is the device that can be used as an active device in the system.
     * Active devices points to the Audio Device for the Le Audio group.
     * This method returns the Lead devices for the connected LE Audio
     * group and this device should be used in the setActiveDevice() method by other parts
     * of the system, which wants to set to active a particular Le Audio group.
     * <p>Lead device is the device that can be used as an active device in the system. Active
     * devices points to the Audio Device for the Le Audio group. This method returns the Lead
     * devices for the connected LE Audio group and this device should be used in the
     * setActiveDevice() method by other parts of the system, which wants to set to active a
     * particular Le Audio group.
     *
     * Note: getActiveDevice() returns the Lead device for the currently active LE Audio group.
     * <p>Note: getActiveDevice() returns the Lead device for the currently active LE Audio group.
     * Note: When Lead device gets disconnected while Le Audio group is active and has more devices
     * in the group, then Lead device will not change. If Lead device gets disconnected, for the
     * Le Audio group which is not active, a new Lead device will be chosen
     * in the group, then Lead device will not change. If Lead device gets disconnected, for the Le
     * Audio group which is not active, a new Lead device will be chosen
     *
     * @param groupId The group id.
     * @return group lead device.
     *
     * @hide
     */
    @RequiresApi(Build.VERSION_CODES.TIRAMISU)
@@ -310,8 +322,8 @@ public class LeAudioProfile implements LocalBluetoothProfile {
        }
        if (mService != null) {
            try {
                BluetoothAdapter.getDefaultAdapter().closeProfileProxy(BluetoothProfile.LE_AUDIO,
                        mService);
                BluetoothAdapter.getDefaultAdapter()
                        .closeProfileProxy(BluetoothProfile.LE_AUDIO, mService);
                mService = null;
            } catch (Throwable t) {
                Log.w(TAG, "Error cleaning up LeAudio proxy", t);