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

Commit 637fc937 authored by Eric Laurent's avatar Eric Laurent
Browse files

AudioService: BtHelper: supplementary fix for late BT profile connection

Commit 90420310 was incomplete in that it would not properly
differentiate LE output devices from LE input devices.
Also prevent from connecting 2 active devices for the same profile with
different addresses.

Bug: 328248404
Test: make
Change-Id: I4a4340fdea865304097e987361fc810420056d6f
parent 42f1e793
Loading
Loading
Loading
Loading
+0 −1
Original line number Diff line number Diff line
@@ -1436,7 +1436,6 @@ public class AudioDeviceBroker {
        sendMsgNoDelay(MSG_BROADCAST_AUDIO_BECOMING_NOISY, SENDMSG_REPLACE);
    }

    @GuardedBy("mDeviceStateLock")
    /*package*/ void postBluetoothActiveDevice(BtDeviceInfo info, int delay) {
        sendLMsg(MSG_L_SET_BT_ACTIVE_DEVICE, SENDMSG_QUEUE, info, delay);
    }
+1 −1
Original line number Diff line number Diff line
@@ -7888,7 +7888,7 @@ public class AudioService extends IAudioService.Stub
                    + previousDevice + " -> " + newDevice + ". Got: " + profile);
        }
        sDeviceLogger.enqueue(new EventLogger.StringEvent("BlutoothActiveDeviceChanged for "
        sDeviceLogger.enqueue(new EventLogger.StringEvent("BluetoothActiveDeviceChanged for "
                + BluetoothProfile.getProfileName(profile) + ", device update " + previousDevice
                + " -> " + newDevice).printLog(TAG));
        AudioDeviceBroker.BtDeviceChangedData data =
+62 −11
Original line number Diff line number Diff line
@@ -289,6 +289,7 @@ public class BtHelper {
                    Log.e(TAG, "Exception while getting status of " + device, e);
                }
                if (btCodecStatus == null) {
                    Log.e(TAG, "getCodec, null A2DP codec status for device: " + device);
                    mA2dpCodecConfig = null;
                    return new Pair<>(AudioSystem.AUDIO_FORMAT_DEFAULT, changed);
                }
@@ -316,6 +317,7 @@ public class BtHelper {
                    Log.e(TAG, "Exception while getting status of " + device, e);
                }
                if (btLeCodecStatus == null) {
                    Log.e(TAG, "getCodec, null LE codec status for device: " + device);
                    mLeAudioCodecConfig = null;
                    return new Pair<>(AudioSystem.AUDIO_FORMAT_DEFAULT, changed);
                }
@@ -363,6 +365,7 @@ public class BtHelper {
            return new Pair<>(profile == BluetoothProfile.A2DP
                    ? AudioSystem.AUDIO_FORMAT_SBC : AudioSystem.AUDIO_FORMAT_LC3, true);
        }

        return codecAndChanged;
    }

@@ -653,7 +656,7 @@ public class BtHelper {
                // Not a valid profile to connect
                Log.e(TAG, "onBtProfileConnected: Not a valid profile to connect "
                        + BluetoothProfile.getProfileName(profile));
                break;
                return;
        }

        // this part is only for A2DP, LE Audio unicast and Hearing aid
@@ -664,18 +667,66 @@ public class BtHelper {
            return;
        }
        List<BluetoothDevice> activeDevices = adapter.getActiveDevices(profile);
        BluetoothProfileConnectionInfo bpci = new BluetoothProfileConnectionInfo(profile);
        for (BluetoothDevice device : activeDevices) {
            if (device == null) {
                continue;
        if (activeDevices.isEmpty() || activeDevices.get(0) == null) {
            return;
        }
        BluetoothDevice device = activeDevices.get(0);
        switch (profile) {
            case BluetoothProfile.A2DP: {
                BluetoothProfileConnectionInfo bpci =
                        BluetoothProfileConnectionInfo.createA2dpInfo(false, -1);
                postBluetoothActiveDevice(device, bpci);
            } break;
            case BluetoothProfile.HEARING_AID: {
                BluetoothProfileConnectionInfo bpci =
                        BluetoothProfileConnectionInfo.createHearingAidInfo(false);
                postBluetoothActiveDevice(device, bpci);
            } break;
            case BluetoothProfile.LE_AUDIO: {
                int groupId = mLeAudio.getGroupId(device);
                BluetoothLeAudioCodecStatus btLeCodecStatus = null;
                try {
                    btLeCodecStatus = mLeAudio.getCodecStatus(groupId);
                } catch (Exception e) {
                    Log.e(TAG, "Exception while getting status of " + device, e);
                }
                if (btLeCodecStatus == null) {
                    Log.i(TAG, "onBtProfileConnected null LE codec status for groupId: "
                            + groupId + ", device: " + device);
                    break;
                }
                List<BluetoothLeAudioCodecConfig> outputCodecConfigs =
                        btLeCodecStatus.getOutputCodecSelectableCapabilities();
                if (!outputCodecConfigs.isEmpty()) {
                    BluetoothProfileConnectionInfo bpci =
                            BluetoothProfileConnectionInfo.createLeAudioInfo(
                                    false /*suppressNoisyIntent*/, true /*isLeOutput*/);
                    postBluetoothActiveDevice(device, bpci);
                }
                List<BluetoothLeAudioCodecConfig> inputCodecConfigs =
                        btLeCodecStatus.getInputCodecSelectableCapabilities();
                if (!inputCodecConfigs.isEmpty()) {
                    BluetoothProfileConnectionInfo bpci =
                            BluetoothProfileConnectionInfo.createLeAudioInfo(
                                    false /*suppressNoisyIntent*/, false /*isLeOutput*/);
                    postBluetoothActiveDevice(device, bpci);
                }
            } break;
            default:
                // Not a valid profile to connect
                Log.wtf(TAG, "Invalid profile! onBtProfileConnected");
                break;
        }
    }

    private void postBluetoothActiveDevice(
            BluetoothDevice device, BluetoothProfileConnectionInfo bpci) {
        AudioDeviceBroker.BtDeviceChangedData data = new AudioDeviceBroker.BtDeviceChangedData(
                device, null, bpci, "mBluetoothProfileServiceListener");
        AudioDeviceBroker.BtDeviceInfo info = mDeviceBroker.createBtDeviceInfo(
                data, device, BluetoothProfile.STATE_CONNECTED);
        mDeviceBroker.postBluetoothActiveDevice(info, 0 /* delay */);
    }
    }

    /*package*/ synchronized boolean isProfilePoxyConnected(int profile) {
        switch (profile) {