问题
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