3

我有一个应用程序需要连接到无线蓝牙耳机来收集 RAW 音频。MODE_IN_CALL 适用于某些设备,而 MODE_IN_COMMUNICATION 适用于其他设备。麦克风工作,我失去了音频,反之亦然。

我正在使用 Nexus 5x 和三星 Edge。但是,相同型号和品牌的 2 台设备的行为不一致。已为配对的蓝牙耳机启用电话呼叫和媒体音频。

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();
                }
            }
        }

我需要一个跨设备和操作系统版本的蓝牙连接代码。提前感谢您的帮助。

4

0 回答 0