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

Commit ad5de385 authored by Łukasz Rymanowski's avatar Łukasz Rymanowski
Browse files

btservice: Add LeAudio to btservice

This adds active device handling and adds other missing pieces in the
btservice

Bug: 150670922
Tag: #feature
Sponsor: jpawlowski@
Test: atest BluetoothInstrumentationTests
Merged-In: I4d7b7c97fccfca0c7907397085b7ddb63f8d9a03
Change-Id: I4d7b7c97fccfca0c7907397085b7ddb63f8d9a03
parent f26c5847
Loading
Loading
Loading
Loading
+51 −0
Original line number Diff line number Diff line
@@ -21,6 +21,7 @@ import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothHeadset;
import android.bluetooth.BluetoothHearingAid;
import android.bluetooth.BluetoothLeAudio;
import android.bluetooth.BluetoothProfile;
import android.content.BroadcastReceiver;
import android.content.Context;
@@ -38,6 +39,7 @@ import android.util.Log;
import com.android.bluetooth.a2dp.A2dpService;
import com.android.bluetooth.hearingaid.HearingAidService;
import com.android.bluetooth.hfp.HeadsetService;
import com.android.bluetooth.le_audio.LeAudioService;
import com.android.internal.annotations.VisibleForTesting;

import java.util.LinkedList;
@@ -110,6 +112,7 @@ class ActiveDeviceManager {
    private static final int MESSAGE_HFP_ACTION_CONNECTION_STATE_CHANGED = 4;
    private static final int MESSAGE_HFP_ACTION_ACTIVE_DEVICE_CHANGED = 5;
    private static final int MESSAGE_HEARING_AID_ACTION_ACTIVE_DEVICE_CHANGED = 6;
    private static final int MESSAGE_LE_AUDIO_ACTION_ACTIVE_DEVICE_CHANGED = 7;

    private final AdapterService mAdapterService;
    private final ServiceFactory mFactory;
@@ -123,6 +126,7 @@ class ActiveDeviceManager {
    private BluetoothDevice mA2dpActiveDevice = null;
    private BluetoothDevice mHfpActiveDevice = null;
    private BluetoothDevice mHearingAidActiveDevice = null;
    private BluetoothDevice mLeAudioActiveDevice = null;

    // Broadcast receiver for all changes
    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
@@ -158,6 +162,10 @@ class ActiveDeviceManager {
                    mHandler.obtainMessage(MESSAGE_HEARING_AID_ACTION_ACTIVE_DEVICE_CHANGED,
                            intent).sendToTarget();
                    break;
                case BluetoothLeAudio.ACTION_LE_AUDIO_ACTIVE_DEVICE_CHANGED:
                    mHandler.obtainMessage(MESSAGE_LE_AUDIO_ACTION_ACTIVE_DEVICE_CHANGED,
                            intent).sendToTarget();
                    break;
                default:
                    Log.e(TAG, "Received unexpected intent, action=" + action);
                    break;
@@ -239,6 +247,7 @@ class ActiveDeviceManager {
                    }
                    if (device != null && !Objects.equals(mA2dpActiveDevice, device)) {
                        setHearingAidActiveDevice(null);
                        setLeAudioActiveDevice(null);
                    }
                    // Just assign locally the new value
                    mA2dpActiveDevice = device;
@@ -298,6 +307,7 @@ class ActiveDeviceManager {
                    }
                    if (device != null && !Objects.equals(mHfpActiveDevice, device)) {
                        setHearingAidActiveDevice(null);
                        setLeAudioActiveDevice(null);
                    }
                    // Just assign locally the new value
                    mHfpActiveDevice = device;
@@ -317,9 +327,28 @@ class ActiveDeviceManager {
                    if (device != null) {
                        setA2dpActiveDevice(null);
                        setHfpActiveDevice(null);
                        setLeAudioActiveDevice(null);
                    }
                }
                break;

                case MESSAGE_LE_AUDIO_ACTION_ACTIVE_DEVICE_CHANGED: {
                    Intent intent = (Intent) msg.obj;
                    BluetoothDevice device =
                            intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                    if (DBG) {
                        Log.d(TAG, "handleMessage(MESSAGE_LE_AUDIO_ACTION_ACTIVE_DEVICE_CHANGED): "
                                + "device= " + device);
                    }
                    // Just assign locally the new value
                    if (device != null && !Objects.equals(mLeAudioActiveDevice, device)) {
                        setA2dpActiveDevice(null);
                        setHfpActiveDevice(null);
                        setHearingAidActiveDevice(null);
                    }
                    mLeAudioActiveDevice = device;
                }
                break;
            }
        }
    }
@@ -387,6 +416,7 @@ class ActiveDeviceManager {
        filter.addAction(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED);
        filter.addAction(BluetoothHeadset.ACTION_ACTIVE_DEVICE_CHANGED);
        filter.addAction(BluetoothHearingAid.ACTION_ACTIVE_DEVICE_CHANGED);
        filter.addAction(BluetoothLeAudio.ACTION_LE_AUDIO_ACTIVE_DEVICE_CHANGED);
        mAdapterService.registerReceiver(mReceiver, filter);

        mAudioManager.registerAudioDeviceCallback(mAudioManagerAudioDeviceCallback, mHandler);
@@ -462,6 +492,20 @@ class ActiveDeviceManager {
        mHearingAidActiveDevice = device;
    }

    private void setLeAudioActiveDevice(BluetoothDevice device) {
        if (DBG) {
            Log.d(TAG, "setLeAudioActiveDevice(" + device + ")");
        }
        final LeAudioService leAudioService = mFactory.getLeAudioService();
        if (leAudioService == null) {
            return;
        }
        if (!leAudioService.setActiveDevice(device)) {
            return;
        }
        mLeAudioActiveDevice = device;
    }

    private void resetState() {
        mA2dpConnectedDevices.clear();
        mA2dpActiveDevice = null;
@@ -470,6 +514,7 @@ class ActiveDeviceManager {
        mHfpActiveDevice = null;

        mHearingAidActiveDevice = null;
        mLeAudioActiveDevice = null;
    }

    @VisibleForTesting
@@ -492,6 +537,11 @@ class ActiveDeviceManager {
        return mHearingAidActiveDevice;
    }

    @VisibleForTesting
    BluetoothDevice getLeAudioActiveDevice() {
        return mLeAudioActiveDevice;
    }

    /**
     * Called when a wired audio device is connected.
     * It might be called multiple times each time a wired audio device is connected.
@@ -504,5 +554,6 @@ class ActiveDeviceManager {
        setA2dpActiveDevice(null);
        setHfpActiveDevice(null);
        setHearingAidActiveDevice(null);
        setLeAudioActiveDevice(null);
    }
}
+36 −1
Original line number Diff line number Diff line
@@ -100,6 +100,7 @@ import com.android.bluetooth.hfp.HeadsetService;
import com.android.bluetooth.hfpclient.HeadsetClientService;
import com.android.bluetooth.hid.HidDeviceService;
import com.android.bluetooth.hid.HidHostService;
import com.android.bluetooth.le_audio.LeAudioService;
import com.android.bluetooth.map.BluetoothMapService;
import com.android.bluetooth.mapclient.MapClientService;
import com.android.bluetooth.pan.PanService;
@@ -280,6 +281,7 @@ public class AdapterService extends Service {
    private SapService mSapService;
    private VolumeControlService mVolumeControlService;
    private CsipSetCoordinatorService mCsipSetCoordinatorService;
    private LeAudioService mLeAudioService;

    /**
     * Register a {@link ProfileService} with AdapterService.
@@ -941,6 +943,9 @@ public class AdapterService extends Service {
        if (profile == BluetoothProfile.CSIP_SET_COORDINATOR) {
            return Utils.arrayContains(remoteDeviceUuids, BluetoothUuid.COORDINATED_SET);
        }
        if (profile == BluetoothProfile.LE_AUDIO) {
            return Utils.arrayContains(remoteDeviceUuids, BluetoothUuid.LE_AUDIO);
        }

        Log.e(TAG, "isSupported: Unexpected profile passed in to function: " + profile);
        return false;
@@ -999,6 +1004,10 @@ public class AdapterService extends Service {
                        > BluetoothProfile.CONNECTION_POLICY_FORBIDDEN) {
            return true;
        }
        if (mLeAudioService != null && mLeAudioService.getConnectionPolicy(device)
                > BluetoothProfile.CONNECTION_POLICY_FORBIDDEN) {
            return true;
        }
        return false;
    }

@@ -1086,7 +1095,13 @@ public class AdapterService extends Service {
            Log.i(TAG, "connectEnabledProfiles: Connecting Coordinated Set Profile");
            mCsipSetCoordinatorService.connect(device);
        }

        if (mLeAudioService != null && isSupported(localDeviceUuids, remoteDeviceUuids,
                BluetoothProfile.LE_AUDIO, device)
                && mLeAudioService.getConnectionPolicy(device)
                > BluetoothProfile.CONNECTION_POLICY_FORBIDDEN) {
            Log.i(TAG, "connectEnabledProfiles: Connecting LeAudio profile (BAP)");
            mLeAudioService.connect(device);
        }
        return true;
    }

@@ -1125,6 +1140,7 @@ public class AdapterService extends Service {
        mSapService = SapService.getSapService();
        mVolumeControlService = VolumeControlService.getVolumeControlService();
        mCsipSetCoordinatorService = CsipSetCoordinatorService.getCsipSetCoordinatorService();
        mLeAudioService = LeAudioService.getLeAudioService();
    }

    private boolean isAvailable() {
@@ -2583,6 +2599,13 @@ public class AdapterService extends Service {
                return false;
        }

        if (mLeAudioService != null && (device == null
                || mLeAudioService.getConnectionPolicy(device)
                == BluetoothProfile.CONNECTION_POLICY_ALLOWED)) {
            Log.i(TAG, "setActiveDevice: Setting active Le Audio device " + device);
            mLeAudioService.setActiveDevice(device);
        }

        if (setA2dp && mA2dpService != null && (device == null
                || mA2dpService.getConnectionPolicy(device)
                == BluetoothProfile.CONNECTION_POLICY_ALLOWED)) {
@@ -2704,6 +2727,13 @@ public class AdapterService extends Service {
                    device, BluetoothProfile.CONNECTION_POLICY_ALLOWED);
            numProfilesConnected++;
        }
        if (mLeAudioService != null && isSupported(localDeviceUuids, remoteDeviceUuids,
                BluetoothProfile.LE_AUDIO, device)) {
            Log.i(TAG, "connectAllEnabledProfiles: Connecting LeAudio profile (BAP)");
            mLeAudioService.setConnectionPolicy(device,
                    BluetoothProfile.CONNECTION_POLICY_ALLOWED);
            numProfilesConnected++;
        }

        Log.i(TAG, "connectAllEnabledProfiles: Number of Profiles Connected: "
                + numProfilesConnected);
@@ -2800,6 +2830,11 @@ public class AdapterService extends Service {
            Log.i(TAG, "disconnectAllEnabledProfiles: Disconnecting Coordinater Set Profile");
            mCsipSetCoordinatorService.disconnect(device);
        }
        if (mLeAudioService != null && mLeAudioService.getConnectionState(device)
                == BluetoothProfile.STATE_CONNECTED) {
            Log.i(TAG, "disconnectAllEnabledProfiles: Disconnecting LeAudio profile (BAP)");
            mLeAudioService.disconnect(device);
        }

        return true;
    }
+5 −0
Original line number Diff line number Diff line
@@ -22,6 +22,7 @@ import com.android.bluetooth.hearingaid.HearingAidService;
import com.android.bluetooth.hfp.HeadsetService;
import com.android.bluetooth.hid.HidDeviceService;
import com.android.bluetooth.hid.HidHostService;
import com.android.bluetooth.le_audio.LeAudioService;
import com.android.bluetooth.mcp.McpService;
import com.android.bluetooth.pan.PanService;

@@ -51,6 +52,10 @@ public class ServiceFactory {
        return HearingAidService.getHearingAidService();
    }

    public LeAudioService getLeAudioService() {
        return LeAudioService.getLeAudioService();
    }

    public AvrcpTargetService getAvrcpTargetService() {
        return AvrcpTargetService.get();
    }