简体   繁体   English

如何从特定的蓝牙配置文件中获取连接的设备列表

[英]How to get the connected device list from a specific bluetooth profile

Is there anyway to get the connected devices list from support profiles (HDD,Spp and audio). 无论如何从支持配置文件(HDD,Spp和音频)获取连接的设备列表。 The requirement is like my device will support HDD,SPP and Audio, so i have to filter the devices which supports all these profiles. 要求就像我的设备将支持HDD,SPP和音频,所以我必须过滤支持所有这些配置文件的设备。 Is there anyway to filter the devices? 反正过滤设备了吗?

Yes that is possible but your Android application must target SDK 11 or later ( Android 3.0.X ). 是的,但您的Android应用程序必须以SDK 11或更高版本( Android 3.0.X )为目标。

The solution to your question is that you have to query all BluetoothDevices known by your Android device. 您的问题的解决方案是您必须查询Android设备已知的所有BluetoothDevices。 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. 我们稍后会过滤掉未连接的设备,因为您只需要当前连接的设备。


  • First you need to retrieve the BluetoothAdapter : 首先,您需要检索BluetoothAdapter

final BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter(); 最终的BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();

  • Second you need to make sure Bluetooth is available and turned on : 其次,您需要确保蓝牙可用并打开:

if (btAdapter != null && btAdapter.isEnabled()) // null means no Bluetooth! if(btAdapter!= null && btAdapter.isEnabled())// null表示没有蓝牙!

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 如果未关闭蓝牙,您可以使用文档中不推荐的btAdapter.enable()或要求用户执行此操作: 在Android上编程方式启用蓝牙

  • Third you need to define an array of states (to filter out unconnected devices): 第三,你需要定义一个状态数组(过滤掉未连接的设备):

final int[] states = new int[] {BluetoothProfile.STATE_CONNECTED, BluetoothProfile.STATE_CONNECTING}; final int [] states = new int [] {BluetoothProfile.STATE_CONNECTED,BluetoothProfile.STATE_CONNECTING};

  • Fourth, you create a BluetoothProfile.ServiceListener which contains two callbacks triggered when a service is connected and disconnected : 第四,创建一个BluetoothProfile.ServiceListener ,它包含连接和断开服务时触发的两个回调:

     final BluetoothProfile.ServiceListener listener = new BluetoothProfile.ServiceListener() { @Override public void onServiceConnected(int profile, BluetoothProfile proxy) { } @Override public void onServiceDisconnected(int profile) { } }; 

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 : 现在,由于您必须重复Android SDK( A2Dp,GATT,GATT_SERVER,手机,运行状况,SAP )中所有可用蓝牙配置文件的查询过程,您应该按照以下步骤操作:

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: onServiceConnected ,放置一个条件来检查当前配置文件是什么,以便我们将找到的设备添加到正确的集合中,我们使用: proxy.getDevicesMatchingConnectionStates(states)来过滤掉未连接的设备:

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 !

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM