Hello everyone!
I'm making an application where the peripheral can establish a connection to the central device (a mobile phone). When there is data (e.g, temperature change), the peripheral will start advertising and establish a connection with the central device. Since the peripheral is powered from the battery, I would like to implement a connection timeout from the peripheral during connection with the central device. In particular, after connection is established and data is exchanged, within 5 seconds the peripheral can decide itself to terminate the connection. Could you please advice me how this procedure can be done in SDK5.0.4.
Thank you!
Device:
Hi quangng,
To start with, you wont be able to send a connection request from a peripheral, only a central can send a connection request. So you can have the device in permanent sleep and wake it up via an interrupt, for example when a sensor has data. After that the device should start advertising in order for the central to locate the device. When the central tracks the device and issues a connection request the user_app_connection() will be triggered and in that function you will be able to start a kernel timer that will define how long your connection with the peripheral will last in order to send your notifications or do whatever your application would do (in order to send data from the peripheral you will have to use notifications which means that you will have to activate the notifications of the peripheral from your central). When the timer elapses the callback of the timer will be invoked, and from that point you can invoke the app_easy_gap_disconnect() which will terminate the connection with the central. After the disconnection the user_app_disconnect() will be invoked and from there you can set again the wakeup interrupt and fallback to sleep.
Thanks MT_dialog
Hi MT_dialog,
Today I got it working by following your instructions. Thank you! However, there is still one thing to be improved. The timeout for my peripheral - central connection is 5s. When the central disconnects the connection in less than 5s (e.g, the central disconnects the connection after 2s of established connection), the connection can be successfully done. After the disconnection about 3s, the kernel timer triggers the callback function that I've already set in user_app_connection(). Well... that can be expected since I did not cancel the kernel timer in user_app_disconnect(). If I cancel the kernel timer using app_easy_timer_cancel() when the timer has already expired, the application will hang at ASSERT_WARNING(0) (line 395 in SDK5.0.4) in app_easy_timer_cancel(). If I understood correctly, the application hangs there because I cancel an inactive kernel timer. Then is there any API call to check if the kernel timer with timer id (e.g, conn_timeout_id) is active so that we can cancel it properly?
Hi quangng,
The app_easy_timer() returns a timer_id when the timer is created, so in the disconnection callback you can cancel the timer, if this occurs before the time elapses. Also in the callback of the timer you dont have to cancel the timer, since the timer has elapses the timer message has left the execution queue.
Thanks MT_dialog