【问题标题】:How to switching audio between Bluetooth audio devices on android using programmatically如何以编程方式在android上的蓝牙音频设备之间切换音频
【发布时间】:2019-06-05 10:19:31
【问题描述】:

让我困惑了很久。

我在安卓手机上连接了两个蓝牙音频设备,我想以编程方式切换特定的蓝牙音频设备。

我搜索关于 a2dp、媒体路由器和 audioManager 的关键字似乎不能做这件事......

我唯一找到的功能是:

BT 设备=> A 和 B 连接在 Android 手机上。 现在媒体播放到A,我想切换到B

Step1:A、B在Android手机上解除配对。

Step2:Android 手机上的一对。

Step3:安卓手机B对。

媒体播放输出为B,

似乎最近的一对蓝牙设备是媒体播放输出。

谁能给我一些建议或方向? 谢谢各位

【问题讨论】:

  • 您是在寻找代码示例还是通用方法?
  • @bautista 我想找到代码示例。而且我发现使用A2dp断开和重新连接可以切换音频的活动,我希望找到更聪明的代码示例。

标签: java android kotlin bluetooth


【解决方案1】:

Documentation表示一次只能连接一个A2DP设备,所以你应该可以通过连接到你想使用的设备在设备之间切换。 您可以使用BluetoothSockets 连接到设备。

编辑: 我找到了其他解决方案,我认为这是可靠的。它们都依赖于方法反射。

第一个来自这个post。 1.从serviceListener获取BluetoothA2dp代理

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. 从反射中获取“连接”方法。

    private val connect = BluetoothA2dp::class.java.getDeclaredMethod("connect",BluetoothDevice::class.java)
    
  2. 使用代理作为第一个参数和设备作为第二个参数的调用方法。

    connect.invoke(bluetoothA2dp,device)

其他方法使用反射来获取 createRfcommSocket 以规避一个您通常无法访问的参数的问题。我可以发布它的链接,但我遇到的问题是主线程总是滞后,总是给我一个错误并且无法预测地连接。

【讨论】:

  • 谢谢你的回复,那么BluetoothSockets可以控制蓝牙耳塞吗?我认为 bluetoothSockets 仅在我们想要控制时在两个 android 手机之间使用。
  • 不幸的是,我不知道设置它的正确方法。到目前为止,这是我能做到的最接近的(处理类似问题)。另一个最接近找到答案的事情是这个线程,但我很难弄清楚哪个部分实际设置了当前设备播放声音。 stackoverflow.com/questions/22226552/…
  • 希望新增功能对您有更多帮助!
猜你喜欢
  • 1970-01-01
  • 2013-12-08
  • 2022-06-16
  • 1970-01-01
  • 2020-05-23
  • 2014-01-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多