AndroidBLE应用只能为1个BLE设备订阅特征通知
创始人
2024-10-06 12:30:37
0

可以使用SparseArray来存储多个设备的Gatt对象,在订阅通知时使用该设备的Gatt对象。示例代码如下:

private SparseArray mGattDevices = new SparseArray<>();

// 连接BLE设备 public void connect(final String address) { final BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address); if (device == null) { Log.e(TAG, "Device not found!"); return; } mGattDevices.clear(); mGatt = device.connectGatt(this, false, mGattCallback); mGattDevices.put(0, mGatt); }

// 订阅通知 public void subscribe(final int deviceIndex, final UUID serviceUuid, final UUID characteristicUuid) { final BluetoothGatt gatt = mGattDevices.get(deviceIndex); if (gatt == null) { Log.e(TAG, "Gatt not found!"); return; } final BluetoothGattService service = gatt.getService(serviceUuid); if (service == null) { Log.e(TAG, "Service not found!"); return; } final BluetoothGattCharacteristic characteristic = service.getCharacteristic(characteristicUuid); if (characteristic == null) { Log.e(TAG, "Characteristic not found!"); return; } gatt.setCharacteristicNotification(characteristic, true); final BluetoothGattDescriptor descriptor = characteristic.getDescriptor(UUID.fromString(CLIENT_CHARACTERISTIC_CONFIG)); descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE); gatt.writeDescriptor(descriptor); }

相关内容

热门资讯

透视必备!黑侠破解wepoke... 透视必备!黑侠破解wepoker(透视)开挂辅助方法(原来真的是有挂)-哔哩哔哩黑侠破解wepoke...
透视辅助!wepoker底牌透... 透视辅助!wepoker底牌透视,wpk透视是真的吗,资料教程(好像是真的挂)-哔哩哔哩1、实时wp...
透视神器!wepoker破解是... 透视神器!wepoker破解是真的还是假的(透视)开挂辅助方法(原来真的是有挂)-哔哩哔哩进入游戏-...
透视app!sohoo竞技联盟... 透视app!sohoo竞技联盟辅助器,wepoker透视脚本免费使用视频,教程书教程(本来存在有挂)...
透视胜率!we poker辅助... 透视胜率!we poker辅助器v3.3(透视)开挂辅助方法(本来存在有挂)-哔哩哔哩一、we po...
透视总结!wpk真的有透视嘛,... 透视总结!wpk真的有透视嘛,epoker透视,方案教程(原来是真的挂)-哔哩哔哩1、上手简单,内置...
透视私人局!德普之星透视(透视... 透视私人局!德普之星透视(透视)开挂辅助软件(一直真的有挂)-哔哩哔哩进入游戏-大厅左侧-新手福利-...
透视挂透视!约局吧开挂神器是真... 透视挂透视!约局吧开挂神器是真的吗,hhpoker有没有辅助辅助,法门教程(确实是真的挂)-哔哩哔哩...
透视程序!德普之星私人局辅助免... 透视程序!德普之星私人局辅助免费(透视)开挂辅助脚本(竟然真的有挂)-哔哩哔哩1、全新机制【德普之星...
透视线上!约局吧怎么看有没有挂... 透视线上!约局吧怎么看有没有挂,约局吧能不能开挂,资料教程(果然是真的挂)-哔哩哔哩1)约局吧怎么看...