获取android 3.1及更高版本中已连接的蓝牙低功耗设备的列表 [英] Obtaining list of connected bluetooth low energy devices in android 3.1 and later
问题描述
我正在研究显示某些已连接的蓝牙低能耗设备列表的应用程序,因此用户可以选择要配置的设备之一.
I'm working on application which shows list of certain connected bluetooth low energy devices, so user can choose which one of them he wants to configure.
问题在于,您不能仅列出所有已连接的设备.据我所知,有三种可能的方法:
bluetoothManager.getConnectedDevices(BluetoothProfile.GATT_SERVER);
此操作失败,因为设备连接时android不会连接到GATT服务器,因此设备既不在GATT_SERVER也不在GATT配置文件下.但是,一旦我调用connectGatt方法,
This fails because android won't connect to GATT server, when device connects, so device is neither under GATT_SERVER nor GATT profile. However once I call connectGatt method,
bluetoothDevice.connectGatt(getApplicationContext(), false, gattCallback);
设备可以在GATT_SERVER和GATT配置文件下找到.低能耗设备不支持其他配置文件.
device can be found under both GATT_SERVER and GATT profile. Other profiles are not supported by low energy devices.
列出已配对的设备,并在每个设备上尝试使用connectGatt
List paired devices and try connectGatt on each of them
List<BluetoothDevice> connectedDevices = new ArrayList<BluetoothDevice>();
for(BluetoothDevice device : bluetoothAdapter.getBondedDevices()) {
BluetoothGatt gatt = device.connectGatt(getApplicationContext(), false, gattCallback);
if(gatt != null) {
connectedDevices.add(device);
}
gatt.disconnect();
}
无法使用此方法,因为它无法确定设备是否已连接或仅在范围内但未连接
This method cannot be used as it cannot determine if device is already connected or only in range but not connected
在系统启动启动服务上,监听 ACL_CONNECTED和ACL_DISCONNECTED 目的和维护已连接设备的列表
On system boot start service listening to ACL_CONNECTED and ACL_DISCONNECTED intents and maintaining list of connected devices
清单
<service android:name=".ManagerService" android:enabled="true" />
<receiver
android:name=".BootFinishedReceiver"
android:directBootAware="true"
android:enabled="true"
android:exported="false"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.LOCKED_BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
接收器
public class BootFinishedReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent serviceIntent = new Intent(context, ManagerService.class);
context.startService(serviceIntent);
}
}
服务
public class ManagerService extends Service {
private static List<BluetoothDevice> connectedDevices;
@Override
public void onCreate() {
connectedDevices = new ArrayList<BluetoothDevice>();
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
IntentFilter filter = new IntentFilter();
filter.addAction(BluetoothDevice.ACTION_ACL_CONNECTED);
filter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED);
registerReceiver(connectionReceiver, filter);
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
unregisterReceiver(connectionReceiver);
super.onDestroy();
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
private final BroadcastReceiver connectionReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
if(BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) {
connectedDevices.add(device);
}else{
connectedDevices.remove(device);
}
}
};
public static List<BluetoothDevice> getConnectedDevices() {
return connectedDevices;
}
}
因为3.1应用程序在活动开始之前不再能够接收系统意图,因此也无法使用.
还有其他方法或如何在更高的android版本中实现它?
谢谢您的建议
推荐答案
Well, I found out that you can still use ON_BOOT_COMPLETED, but you have to allow it in setting on your device. So my problem is solved
这篇关于获取android 3.1及更高版本中已连接的蓝牙低功耗设备的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!