3

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.

Immanuel
  • 196
  • 13
  • `extrasBundle.getInt(AudioManager.ACTION_SCO_AUDIO_STATE_UPDATED) != 2` is always true, because there is no extra `AudioManager.ACTION_SCO_AUDIO_STATE_UPDATED` – atarasenko May 23 '19 at 10:43
  • Thank you for calling it out. I changed it to extrasBundle.getInt(AudioManager.EXTRA_SCO_AUDIO_STATE) != 2. That still does not solve the problem. Is there a logic to check if I should use MODE_IN_CALL or MODE_IN_COMMUNICATION? Thanks in advance – Immanuel May 24 '19 at 11:22
  • did u get an answer for this? – SnG Apr 21 '20 at 21:44
  • No @SnG. Are you facing the same issue? – Immanuel Apr 23 '20 at 05:05

0 Answers0