如何从特定的蓝牙配置文件获取连接的设备列表 [英] How to get the connected device list from a specific bluetooth profile
问题描述
无论如何,都可以从支持配置文件(HDD,Spp和音频)中获取已连接的设备列表.要求就像我的设备将支持HDD,SPP和音频,因此我必须过滤支持所有这些配置文件的设备.反正有过滤设备吗?
Is there anyway to get the connected devices list from support profiles (HDD,Spp and audio). The requirement is like my device will support HDD,SPP and Audio, so i have to filter the devices which supports all these profiles. Is there anyway to filter the devices?
推荐答案
可以,但是您的Android应用程序必须以SDK 11或更高版本为目标(
Yes that is possible but your Android application must target SDK 11 or later (Android 3.0.X).
问题的解决方案是,您必须查询Android设备已知的所有BluetoothDevices. 已知是指所有配对的已连接或未连接的设备以及未配对的已连接设备.
The solution to your question is that you have to query all BluetoothDevices known by your Android device. By known I mean all paired connected or unconnected devices and unpaired connected devices.
我们稍后将过滤掉未连接的设备,因为您只需要当前已连接的设备.
We will filter out unconnected devices later since you only want currently connected devices.
- 首先,您需要检索
BluetoothAdapter
:
最终BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();
final BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();
- 第二,您需要确保蓝牙可用并打开:
- 第三,您需要定义一个状态数组(以过滤掉 未连接的设备):
- Third you need to define an array of states (to filter out unconnected devices):
-
第四,您创建一个
BluetoothProfile.ServiceListener
包含两个在连接服务时触发的回调,以及 断开连接:Fourth, you create a
BluetoothProfile.ServiceListener
which contains two callbacks triggered when a service is connected and disconnected :final BluetoothProfile.ServiceListener listener = new BluetoothProfile.ServiceListener() { @Override public void onServiceConnected(int profile, BluetoothProfile proxy) { } @Override public void onServiceDisconnected(int profile) { } };
现在,因为您必须对Android SDK中所有可用的蓝牙配置文件重复查询过程(
Now since you have to repeat the querying process for all available Bluetooth Profiles in the Android SDK (A2Dp, GATT, GATT_SERVER, Handset, Health, SAP) you should proceed as follow :
在
onServiceConnected
中,放置一个条件以检查当前配置文件是什么,以便将找到的设备添加到正确的集合中,并使用:proxy.getDevicesMatchingConnectionStates(states)
筛选出未连接的设备:In
onServiceConnected
, place a condition that check what is the current profile so that we add the found devices into the correct collection and we use :proxy.getDevicesMatchingConnectionStates(states)
to filter out unconnected devices:switch (profile) { case BluetoothProfile.A2DP: ad2dpDevices.addAll(proxy.getDevicesMatchingConnectionStates(states)); break; case BluetoothProfile.GATT: // NOTE ! Requires SDK 18 ! gattDevices.addAll(proxy.getDevicesMatchingConnectionStates(states)); break; case BluetoothProfile.GATT_SERVER: // NOTE ! Requires SDK 18 ! gattServerDevices.addAll(proxy.getDevicesMatchingConnectionStates(states)); break; case BluetoothProfile.HEADSET: headsetDevices.addAll(proxy.getDevicesMatchingConnectionStates(states)); break; case BluetoothProfile.HEALTH: // NOTE ! Requires SDK 14 ! healthDevices.addAll(proxy.getDevicesMatchingConnectionStates(states)); break; case BluetoothProfile.SAP: // NOTE ! Requires SDK 23 ! sapDevices.addAll(proxy.getDevicesMatchingConnectionStates(states)); break; }
最后,最后要做的就是开始查询过程:
And finally, the last thing to do is start the querying process :
btAdapter.getProfileProxy(yourContext, listener, BluetoothProfile.A2DP); btAdapter.getProfileProxy(yourContext, listener, BluetoothProfile.GATT); // NOTE ! Requires SDK 18 ! btAdapter.getProfileProxy(yourContext, listener, BluetoothProfile.GATT_SERVER); // NOTE ! Requires SDK 18 ! btAdapter.getProfileProxy(yourContext, listener, BluetoothProfile.HEADSET); btAdapter.getProfileProxy(yourContext, listener, BluetoothProfile.HEALTH); // NOTE ! Requires SDK 14 ! btAdapter.getProfileProxy(yourContext, listener, BluetoothProfile.SAP); // NOTE ! Requires SDK 23 !
这篇关于如何从特定的蓝牙配置文件获取连接的设备列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
if(btAdapter!= null&& btAdapter.isEnabled())//null表示否 蓝牙!
if (btAdapter != null && btAdapter.isEnabled()) // null means no Bluetooth!
如果未关闭蓝牙,则可以使用文档中不推荐的btAdapter.enable()
或要求用户这样做:
If the Bluetooth is not turned out you can either use btAdapter.enable()
which is not recommended in the documentation or ask the user to do it : Programmatically enabling bluetooth on Android
最终的int []状态=新的int [] {BluetoothProfile.STATE_CONNECTED, BluetoothProfile.STATE_CONNECTING};
final int[] states = new int[] {BluetoothProfile.STATE_CONNECTED, BluetoothProfile.STATE_CONNECTING};