0

I'm working with android BLE. in this task android application should connect to multiple BLE devices and it's ok. I can connect successfully.

I know this code is need to connecting BLE device:

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

I using Map<String, BluetoothGatt> connectedDeviceMap to keep BLE devices addresses and BluetoothGatt:

    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);
            }
        }
    }

as you can see for getting gatt value for each device I need to call connectGatt function. but when this function call multiple times, devices disconnecting from the application (i think this is because fulling memory of BLE)

so i using close() to release all memory and connect again :

 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();
        }
    }
}

but this not working and also devices disconecting from application when calling multiple times connectGatt.

Pooya Chavoshi
  • 346
  • 4
  • 12

1 Answers1

0

Like in this example Android BLE multiple connections. If I see correctly diff between your solution and this in link, is you don't check device connection state

Konrad Sikora
  • 11
  • 1
  • 3