This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Bluetooth automatic reconnection nRF51

Hi, The situation: nRF51 has established a connection with the iOS/Android (custom made)App. The nRF51 goes out of the Bluetooth range. Eventually, the Bluetooth connection is disconnected. The nRF51 comes into the Bluetooth range. The App reconnects with the nRF51 automatically. The nRF51 receives BLE_GAP_EVT_CONNECTED event and upon reception of this event, the nRF51 sends initial data to be populated in the characteristics of the App. The nRF51 keeps sending initial data to be populated until it receives the BLE_EVT_TX_COMPLETE event from the App (meaning the App has received the info sent by the nRF51). But, for some reason,(from the logs of the App), the App (tested with iOS) doesnot receive this initial data. If nRF51 sends out initial data continuously for 300 times in a row, then, the App receives the last 5 attempts(the 295 attempts are lost!). Instead of using the magic number 300, what is the proper way of populating the characteristics of the App upon reconnection? PS: If in the App, the connection is deliberately disconnected and reconnected, then, the App receives the initial data to be populated. The problem occurs only upon automatic reconnection.

  • Hi, how are you sending "initial data to be populated in the characteristics"? Are you using notifications? If so, did you check the return code from sd_ble_gatts_hvx()? Also, are you using bonding? If not, will the App enable notifications from the nRF51 on reconnect?

  • Hi Jorgen, Thanks for your reply. Yes I am using bonding- meaning, only if the app and the nRF51 are bonded ('pair'ed in iOS), the app can write data to the nRF51. Yes, I am using notifications and in the app, the notifications are checked to update the value of the characteristics. The App enables notifications upon reconnect, but by the time the app enables notifications, the initial data has already been sent to the app by the nRF51 (and nRF51 has got a BLE_EVT_TX_COMPLETE event in return). But, I am not checking the return code from sd_ble_gatts_hvx(). I am not checking the return code from sd_ble_gatts_hvx because I thought the Bluetooth stack itself would take care of sending multiple retries if the app has not acked the nRF51 regarding the initial(or any) data sent to the app. Please correct me if I am wrong.

  • If you are running sd_ble_gatts_hvx() 295 times before getting a BLE_EVT_TX_COMPLETE event, i suspect it must be returning an error code. Check the documentation here

  • Hi Jorgen, Sorry, I didn't explain it properly. Even when I send sd_ble_gatts_hvx() upon reconnection, I do get the BLE_EVT_TX_COMPLETE, but in the XCode logs of the (custom made)App, there is no indication that it has received a data from the nRF51. But when I keep sending the same data 300 times, then, the XCode logs of the app says that it has received 5 of the data (didn't receive all the 300). I think it is something to do with the way the app handles reconnection with Bluetooth. Because, when I close the app and reopen it again(which is a compete disconnection and a new connection - not reconnection), the XCode logs says that it has received data from the nRF51 (nRF51 sent the data only once upon new bluetooth connection). From nRF51 point of view, reconnection or new connection is the same - which is BLE_EVT_CONNECTED, so if the app can receive data from nRF51 upon a new conection, it has to receive data upon reconnection, as well, isn't it?

  • To clarify, do you have 300 BLE_EVT_TX_COMPLETE events (and 300 calls to sd_ble_gatts_hvx) on the nrf51 app, and only 5 packets received on your Ios/Android app?

Related