Gatt communication works only first time it is used.
I have read a lot of issues related to this one but no solution helped.
Whole process:
1. Restart phone
2. Run the app
3. App connects to BLE device and fetches list of accessible Wifi networks (SdkGattAttributes.WIFI_CHAR_WIFI_LIST)
till now everything ok
4. restart app
5. App connects to device and tries to fetch wifi list but onCharacteristicRead is never received. No writeCharacteristic was sent prio this
6. After phone restart app is able to fetch wifi list but again only once
What can be wrong. Some resources freeing or what? I can post some code If needed.
Thanks in advance.
Android BLE: onCharacteristicRead works only first time
1.1k Views Asked by Matej Procházka At
2
There are 2 best solutions below
1
On
In the onCharacteristicWriteRequest do not forget to send response if needed:
if (responseNeeded) {
bluetoothGattServer?.sendResponse(device,
requestId,
BluetoothGatt.GATT_SUCCESS,
offset,
value)
}
Another possibility, if the data that you sent is not received at once (preparedWrite is true with specific offset), you need to handle it on the onExecuteWrite when data is received completely and send response.
bluetoothGattServer?.sendResponse(device,
requestId,
BluetoothGatt.GATT_SUCCESS,
0,
ByteArray(0))
I found the solution in the end so I post it here for others.
Problem was that after successful connection MTU was set
mBluetoothGatt.requestMtu(512)and services were requestedmBluetoothGatt.discoverServices()one after another which probably confused gatt.Solution: first request mtu and when finshed discover services