微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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

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

非常感谢提前.

解决方法:

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

例如,如果要查找哪个BluetoothDevices支持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);

这将记录所有支持A2DP的蓝牙设备,这些设备处于请求状态.在此示例中,它包括当前连接的所有设备和先前已断开连接的配对设备.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐