如何检测断开连接的蓝牙音频设备是当前播放音乐流的设备? [英] How to detect that the disconnecting bluetooth audio device is the current device playing the music stream?

查看:83
本文介绍了如何检测断开连接的蓝牙音频设备是当前播放音乐流的设备?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的音频播放器应用当前正在监听 ACTION_ACL_DISCONNECTED 事件,正在检查 BluetoothDevice.EXTRA_DEVICE 的意图,并检查设备的类以查看它是否为 BluetoothClass.Device.Major.AUDIO_VIDEO 设备.如果是这样,那么我想暂停播放.基本上,当蓝牙耳机或汽车连接断开时暂停.

但是,我发现对于某些实际上正在以其他方式收听的用户而言,这种情况正在激发.例如,与蓝牙扬声器配对并收听一会儿.然后插入有线耳机;播放不会错过任何节拍,蓝牙扬声器不再接收音频,但仍处于连接状态.如果我失去了蓝牙连接(关闭扬声器或不在范围内),我的播放会暂停,但我不想这样做,因为 STREAM_MUSIC 音频实际上正在传送到有线耳机./p>

当我收到 ACTION_ACL_DISCONNECTED 时,如何确定该设备当前是否正在接收音乐流? BluetoothDevice 类上有东西吗?我是否可以查询 AudioManager 或其他系统服务,以了解哪个设备正在接收音频流并将其与我刚刚收到断开广播的设备进行比较?有什么想法吗?

更新:

我看到AudioManager提供了 isBluetoothA2dpOn() isBluetoothScoOn() isSpeakerphoneOn() isWiredHeadsetOn().

这适用于我的示例.如果用户已从蓝牙扬声器切换为有线耳机,则 isBluetoothA2dpOn 返回False,因此我可以忽略断开事件.但是,如果用户从一种蓝牙连接更改为另一种蓝牙连接怎么办? isBluetoothA2dpOn 将返回True,但是 ACTION_ACL_DISCONNECTED 可能适用于未接收音频的设备吗?

解决方案

您也可以注册一个广播接收器并播放/暂停音乐.这是我在汽车上使用的音乐应用的kotlin实现

  class BluetoothConnectionStateReceiver:BroadcastReceiver(){重写fun onReceive(context:Context,intent:Intent){val intentAction = intent.action?:返回//仅对BT连接状态感兴趣,与其他STATE_CHANGED无关BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED->{何时(intent.getIntExtra(BluetoothAdapter.EXTRA_CONNECTION_STATE,-1)){BluetoothAdapter.STATE_DISCONNECTED->{logMessage("STATE_DISCONNECTED")//暂停音乐}BluetoothAdapter.STATE_CONNECTED->{logMessage("STATE_CONNECTED")//自动播放音乐}}}}}} 

然后在清单中声明接收方

 <接收器android:name =".music.player.BluetoothConnectionStateReceiverandroid:enabled ="true"<意图过滤器>< action android:name ="android.bluetooth.adapter.action.CONNECTION_STATE_CHANGED"/></intent-filter></receiver> 

My audio player app is currently listening for ACTION_ACL_DISCONNECTED events, examining the intent for BluetoothDevice.EXTRA_DEVICE, and checking the class of the device to see if it is a BluetoothClass.Device.Major.AUDIO_VIDEO device. If so, then I want to pause playback. Basically, pause when Bluetooth headphones or a car connection disconnects.

However, I'm finding that this is firing for some users who are actually listening some other way. For example, pair to a Bluetooth speaker and listen for a while. Then plug in wired headphones; playback doesn't miss a beat, the Bluetooth speaker is no longer receiving the audio but it IS still connected. If I lose that Bluetooth connection (turn off the speaker or walk out of range), my playback is pausing but I don't want it to because the STREAM_MUSIC audio is actually going to the wired headset.

When I receive an ACTION_ACL_DISCONNECTED, how can I tell if that device is the thing currently receiving the music stream? Is there something on the BluetoothDevice class? Can I query the AudioManager or some other system service to learn which device is receiving the audio stream and compare it to the device I just received a disconnection broadcast for? Any ideas?

UPDATE:

I see AudioManager provides isBluetoothA2dpOn(), isBluetoothScoOn(), isSpeakerphoneOn(), and isWiredHeadsetOn().

This works for my example. if the user has switched from Bluetooth speaker to wired headset, isBluetoothA2dpOn returns False so I can disregard the disconnect event. But what if the user changed from one Bluetooth connection to another? isBluetoothA2dpOn will return True but the ACTION_ACL_DISCONNECTED might be for the device that isn't receiving audio?

解决方案

You could also instead register a broadcast receiver and play/pause music. Here's my kotlin implementation of my music app used in the car

class BluetoothConnectionStateReceiver : BroadcastReceiver() {
    override fun onReceive(context: Context, intent: Intent) {
            val intentAction = intent.action ?: return
            // only interested in BT connection state regardless of other STATE_CHANGED
            BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED -> {
                when (intent.getIntExtra(BluetoothAdapter.EXTRA_CONNECTION_STATE, -1)) {
                    BluetoothAdapter.STATE_DISCONNECTED -> {
                        logMessage("STATE_DISCONNECTED")
                        // pause music
                    }
                    BluetoothAdapter.STATE_CONNECTED -> {
                        logMessage("STATE_CONNECTED")
                        // auto play music
                    }
                }
            }
        }
    }
}

Then declare your receiver in your manifest

<receiver
    android:name=".music.player.BluetoothConnectionStateReceiver"
    android:enabled="true">
    <intent-filter>
        <action android:name="android.bluetooth.adapter.action.CONNECTION_STATE_CHANGED" />
    </intent-filter>
</receiver>

这篇关于如何检测断开连接的蓝牙音频设备是当前播放音乐流的设备?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆