在Android中,如何获取连接的蓝牙设备的配置文件?

时间:2014-05-09 04:46:59

标签: android bluetooth

在这里得到很多答案后,我可以在BroadcastReceiver的帮助下构建连接蓝牙设备列表。现在我的问题是如何知道哪个设备支持哪个配置文件。我希望能够根据配置文件选择设备,例如,获取当前连接的设备及其配置文件的列表,然后选择其中一个。如果我有BluetoothDevice的实例,我不知道如何获得这样的信息。

在此页面上,有一些代码说明了如何使用蓝牙耳机配置文件:http://developer.android.com/guide/topics/connectivity/bluetooth.html#Profiles。但它并没有解决我的问题。如果您认为我遗漏了任何东西,请帮助我并指出。

提前多多感谢。

2 个答案:

答案 0 :(得分:3)

我遇到了同样的问题。您似乎无法从 BluetoothDevice 类中获取可用的配置文件。 但是,要从 BluetoothProfile 类中的 getDevicesMatchingConnectionStates 方法获取 BluetoothDevice 列表,还有很长的路要走。

例如,如果您想查找哪些 BluetoothDevice 支持A2DP,请首先创建自定义 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<BluetoothDevice> Devices=bluetoothProfile.getDevicesMatchingConnectionStates(states);
    for (BluetoothDevice loop:Devices){
        Log.i("myTag",loop.getName());
    }
}

@Override
public void onServiceDisconnected(int profile) {
}

}

然后将其附加到您要检查的配置文件,在此示例中为A2DP

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

这将logcat 所有支持A2DP的蓝牙设备,这些设备位于请求的 状态 中。在此示例中,它包括当前连接的所有设备以及先前已断开连接的配对设备。

答案 1 :(得分:0)

查看Android源代码,您可以通过查看其UUID来猜测设备可用的配置文件,然后逐个连接每个配置文件。

第0步:从那里复制_PROFILE_UUIDS个常量:https://android.googlesource.com/platform/packages/apps/Settings/+/9ad703cdb9a8d0972c123b041d18aa7bbeb391a4/src/com/android/settings/bluetooth/LocalBluetoothProfileManager.java

第1步:通过扫描获取BluetoothDevice。确保它正确粘合。

第2步:BroadcastReceiver动作意图注册android.bluetooth.BluetoothDevice.ACTION_UUID

在设备上

第3步:,请调用fetchUuidsWithSdp方法

第4步:您将收到ACTION_UUID广播:在onReceive方法中,您可以取消注册接收器,并获取如下所示的配置文件列表:

ArrayList<Integer> profiles = new ArrayList<>();

ParcelUuid[] uuids = device.getUuids();

if (BluetoothUuid.containsAnyUuid(uuids, HEADSET_PROFILE_UUIDS))
{
    profiles.add(BluetoothProfile.HEADSET);
}

if (BluetoothUuid.containsAnyUuid(uuids, A2DP_PROFILE_UUIDS))
{
    profiles.add(BluetoothProfile.A2DP);
}

if (BluetoothUuid.containsAnyUuid(uuids, OPP_PROFILE_UUIDS))
{
    //OPP doesn't have any BluetoothProfile value
}

if (BluetoothUuid.containsAnyUuid(uuids, HID_PROFILE_UUIDS))
{
    //You will need system privileges in order to use this one
    profiles.add(BluetoothProfile.INPUT_DEVICE);
}

if (BluetoothUuid.containsAnyUuid(uuids, PANU_PROFILE_UUIDS))
{
    profiles.add(BluetoothProfile.PAN);
}

第5步:逐个获取配置文件的代理:

for (int profile : profiles)
{
    if (!adapter.getProfileProxy(context, listener, profile))
    {
        //Do something
    }
}

第6步:对您在侦听器的onServiceConnected方法中检索到的每个代理执行任何操作。您可以使用relfection访问connect方法。

相关问题