In Android, how to get the profile of a connected bluetooth device?

后端 未结 2 815
故里飘歌
故里飘歌 2020-12-19 00:57

Following a lot of answers here, I am able to build the list of connected bluetooth devices with the help of a BroadcastReceiver. Now my question is how do I kn

2条回答
  •  南笙
    南笙 (楼主)
    2020-12-19 01:29

    I've run into the same problem. It doesn't appear that you can get the available profiles from the BluetoothDevice class. But there is a long way around by getting a List of BluetoothDevices from the getDevicesMatchingConnectionStates method in the BluetoothProfile class.

    For example if you want to find which BluetoothDevices support A2DP, first create a custom BluetoothProfile.ServiceListener

    public class cServiceListener implements BluetoothProfile.ServiceListener {
    private static final int[] states={ BluetoothProfile.STATE_DISCONNECTING,
                                        BluetoothProfile.STATE_DISCONNECTED,
                                        BluetoothProfile.STATE_CONNECTED,
                                        BluetoothProfile.STATE_CONNECTING};
    @Override
    public void onServiceConnected(int profile, BluetoothProfile bluetoothProfile) {
    List Devices=bluetoothProfile.getDevicesMatchingConnectionStates(states);
        for (BluetoothDevice loop:Devices){
            Log.i("myTag",loop.getName());
        }
    }
    
    @Override
    public void onServiceDisconnected(int profile) {
    }
    
    }
    

    Then attach it to the profile you want to check, in this example A2DP

    mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    cServiceListener mServiceListener=new cServiceListener();
    mBluetoothAdapter.getProfileProxy(thisContext,mServiceListener, BluetoothProfile.A2DP);
    

    This will logcat all the bluetooth devices that support A2DP which are in the requested states. In this example it includes all devices which are currently connected and previously paired devices which are disconnected.

提交回复
热议问题