How to play audio through Bluetooth speaker even when headset is plugged in?

前端 未结 5 606
夕颜
夕颜 2021-02-06 00:53

I have my phone connected to a Bluetooth speaker and the headphones plugged in. Now I\'d like to play audio through the Bluetooth speaker. When I set the audio stream to A

5条回答
  •  灰色年华
    2021-02-06 01:09

    Bluetooth connection may work with below state is true . After receive BluetoothA2dp.STATE_CONNECTED, you can play music as normal.

    Java Code Examples for android.bluetooth.BluetoothA2dp.STATE_CONNECTED

    public BluetoothHandsfree(Context context, CallManager cm) {
        mCM = cm;
        mContext = context;
        BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
        boolean bluetoothCapable = (adapter != null);
        mHeadset = null;  // nothing connected yet
        mA2dp = new BluetoothA2dp(mContext);
        mA2dpState = BluetoothA2dp.STATE_DISCONNECTED;
        mA2dpDevice = null;
        mA2dpSuspended = false;
    
        mPowerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
        mStartCallWakeLock = mPowerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
                                                       TAG + ":StartCall");
        mStartCallWakeLock.setReferenceCounted(false);
        mStartVoiceRecognitionWakeLock = mPowerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
                                                       TAG + ":VoiceRecognition");
        mStartVoiceRecognitionWakeLock.setReferenceCounted(false);
    
        mLocalBrsf = BRSF_AG_THREE_WAY_CALLING |
                     BRSF_AG_EC_NR |
                     BRSF_AG_REJECT_CALL |
                     BRSF_AG_ENHANCED_CALL_STATUS;
    
        if (sVoiceCommandIntent == null) {
            sVoiceCommandIntent = new Intent(Intent.ACTION_VOICE_COMMAND);
            sVoiceCommandIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        if (mContext.getPackageManager().resolveActivity(sVoiceCommandIntent, 0) != null &&
                BluetoothHeadset.isBluetoothVoiceDialingEnabled(mContext)) {
            mLocalBrsf |= BRSF_AG_VOICE_RECOG;
        }
    
        mBluetoothPhoneState = new BluetoothPhoneState();
        mUserWantsAudio = true;
        mPhonebook = new BluetoothAtPhonebook(mContext, this);
        mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
        cdmaSetSecondCallState(false);
    
        if (bluetoothCapable) {
            resetAtState();
        }
    
    }
    

    please find below links : with sample codes it may help you.

    Java Code Examples for android.bluetooth.BluetoothHeadset

    Programmatically connect to paired Bluetooth speaker and play audio

提交回复
热议问题