AudiManager 从蓝牙更改为 EAR_PIECE 不起作用

AudiManager changing from Bluetooth to EAR_PIECE does not work

我有一个按钮,应该从 EARPIECE 到 SPEAKER 再到 BLUETOOTH 等等。

这是我的代码:

 fun setSpeakerValue(value: SIPManager.AUDIO) {
        speaker = value
        when (value) {
            SIPManager.AUDIO.EAR_PIECE -> {
                Log.i("Speaker", "Speaker1 EARPIECE")
                binding.callItemIconSpeaker.setImageResource(R.drawable.speaker_off)
                if (SIPManager.isBluetoothConnected()) {
                    audioManager?.isBluetoothScoOn = false
                    audioManager?.stopBluetoothSco()
                }
                audioManager?.mode = AudioManager.MODE_NORMAL
                audioManager?.isSpeakerphoneOn = false
            }
            SIPManager.AUDIO.SPEAKER -> {
                Log.i("Speaker", "Speaker1 SPEAKER")
                binding.callItemIconSpeaker.setImageResource(R.drawable.speaker_on)
                if (SIPManager.isBluetoothConnected()) {
                    audioManager?.isBluetoothScoOn = false
                    audioManager?.stopBluetoothSco()
                }
                audioManager?.mode = AudioManager.MODE_IN_COMMUNICATION
                audioManager?.isSpeakerphoneOn = true
            }
            SIPManager.AUDIO.BLUETOOTH -> {
                Log.i("Speaker", "Speaker1 BLUETOOTH")
                binding.callItemIconSpeaker.setImageResource(android.R.drawable.stat_sys_data_bluetooth)
                audioManager?.mode = AudioManager.MODE_NORMAL
                audioManager?.isSpeakerphoneOn = false
                audioManager?.startBluetoothSco()
                audioManager?.isBluetoothScoOn = true
            }
        }
    }

但是当我转向听筒时,我停止了 BluetoothSCO,我将 isSpeakerOn 设置为 false,但在那之后,我开始在蓝牙而不是 phone 的听筒中听到声音。我做错了什么?

使用这里的答案让它工作: How to switch audio output from Phone, Phone Speaker, earphones or Bluetooth device

 //For BT
        mAudioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);
        mAudioManager.startBluetoothSco();
        mAudioManager.setBluetoothScoOn(true);
    } else if(true) {
        //For phone ear piece
        mAudioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);
        mAudioManager.stopBluetoothSco();
        mAudioManager.setBluetoothScoOn(false);
        mAudioManager.setSpeakerphoneOn(false);
    } else {
        //For phone speaker(loudspeaker)
        mAudioManager.setMode(AudioManager.MODE_NORMAL);
        mAudioManager.stopBluetoothSco();
        mAudioManager.setBluetoothScoOn(false);
        mAudioManager.setSpeakerphoneOn(true);
    }