Programmatically connect to paired Bluetooth speaker and play audio

前端 未结 4 1654
陌清茗
陌清茗 2020-12-15 22:59

In our app, I\'d like to connect to a previously paired A2DP Bluetooth Speaker and direct audio playback to it, using Android v4.2 or later.

I can successfully creat

4条回答
  •  孤街浪徒
    2020-12-16 00:04

    Ted,

    I have the same issue like you when I tried with BluetoothHeadset. I guess my work around may work with A2DP. Since my headset only support Handsfree profile. I am only test with BluetoothHeadset.

    No need to establish RFComm channel.

    For me. After you connected to BluetoothHeadsetService, 1. check whether audio is already connected

    mBluetoothSpeaker.isAudioConnected(btSpeaker);
    

    2. if not, establish audio connection

    mBluetoothSpeaker.startVoiceRecognition(btSpeaker);
    

    3. register BroadcastReceiver for BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED and BluetoothHeadset.ACTION_AUDIO_STATE_CHANGED

    registerReceiver(mReceiver, new IntentFilter(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED));
    registerReceiver(mReceiver, new IntentFilter(BluetoothHeadset.ACTION_AUDIO_STATE_CHANGED));
    

    4. BroadcastReceiver

    protected BroadcastReceiver mReceiver = new BroadcastReceiver() {
    
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            int state = BluetoothHeadset.STATE_DISCONNECTED;
            int previousState = intent.getIntExtra(BluetoothHeadset.EXTRA_PREVIOUS_STATE, BluetoothHeadset.STATE_DISCONNECTED);
            if (action.equals(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED)) {
                state = intent.getIntExtra(BluetoothHeadset.EXTRA_STATE, BluetoothHeadset.STATE_DISCONNECTED);
                if (state == BluetoothHeadset.STATE_CONNECTED) {
                    mConnectedHeadset = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                    mInfoTextview.append("\n\nDevice name = " + mConnectedHeadset.getName());
    
                    // Audio should not be connected yet but just to make sure.
                    if (mBluetoothHeadset.isAudioConnected(mConnectedHeadset)) {
                        Log.d(TAG, "Headset audio connected already");
                    } else {
                        if (!mBluetoothHeadset.startVoiceRecognition(mConnectedHeadset)) {
                            Log.e(TAG, "maybe you do not call stopVoiceRecognition previously");
                            mBluetoothHeadset.stopVoiceRecognition(mConnectedHeadset);
                            mBluetoothHeadset.startVoiceRecognition(mConnectedHeadset);
                        }
                    }
                }
                else if (state == BluetoothHeadset.STATE_DISCONNECTED) {
                    mConnectedHeadset = null;
                }
            }
            else if (action.equals(BluetoothHeadset.ACTION_AUDIO_STATE_CHANGED))// audio
            {
                state = intent.getIntExtra(BluetoothHeadset.EXTRA_STATE, BluetoothHeadset.STATE_AUDIO_DISCONNECTED);
                if (state == BluetoothHeadset.STATE_AUDIO_CONNECTED) {
                    // Bluetooth audio connected. you send audio stream to headset now!!!
                    mAudioManager.setMode(AudioManager.STREAM_VOICE_CALL);
                    mMediaPlayer = new MediaPlayer();
                    AssetManager assetManager = getAssets();
                    try {
            AssetFileDescriptor fd = assetManager.openFd("Radioactive.mp3");
            mMediaPlayer.setDataSource(fd.getFileDescriptor());
                        // set audio stream type to STREAM_VOICE_CALL will send audio to headset
                        // @see SCO
                        mMediaPlayer.setAudioStreamType(AudioManager.STREAM_VOICE_CALL);
            mMediaPlayer.prepare();
            Log.d(TAG, "start play music");
            mMediaPlayer.start();
            } catch (IOException e) {
            e.printStackTrace();
            }
                }
                else if (state == BluetoothHeadset.STATE_AUDIO_DISCONNECTED) {
                    if (mMediaPlayer != null) {
                    mMediaPlayer.stop();
                    mMediaPlayer = null;
                    }
                    mBluetoothHeadset.stopVoiceRecognition(mConnectedHeadset);
                }
            }   
        }
    };
    

提交回复
热议问题