bt_gatt_client_conn_state_get()
Description
Function requests and gets BLE connection state.
Syntax
bt_gatt_client_conn_state_get(connection_id, conn_state, conn_err_code, conn_role, security_level, mac_addr_type, mac_addr[]);
Parameters
Variable | Input |
|---|---|
| Connection id which was described in |
| A variable onto which connection state will be written. Possible connection states: BT_CONN_STATE_DISCONNECTED (value: 0) |
| A variable onto which last known ATT error code will be stored. |
| A variable onto which connection role will be stored. Can be 0 - Central, 1 - Peripheral. |
| A variable onto which connection security level will be stored. Possible security levels: BT_SECURITY_L0 (value: 0) |
| A variable onto which connected peer device MAC address type will be stored. Possible values: BT_ADDR_PUBLIC (value: 0) |
| Array into which connected peer device MAC address will be stored. |
Returns
Return value | Return explanation |
SCRIPT_OPERATION_SUCCESS | Successfully fetched connection state. |
SCRIPT_OPERATION_FAILED | Failed to get connection state. |
SCRIPT_NO_ACCESS | Bluetooth GATT client is not initialized. |
SCRIPT_PARAM_INVALID | Incorrect parameters were passed into the function. |
Function call example
#include <io>
#include <read>
#include <float>
#include <string>
#include <core>
#include <write>
#include <define>
#include <socket>
#include <bluetooth>
#define FIRST_CONNECTION 0
#define SECOND_CONNECTION 1
new peer_mac_address[] = [0xFF, 0x48, 0x80, 0x33, 0xE3, 0xA8];
new connection_state = 0, connection_err_code = 0, connection_role_ptr = 0, connection_security_lev = 0, connection_addr_type = 0;
new connection_addr[6];
main()
{
bt_init();
bt_gatt_client_init();
bt_gatt_client_connect(FIRST_CONNECTION, BT_ADDR_RANDOM, peer_mac_address);
bt_gatt_client_conn_state_get(FIRST_CONNECTION, conn_state, connection_err_code, connection_role_ptr,
connection_security_lev, connection_addr_type, connection_addr);
for(;;)
{
Delay(100);
}
}
Debug example
This example initializes BLE, then initializes GATT Client module, connects to peer device, gets connection state, disconnects and gets connection state again. Example uses debug_print function to print information log to Initialized peripheral.
#include <io>
#include <read>
#include <float>
#include <string>
#include <core>
#include <write>
#include <define>
#include <socket>
#include <bluetooth>
#include <debug>
#define RS232_SPEED 115200
#define WORD_LENGTH WORDLENGTH_8
#define STOP_BITS STOPBITS_1
#define PARITY PARITY_NONE
#define FIRST_CONNECTION 0
#define SECOND_CONNECTION 1
new peer_mac_address[] = [0xFF, 0x48, 0x80, 0x33, 0xE3, 0xA8];
new return_code = SCRIPT_OPERATION_SUCCESS;
new connection_state = 0, connection_err_code = 0, connection_role_ptr = 0, connection_security_lev = 0, connection_addr_type = 0;
new connection_addr[6];
connection_state_print(conn_state)
{
switch(conn_state)
{
case BT_CONN_STATE_DISCONNECTED:
{
debug_print("Connection state: Not connected\r\n");
}
case BT_CONN_STATE_CONNECTING:
{
debug_print("Connection state: Connecting\r\n");
}
case BT_CONN_STATE_CONNECTED:
{
debug_print("Connection state: Connected\r\n");
}
case BT_CONN_STATE_DISCONNECTING:
{
debug_print("Connection state: Disconnecting\r\n");
}
}
}
main()
{
Init(RS232, RS232_SPEED, WORD_LENGTH, STOP_BITS, PARITY);
debug_init(RS232);
return_code = bt_init();
if (return_code != SCRIPT_OPERATION_SUCCESS)
{
// Handle failed bt_init here
debug_print("bt_init failed\r\n");
}
else
{
debug_print("bt_init succeeded\r\n");
}
return_code = bt_gatt_client_init();
if (return_code != SCRIPT_OPERATION_SUCCESS)
{
// Handle failed bt_gatt_client_init here
debug_print("bt_gatt_client_init failed\r\n");
}
else
{
debug_print("bt_gatt_client_init succeeded\r\n");
}
debug_print("Connecting to a device with this MAC address: %X:%X:%X:%X:%X:%X\r\n", peer_mac_address[0],
peer_mac_address[1], peer_mac_address[2], peer_mac_address[3], peer_mac_address[4], peer_mac_address[5]);
while (bt_gatt_client_connect(FIRST_CONNECTION, BT_ADDR_RANDOM, peer_mac_address) != SCRIPT_OPERATION_SUCCESS)
{
debug_print("bt_gatt_client_connect time-out\r\n");
Delay(100);
}
debug_print("Connected successfully\r\n");
return_code = bt_gatt_client_conn_state_get(FIRST_CONNECTION, conn_state, connection_err_code,
connection_role_ptr, connection_security_lev, connection_addr_type,
connection_addr);
if (return_code != SCRIPT_OPERATION_SUCCESS)
{
// Handle failed bt_gatt_client_conn_state_get here
debug_print("bt_gatt_client_conn_state_get failed\r\n");
}
else
{
connection_state_print(connection_state);
debug_print("bt_gatt_client_conn_state_get succeeded\r\n");
}
return_code = bt_gatt_client_disconnect(FIRST_CONNECTION);
if (return_code != SCRIPT_OPERATION_SUCCESS)
{
// Handle failed bt_gatt_client_disconnect here
debug_print("bt_gatt_client_disconnect failed\r\n");
}
else
{
debug_print("bt_gatt_client_disconnect succeeded\r\n");
}
return_code = bt_gatt_client_conn_state_get(FIRST_CONNECTION, connection_state);
if (return_code != SCRIPT_OPERATION_SUCCESS)
{
// Handle failed bt_gatt_client_conn_state_get here
debug_print("bt_gatt_client_conn_state_get failed\r\n");
}
else
{
connection_state_print(connection_state);
debug_print("bt_gatt_client_conn_state_get succeeded\r\n");
}
for(;;)
{
Delay(1000);
}
}