0

It's let me confused for a long time.

I have two bt audio devices connected on android phone, and I want to switch specific bt audio device using programmatically.

I search keyword about a2dp, media router and audioManager than seems can't do this thing...

I only way found the function is:

BT devices=> A and B connected on an Android phone. And right now the media play to A and I want to switch to B

Step1: A, B unpair on Android phone.

Step2: A pair on Android phone.

Step3: B pair on Android phone.

The media play output is B,

seems lastest pair Bluetooth device is media play output.

Can anyone give me some advice or direction? Thanks, guys

MarkWang
  • 191
  • 2
  • 6
  • Are you looking for code samples or a general approach? – bautista Jun 05 '19 at 12:17
  • @bautista I want to find the code samples. And I found the using A2dp disconnect and reconnect can switch the activity of audio, I hope to find the smarter code samples. – MarkWang Jun 06 '19 at 09:18

1 Answers1

2

Documentation says it can only be connected to a single A2DP device at a time, so you should be able to switch between devices by connecting to device you want to use. You can connect to device by using BluetoothSockets.

EDIT: I found other solutions, one which I find reliable. Both of them rely on method reflections.

First one is from this post.

  1. Get BluetoothA2dp proxy from serviceListener
  bluetoothManager.adapter.getProfileProxy(this, serviceListener, BluetoothProfile.A2DP)

  private val serviceListener = object : BluetoothProfile.ServiceListener {
      override fun onServiceDisconnected(profile: Int) {
          if (profile == BluetoothProfile.A2DP) {
              bluetoothA2dp = null
          }
      }

      override fun onServiceConnected(profile: Int, proxy: BluetoothProfile?) {
          if (profile == BluetoothProfile.A2DP) {
              handler.post(btSearchRunnable)
              bluetoothA2dp = proxy as BluetoothA2dp
          }
      }
  }
  1. Get "connect" method from reflection.
private val connect = BluetoothA2dp::class.java.getDeclaredMethod("connect",BluetoothDevice::class.java)
  1. Invoke method with proxy as first argument and device as second.
connect.invoke(bluetoothA2dp,device)

Other method used reflection to get createRfcommSocket to circumvent issue with one parameter which you can't normally access. I can post a link to it, but a problem I had with it is that always lagged main thread, always threw me an error and connected unpredictably.

Umesh P
  • 228
  • 2
  • 7
  • Thanks your reply, so BluetoothSockets can control Bluetooth earbuds? I think bluetoothSockets only use between two android phone when we want to control. – MarkWang Aug 28 '19 at 07:29
  • Unfortunately, I wouldn't know correct way to set it. That was closest I could get so far (dealing with similar issue). Other closest thing to finding an answer was this thread, but I have hard time figuring out which part actually sets current device playing sound. https://stackoverflow.com/questions/22226552/programmatically-connect-to-paired-bluetooth-speaker-and-play-audio/22390540#22390540 – Krešimir Turkalj Aug 29 '19 at 12:32
  • Hopefully new addition helps you more! – Krešimir Turkalj Oct 12 '19 at 13:49