Android: 如何检测当前正在接收音频流的是哪个蓝牙设备?

Android: How to detect which Bluetooth device is currently getting audio stream?

我正在获取这样的 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 版本中,您可以连接到蓝牙设备但仍在 phone 本身播放音频,有没有办法知道这种情况何时发生?

尝试在此处查看详细信息 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!"; } }

我终于找到了解决办法。

    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);