Bluetooth connectivity: MODE_IN_CALL vs MODE_IN_COMMUNICATION

橙三吉。 提交于 2021-01-27 12:23:02

问题


I have an application that needs to connect to wireless bluetooth headset to collection RAW audio. MODE_IN_CALL works in some devices and MODE_IN_COMMUNICATION in others. Mic works and I lose audio or vice versa.

I am using Nexus 5x and Samsung Edge. But behaviour is inconsistent across 2 devices of same model and make. Phone calls and Media Audio is enabled for paired bluetooth headset.

if (btAdapter != null && btAdapter.isEnabled() && btAdapter.getProfileConnectionState(BluetoothProfile.HEADSET) == BluetoothProfile.STATE_CONNECTED) {
    if (localAudioManager.isBluetoothScoAvailableOffCall()) {
                Bundle extrasBundle = registerReceiver(new BroadcastReceiver() {
                    @Override
                    public void onReceive(Context context, Intent intent) {
                        int conState = intent.getExtras().getInt(AudioManager.EXTRA_SCO_AUDIO_STATE);
                        if (conState ==AudioManager.SCO_AUDIO_STATE_CONNECTED) {
                            localAudioManager.setBluetoothScoOn(true);
                            context.unregisterReceiver(this);
                        } else {
                            if (conState == AudioManager.SCO_AUDIO_STATE_CONNECTING) {
                                writeLog("Bluetooth Receiver :SCO Connecting....");
                            } else if (conState == AudioManager.SCO_AUDIO_STATE_ERROR) {
                                writeLog("Bluetooth Receiver : SCO Error.");
                                context.unregisterReceiver(this);
                            } else if (conState == AudioManager.SCO_AUDIO_STATE_DISCONNECTED) {
                                writeLog("Bluetooth Receiver :SCO Disconnected");
                                localAudioManager.setBluetoothScoOn(false);
                            }
                        }
                    }
                }, new IntentFilter(AudioManager.ACTION_SCO_AUDIO_STATE_UPDATED)).getExtras();

                if (extrasBundle.getInt(AudioManager.ACTION_SCO_AUDIO_STATE_UPDATED) != 2) {
                    localAudioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);
                    localAudioManager.startBluetoothSco();
                }
            }
        }

I need a bluetooth connectivity code that works across devices and OS versions. Thanks in advance for any help.

来源:https://stackoverflow.com/questions/56271485/bluetooth-connectivity-mode-in-call-vs-mode-in-communication

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!