在 Android 中,如何获取已连接蓝牙设备的配置文件? [英] In Android, how to get the profile of a connected bluetooth device?

查看:15
本文介绍了在 Android 中,如何获取已连接蓝牙设备的配置文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 know which device supports which profile. I want to be able to pick the devices based on the profile, for example, get a list of currently connected devices and their profile, and pick one of them. I don't see how I can get such info if I have the instance of BluetoothDevice.

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

On this page there are some codes illustrating how to work with a bluetooth headset profile: http://developer.android.com/guide/topics/connectivity/bluetooth.html#Profiles. But it doesn't solve my problem. If you think I am missing anything, please help me and point it out.

非常感谢.

推荐答案

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

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.

例如,如果要查找哪些BluetoothDevice支持A2DP,首先创建一个自定义BluetoothProfile.ServiceListener

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<BluetoothDevice> Devices=bluetoothProfile.getDevicesMatchingConnectionStates(states);
    for (BluetoothDevice loop:Devices){
        Log.i("myTag",loop.getName());
    }
}

@Override
public void onServiceDisconnected(int profile) {
}

}

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

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

这将记录所有支持 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.

这篇关于在 Android 中,如何获取已连接蓝牙设备的配置文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆