Android BLE多台设备连接后断开



我正在使用android BLE。在这个任务中,android应用程序应该连接到多个BLE设备,这是可以的。我可以成功连接。

我知道这段代码是需要连接BLE设备的:

BluetoothGatt gaat = device.connectGatt(getApplicationContext(), false, gattCallback);

I使用Map<String, BluetoothGatt> connectedDeviceMap保持BLE器件addressesBluetoothGatt:

List<BluetoothDevice> devices = bluetoothManager.getConnectedDevices(BluetoothProfile.GATT);
if (devices != null && !(devices.isEmpty())) {
for (BluetoothDevice device : devices) {
if (device.getType() == BluetoothDevice.DEVICE_TYPE_LE) {
BluetoothDevice d = bluetoothAdapter.getRemoteDevice(device.getAddress());
gatt = d.connectGatt(getApplicationContext(), false, gattCallback);
connectedDeviceMap.put(d.getAddress(), gatt);
}
}
}

正如您所看到的,为了获得每个设备的gatt值,我需要调用connectGatt函数。但是当这个函数调用多次,设备从应用程序断开(我认为这是因为满内存的BLE)

so i使用close()释放所有内存并再次连接:

List<BluetoothDevice> devices = bluetoothManager.getConnectedDevices(BluetoothProfile.GATT);
if (devices != null && !(devices.isEmpty())) {
for (BluetoothDevice device : devices) {
if (device.getType() == BluetoothDevice.DEVICE_TYPE_LE) {
BluetoothDevice d = bluetoothAdapter.getRemoteDevice(device.getAddress());
gatt = d.connectGatt(getApplicationContext(), false, gattCallback);
connectedDeviceMap.put(d.getAddress(), gatt);
gatt .close();
}
}
}

但这不起作用,并且当多次调用connectGatt时设备也从应用程序断开。

就像这个例子中Android BLE多连接。如果我没看错的话,你的解决方案和链接中的不同之处在于,你没有检查设备连接状态

最新更新