5

I am developing an app that needs to connect to a ble peripheral automatically.

I have a sticky service that does the following:

  1. looks for the required device in bonded devices
  2. if it doesn't fine the device (first time), scans for it and bonds to it by using device.createBond(), waits for the bond to finish by listening to the ACTION_BOND_STATE_CHANGED broadcast
  3. connects to it using device.connectGatt(ctx,true,callback)
  4. waits for onConnectionStateChange callback with connected state
  5. starts service discovery by using gatt.discoverServices()
  6. waits for onServicesDiscoverd callback
  7. enables notifications on a characteristic by writing a descriptor using gatt.writeDescriptor
  8. waits for onDescriptorWrite callback with success status BluetoothGatt.GATT_SUCCESS (0)
  9. does stuff with the notifications it receives

This all works fine for the first time. When the device disconnects (becomes out of range for instance, or turned off) the sticky service callbacks gatt.disconnect() and gatt.close(), restarts and does all this again, this time it uses the bonded device to connect.

Everything works fine until step 7, meaning I get a callback to onDescriptorWrite with status 133 sometimes followed by a connection state change callback with state 0 and status 22.

I could not find any info online for what status 133 or 22 mean.

Any idea why this is happening?

I am sort of working around it now by reacting to the bad onDescriptorWrite callback by removing the bond (reflection) an doing everything again with the freshly scanned device.

So basically I am using the bond just to wait for the device connection and then restart the whole thing.

This means a gatt connection for a bonded device is useless for writing the descriptor I need.

It feels like I am missing something, would love to know what.

EDIT: some relavant logcat output

08-18 16:06:31.363  12765-12835/? W/bt-att﹕ gatt_rsp_timeout disconnecting...  
08-18 16:06:31.363  12765-12835/? W/bt-btif﹕ bta_gattc_conn_cback() - cif=3     connected=0 conn_id=3 reason=0x0016  
08-18 16:06:31.363  12765-12835/? W/bt-btif﹕ bta_gattc_conn_cback() - cif=4     connected=0 conn_id=4 reason=0x0016  
08-18 16:06:31.363  12765-12835/? W/bt-btif﹕ bta_gattc_conn_cback() - cif=5     connected=0 conn_id=5 reason=0x0016  
08-18 16:06:31.366  12765-12807/? D/BtGatt.GattService﹕ onDisconnected() -     clientIf=5, connId=5, address=C1:D1:22:BA:F5:13  
here im getting onDescriptorWrite with status 133  
D/BluetoothGatt﹕ onClientConnectionState() - status=22 clientIf=5     device=C1:D1:22:BA:F5:13  

by the looks of this:
https://android.googlesource.com/platform/external/bluetooth/bluedroid/+/idea133/bta/include/bta_gatt_api.h#169

16 means BTA_GATT_CONN_TERMINATE_LOCAL_HOST
22 means BTA_GATT_CONN_LMP_TIMEOUT

asked this on nordics github:https://github.com/NordicSemiconductor/Android-nRF-Toolbox/issues/9#issuecomment-132191406

talarari
  • 539
  • 3
  • 7
  • 19
  • some more details, if i only save the ble device address and connect using bluetoothAdapter.getRemoteDevice(address) then i never have this problem. it is only when bonding, maybe the bond information is no longer valid after my ble device restarts? what info does the bond keep? – talarari Aug 24 '15 at 07:38
  • Do you find the solution? I also think delay is not good solution for solving it – Jame Dec 02 '16 at 11:22
  • Note that 22 is not BTA_GATT_CONN_LMP_TIMEOUT - there's a huge difference between 22 and 0x22. 22 = 0x16. – Charlie Monroe Sep 05 '17 at 17:44
  • hi i am having the same issue didn't find any solution yet.Got struck here my post link -> https://stackoverflow.com/q/47596419/4111151 – prasanthMurugan Dec 02 '17 at 04:50

1 Answers1

1

asked this on nordics github: https://github.com/NordicSemiconductor/Android-nRF-Toolbox/issues/9#issuecomment-132191406

following their recommendation added a 2 second delay after calling connectGatt and now it works.

talarari
  • 539
  • 3
  • 7
  • 19