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.
Asked
Active
Viewed 1,069 times
2

Matej Procházka
- 217
- 2
- 14
-
I had a similar problem where I would connect to a device, call a characteristic, and then NOT do a disconnect. I would then try another connect and not be able to get my data from the same characteristic. Solution was to do an explicit disconnect after my read or write operation to the characteristic – Taylor Maxwell Nov 01 '18 at 18:13
-
Thanks for effort but it did not help. – Matej Procházka Nov 12 '18 at 09:52
2 Answers
2
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 requested mBluetoothGatt.discoverServices()
one after another which probably confused gatt.
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
if (newState == BluetoothProfile.STATE_CONNECTED) {
mBluetoothGatt.discoverServices();
mBluetoothGatt.requestMtu(512);
}
}
Solution: first request mtu and when finshed discover services
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
if (newState == BluetoothProfile.STATE_CONNECTED) {
mBluetoothGatt.requestMtu(512);
}
}
public void onMtuChanged (BluetoothGatt gatt, int mtu, int status) {
if (status == BluetoothGatt.GATT_SUCCESS) {
mBluetoothGatt.discoverServices();
}
}

Matej Procházka
- 217
- 2
- 14
0
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))

Seref Bulbul
- 1,163
- 12
- 14