BLE firmware Development--How to get the current connection status

Source: Internet
Author: User

In a BLE development project, I use TI's CC2541 bluetooth ble SoC, in order to prevent the master-slave device connection disconnection data loss, the hardware added an EEPROM, when the Bluetooth connection, through the notify to the host, but when the Bluetooth connection is disconnected, The collected data is stored in the EEPROM (I²C), in order to achieve this, you need to get the Bluetooth connection status in real time.

In TI's ble protocol stack, periperal.h has the following definitions:

/** * GAP Peripheral Role states.                        */typedef enum{gaprole_init = 0,//!< waiting to be started gaprole_started, !< Started but not advertising gaprole_advertising,//!< currently advertising Gaprole_ Waiting,//!< Device is started and not advertising, was in waiting period before advertising Aga  In Gaprole_waiting_after_timeout,//!<-Device just timed out from a connection but isn't yet advertising, is In waiting period before advertising again gaprole_connected,//!< in a connection gaprole_conn Ected_adv,//!< in a connection + advertising gaprole_error//!< ERROR o Ccurred-invalid State} gaprole_states_t; 
The above enumerates several states from the device, so how to get the current state of gaprole_states_t in real time? A definition is given later in the file:

/** * Callback structure-must be setup by the application and used when Gaproles_startdevice () is called. */typedef struct{  gaprolesstatenotify_t    pfnstatechange;  !< whenever the device changes state  gaprolesrssiread_t       Pfnrssiread;     !< when a valid RSSI was read from controller} gaprolescbs_t;
The annotations above the struct are written clearly and must be set at the application layer and used after the Gaproles_startdevice () call. Here the Pfnstatechange member variable is a callback function pointer to handle the action under the corresponding state change. So grab this and get the change in the connection state from the device in real time. So then we just need to define a Pfnstatechange callback function in the application layer and use it to initialize a gaprolescbs_t struct.

The callback function is defined as follows: (Glinkstatus is a custom variable that is used to indicate the state of the connection)

static void Peripheralstatenotificationcb (gaprole_states_t newstate) {switch (newstate) {case gaprole_started:        {uint8 Ownaddress[b_addr_len];               Uint8 Systemid[devinfo_system_id_len];        Gaprole_getparameter (GAPROLE_BD_ADDR, ownaddress);        Use 6 bytes of the device address for 8 bytes of system ID value systemid[0] = ownaddress[0];        SYSTEMID[1] = ownaddress[1];        SYSTEMID[2] = ownaddress[2];        SYSTEMID[3] = ownaddress[3];        SYSTEMID[4] = ownaddress[4];        SYSTEMID[5] = ownaddress[5];        Devinfo_setparameter (devinfo_system_id, Devinfo_system_id_len, SystemID);      glinkstatus=0;    } break;      Case gaprole_advertising:glinkstatus=0;    Break      Case Gaprole_connected:glinkstatus=1;    Break      Case gaprole_connected_adv:glinkstatus=0;                Break      Case gaprole_waiting:glinkstatus=0;    Break   Case gaprole_waiting_after_timeout:glinkstatus=0;   Break      Case gaprole_error:glinkstatus=0;    Break  Default:break; }}
The callback function is then used to initialize a gaprolescbs_t structure:

GAP Role callbacksstatic gaprolescbs_t temperaturesensor_peripheralcbs ={  PERIPHERALSTATENOTIFICATIONCB,  Profile state change callbacks  NULL                            /When a valid RSSI are read from controller (not used by application)};
In this way, after the Gaproles_startdevice () call (called in the application-level task handler), the current connection state can be obtained in real time via a custom glinkstatus variable.

BLE firmware Development--How to get the current connection status

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.