When compared to the old iWRAP-based Bluetooth Classic modules, the firmware of the newer BT121 lacks a command to configure automatic re-connections to one or more remote devices, already bonded or yet to be bonded.
Luckily this is not a problem, as such functionality can be easily implemented with a BGScript.
Actually with a BGScript you can design the functionality as you wish, so to match as closely as possible the requirements of your unique application.
And even when module is running in hosted (aka Network Co-Processor or NCP) mode, where an host MCU controls the module over the UART interface using the BGAPI protocol, it might be desirable to delegate to a BGScript the automatic re-connections.
In this knowledge-base article we will post a few simple example scripts showing some ways to implement an automatic re-connection feature.
The first example of the series is a simple script (called autocall.bgs) which allows the module to automatically re-connect to the last connected device, should the connection with it be closed due to normal close by remote side or due to supervision timeout (out-of-range or sudden power off of remote device.)
The comments in the script should explain well the parts of the script itself, but an high level description of it follows:
- Module keeps track of the last connected device by storing into a buffer its remote mac address which is carried in the event bt_rfcomm_opened
- In the same event above, the RFCOMM endpoint of the connection is also stored into a variable
- In case it is this very last connection to be closed next (as recognized via the endpoint), then its RFCOMM endpoint will be explicitly closed and a timer started. The timeout interval of such timer is defined by the user in a constant and it refers to the delay before attempt to connect again after disconnection, and it also refers to the interval between attempts should any of the following multiple attempts to reconnect fail for any reason. Also the max number of attempts is configurable via a constant
- During the time the (multiple) attempt(s) to reconnect is(are) ongoing the module is made not connectable
- When timer elapses the command bt_rfcomm_open is launched to try to re-establish the connection to the intended device
- As soon as the connection is successfully established any further attempt is stopped, that is, timer is not started anymore since the event endpoint_closing will not be triggered by a page timeout. If instead a re-connection attempt fails, the timer will be launched again by the event endpoint_closing which will be triggered by a page timeout. And a new connection attempts by the bt_rfcomm_open will consequently follow. This will continue forever, or for as many attempts as defined by the user, depending on the constant that is used to define the max number of attempts (0 meaning forever)
- When a number of attempts corresponding to the max number of attempts is reached, module is made connectable again and any re-connection is stopped until a connection is opened again and eventually closed. If the script is supposed to try to re-connect forever only a reset will stop this, unless the intended original device will become connected again.
Some notes about the script:
- Certain commands like the bt_gap_set_mode are launched multiple times even when this is a redundant action, but this is not a problem for the firmware, especially when the task here is to keep the script simple for easier understanding.
- Re-connections will not happen when it is this side to close the connection, since the event endpoint_closing is not triggered in this case. However any disconnection by this side is not performed by the script but by the host which would be then in charge to reconnect as well.
- This script was tested with, and can be easily added to, the example under the directory \example\bt121\ of the SDK by simply adding into the project.xml the following lines
<script in="autocall.bgs" />
- This example script or any derivative can also be easily added to an existing BGSCript with the "import" directive.