I tried to look for complete examples that could help me with the development of the app but on the net I found only parts of code not linked to each other. Is it possible to see a complete example of connection including the communication part between the 2 devices? Also I happen to have a problem with the nRF Connect app: the bluetooth automatically disconnects from the device after about 30 seconds, why?
-
Generally BLE connections are closed after certain amount of time (in idle state) to conserve device battery. That maybe the reason behind 30 second disconnect. Also, for the sample app purposes, you can check [this app from Nordic](https://github.com/NordicSemiconductor/Android-nRF-Blinky) – dispatchMain Apr 15 '21 at 06:10
-
@dispatchMain the problem is that it does this even while communicating, I have seen the sample app but isn't there something simpler that shows all the steps? – Davide Torchia Apr 15 '21 at 06:19
-
ATT has defined timeout of 30 seconds for each operation. That could be reason of [disconnect during communication](https://stackoverflow.com/a/54919186/1799364). You may wanna sniff the BLE communication to see what error is causing the disconnection. Also, I could not find any good sample simpler than this. Maybe you could try [nRF Toolbox](https://github.com/NordicSemiconductor/Android-nRF-Toolbox). – dispatchMain Apr 15 '21 at 06:27
2 Answers
BLE complete example link to github - contains Android, iOS, ESP32 - 2 apps for each platform, Central and Peripheral. Each Central is compatible with each Peripheral.
This is a great guide: The Ultimate Guide to Android Bluetooth Low Energy by PunchThrough.
About your second question (that automatic disconnection occurs), I remember I've noticed similar disconnections on iOS, but didn't find out the reason. I also noticed that after subscribing to a characteristic notifications/indications it's not disconnecting anymore, that's why I guess it could be operating system optimization for the inactive connections.

- 747
- 6
- 15
Just an idea about the reason of unexpected disconnections: some Peripherals can be strict to connection parameters, and disconnect when the phone asks for changing them too many times.
In my case it was iPhone and Nordic nRF51822 (sample project from SDK 12.3.0, with modified settings to have low latency). Nordic (Peripheral) was disconnecting after about 30 seconds (4th time iPhone asked to change connection params), and it was a feature of that project, implemented this way:
static void conn_params_init(void)
{
ble_conn_params_init_t cp_init;
...
// allow to update 3 times max, disconnect on the 4th
cp_init.max_conn_params_update_count = 3;
...
}
// after about 30 seconds it was receiving this call
static void on_conn_params_evt(ble_conn_params_evt_t * p_evt)
{
...
if (p_evt->evt_type == BLE_CONN_PARAMS_EVT_FAILED)
{
// and disconneting right here
sd_ble_gap_disconnect(m_conn_handle, BLE_HCI_CONN_INTERVAL_UNACCEPTABLE);
}
}
So that time I fixed it on Periperal side by ignoring event BLE_CONN_PARAMS_EVT_FAILED
and not calling sd_ble_gap_disconnect
, but proper setup of those connection parameters would be a better fix.

- 747
- 6
- 15