Android 12 startBluetoothSco not works once setMode to MODE_IN_COMMUNICATION firstly by other app

Viewed 27

SUBJECT

startBluetoothSco do not work when audio mode was set to MODE_IN_COMMUNICATION firstly by other app on android 12 or higher.

QUESTION

wanna let startBluetoothSco works even have been set to MODE_IN_COMMUNICATION firstly by other app

STEPS

my test steps are as follows:

connect the device with a bluetoothheadset open the first app, let the app set audio mode to MODE_IN_COMMUNICATION and keep recorder or player running. code example :


AudioManager audioManager = (AudioManager) getContext().getSystemService(Context.AUDIO_SERVICE);
                audioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);
 mAudioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC,
                        44100, AudioFormat.CHANNEL_IN_MONO,
                        AudioFormat.ENCODING_PCM_16BIT,
                        11000);
 mAudioRecord.startRecording();

open the second app, and still set audio mode to AudioManager.MODE_IN_COMMUNICATION, by the way, startBluetoothSco.

then the second can not receive any bluetooth broadcast and sco do not works.

however when kill the first app and rerun the second app, sco works.

ANALYSIS

according the source code, here is the code:


private CommunicationRouteClient topCommunicationRouteClient() {
    for (CommunicationRouteClient crc : mCommunicationRouteClients) {
        if (crc.getPid() == mModeOwnerPid) {
            return crc;
        }
    }
    if (!mCommunicationRouteClients.isEmpty() && mModeOwnerPid == 0) {
        return mCommunicationRouteClients.get(0);
    }
    return null;
}

  1. when the first app set mode to MODE_IN_COMMUNICATION and when will become the modeowner
  2. the second app run startBluetoothSco, and will get null client, then will do not start sco

private void onUpdateCommunicationRoute(String eventSource) {
        AudioDeviceAttributes preferredCommunicationDevice = preferredCommunicationDevice();
        if (AudioService.DEBUG_COMM_RTE) {
            Log.v(TAG, "onUpdateCommunicationRoute, preferredCommunicationDevice: "
                    + preferredCommunicationDevice + " eventSource: " + eventSource);
        }
        AudioService.sDeviceLogger.log((new AudioEventLogger.StringEvent(
                "onUpdateCommunicationRoute, preferredCommunicationDevice: "
                + preferredCommunicationDevice + " eventSource: " + eventSource)));

        if (preferredCommunicationDevice == null
                || preferredCommunicationDevice.getType() != AudioDeviceInfo.TYPE_BLUETOOTH_SCO) {
            AudioSystem.setParameters("BT_SCO=off"); 
        } else {
            AudioSystem.setParameters("BT_SCO=on");
        }
        if (preferredCommunicationDevice == null) {
            removePreferredDevicesForStrategySync(mCommunicationStrategyId);
            removePreferredDevicesForStrategySync(mAccessibilityStrategyId);
        } else {
            setPreferredDevicesForStrategySync(
                    mCommunicationStrategyId, Arrays.asList(preferredCommunicationDevice));
            setPreferredDevicesForStrategySync(
                    mAccessibilityStrategyId, Arrays.asList(preferredCommunicationDevice));
        }
        onUpdatePhoneStrategyDevice(preferredCommunicationDevice);
} 

0 Answers
Related