I have an android application that has BLE GATT Server running so the android phone would be a peripheral device and other devices can connect to it.
Sometimes the connections works fine, but very often when I try to pair the external device to the phone I got these errors:
aclStateChangeCallback: device is NULL, address=81:6C:0F:1A:6C:37, newState=0
bt_btm: btm_ble_read_remote_features_complete: failed for handle: 0x0010, status 0x1a
bt_stack: [ERROR:gatt_db.cc(93)] gatts_check_attr_readability: GATT_READ_NOT_PERMIT
After pairing, I am connected to the device, 1 characteristic is written and just before the 2nd characteristic is read the device is disconnected. Always the time before disconnect is 9 seconds after connection is established.
Absolutely the same device and application are sometimes paired and work correct, sometimes are not.. Do you have any idea what might be causing the problem?
Here is some more information from logcat:
2019-03-18 18:48:29.132 20305-20329/? E/BluetoothRemoteDevices: aclStateChangeCallback: device is NULL, address=81:6C:0F:1A:6C:37, newState=0
2019-03-18 18:48:29.134 21813-21831/com.test I/GattServerCallback: BluetoothDevice CONNECTED: 81:6C:0F:1A:6C:37, state: 0, newState: 2
2019-03-18 18:48:29.167 20305-20357/? E/bt_btm: btm_ble_read_remote_features_complete: failed for handle: 0x0010, status 0x1a
2019-03-18 18:48:32.629 21813-21813/com.test D/TestApp: getCharacteristicValue characteristic: 3291dcea-0189-3A9c-b193-26ec13029190
2019-03-18 18:48:32.667 20305-20357/? E/bt_stack: [ERROR:gatt_db.cc(93)] gatts_check_attr_readability: GATT_READ_NOT_PERMIT
2019-03-18 18:48:38.867 20305-20357/? I/bt_btm_sec: btm_sec_disconnected clearing pending flag handle:10 reason:8
2019-03-18 18:48:38.870 20305-20329/? W/bt_btif: btif_av_move_idle: ACL Disconnected state 0 bd_addr=81:6C:0F:1A:6C:37 peer_bda=00:00:00:00:00:00
2019-03-18 18:48:38.871 21813-21831/com.test D/BluetoothGattServer: onServerConnectionState() - status=0 serverIf=7 device=81:6C:0F:1A:6C:37
2019-03-18 18:48:38.871 21813-21831/com.test D/GattServerCallback: onDeviceDisconnect
2019-03-18 18:48:38.871 20305-20329/? E/BluetoothRemoteDevices: aclStateChangeCallback: device is NULL, address=81:6C:0F:1A:6C:37, newState=1
2019-03-18 18:48:38.883 21813-21831/com.test I/GattServerCallback: BluetoothDevice DISCONNECTED: 81:6C:0F:1A:6C:37, state: 0, newState: 0
Here is a picture of the HCI log:
Frame 37885 is the disconnected event. The message is Connection Timeout. Frame 37883 is the GATT_READ_NOT_PERMIT event. So it seems that communication stops after the GATT_READ_NOT_PERMIT event.
Here is an image of the correct flow when everything is working fine. The difference is that the frame with handle id = 3(Service Changed) is missing. So I guess this might be related to this event - Service Changed.. I am not sure when this is triggered and if this is requested by the central device but it is always after the 1st read of characteristics.
This is the log with the Disconnect message: