【问题标题】:Android: How to detect which Bluetooth device is currently getting audio stream?Android:如何检测当前正在获取音频流的蓝牙设备?
【发布时间】:2020-11-13 18:33:27
【问题描述】:

我得到一个 A2DP 设备列表,如下所示:

bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
bluetoothAdapter.getProfileProxy(context, new BluetoothProfile.ServiceListener() {

    @Override
    public void onServiceConnected(int profile, BluetoothProfile proxy) {
        List<BluetoothDevice> devices = proxy.getConnectedDevices();
    }

    @Override
    public void onServiceDisconnected(int profile) {

    }
}, BluetoothProfile.A2DP);

问题在于,有时,当我连接了多个设备时,只有其中一个设备从我的应用程序获取音频流。我怎么知道哪个设备是获取流的设备?另外我听说在较新的 Android 版本中,您可以连接到蓝牙设备,但仍然在手机本身播放音频,有没有办法知道什么时候会发生这种情况?

【问题讨论】:

    标签: android android-bluetooth a2dp


    【解决方案1】:

    我终于找到了解决办法。

        bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        bluetoothAdapter.getProfileProxy(context, new BluetoothProfile.ServiceListener() {
    
        @Override
        public void onServiceConnected(int profile, BluetoothProfile proxy) {
            List<BluetoothDevice> devices = proxy.getConnectedDevices();
            BluetoothA2dp bA2dp = (BluetoothA2dp) proxy;
            for (BluetoothDevice device: devices) { 
                boolean isPlaying = bA2dp.isA2dpPlaying(device);
            }
        }
    
        @Override
        public void onServiceDisconnected(int profile) {
    
        }
    }, BluetoothProfile.A2DP);
    

    【讨论】:

      【解决方案2】:

      请尝试在此处查看详细信息 https://developer.android.com/reference/android/bluetooth/BluetoothClass.Device.Major``https://developer.android.com/reference/android/bluetooth/BluetoothClass.Device.Major

      for (BluetoothDevice device : pairedDevices) { String deviceBTMajorClass = getBTMajorDeviceClass(device.getBluetoothClass().getMajorDeviceClass()); if (D) Log.d(TAG, "deviceBTMajorClass"+deviceBTMajorClass); //btArrayAdapter.add(deviceBTName + "\n"+ deviceBTMajorClass); data.add(device.getName() + "\n" + device.getAddress()); } private String getBTMajorDeviceClass(int major) { switch (major) { case BluetoothClass.Device.Major.AUDIO_VIDEO: return "AUDIO_VIDEO"; case BluetoothClass.Device.Major.COMPUTER: return "COMPUTER"; case BluetoothClass.Device.Major.HEALTH: return "HEALTH"; case BluetoothClass.Device.Major.IMAGING: return "IMAGING"; case BluetoothClass.Device.Major.MISC: return "MISC"; case BluetoothClass.Device.Major.NETWORKING: return "NETWORKING"; case BluetoothClass.Device.Major.PERIPHERAL: return "PERIPHERAL"; case BluetoothClass.Device.Major.PHONE: return "PHONE"; case BluetoothClass.Device.Major.TOY: return "TOY"; case BluetoothClass.Device.Major.UNCATEGORIZED: return "UNCATEGORIZED"; case BluetoothClass.Device.Major.WEARABLE: return "AUDIO_VIDEO";`enter code here` default: return "unknown!"; } }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-12-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-10
        • 1970-01-01
        相关资源
        最近更新 更多