GATT server console service
1. Introduction
The BT Console Service provides a simple, bidirectional communication channel between a BLE-enabled device (server) and a client (e.g., mobile app, desktop, or other BLE devices). This service allows the client to send and receive data from the device, effectively emulating serial communication.
This functionality is accessible with the ***_02_003 device firmware and 2.3.0 Bluetooth firmware versions or higher.
1.1 Key Features:
Simple setup and usage compared to a full GATT server implementation.
Up to 200-byte data payloads.
Ability to subscribe to the TX characteristic to receive notifications.
GATT client data is received in script with the UUID and user reference identifiers.
Console service can work alongside user-defined services.
1.2 Structure
Console Service UUID:
00000200-0000-1000-8000-00805F9B34FB(0200)TX Characteristic UUID:
00000201-0000-1000-8000-00805F9B34FB(0201): Accepts client-written data, such as custom commands or values.RX Characteristic UUID:
00000202-0000-1000-8000-00805F9B34FB(0202): Allows the device to send user data from the script to the client. This characteristic hasreadandnotify(if enabled) attribute properties.
2. Setup
2.1 Required commands
Only four commands are necessary to start a GATT server with a console service:
bt_init()andbt_gatt_server_init()are needed to initialise Bluetooth communication and callbacks.bt_gatt_server_console_init(subscription_type)creates the service and characteristics in the GATT server. The attributes are not yet visible before thebt_gatt_server_start()command.
Additional GATT services can be created before and after bt_gatt_server_console_init().
It is not permissible to create a user-defined GATT characteristic immediately following the bt_gatt_server_console_init() function, as this would result in the characteristic being added to the console service, which is prohibited. Therefore, a user-defined GATT service must be established prior to creating GATT characteristics.
bt_gatt_server_start()registers the console service and other user-defined services (if created). GATT console is ready to use when the GATT client connects.
To make the GATT server visible to other devices, advertising must be started either in using the device settings under GATT server settings, or using script functions.
2.2 Example code
#include <io>
#include <read>
#include <float>
#include <string>
#include <core>
#include <write>
#include <define>
#include <socket>
#include <bluetooth>
new tx_data[100];
new rx_data[100];
forward public callback (event);
public callback(event)
{
switch(event)
{
case BT_GATT_SERVER_RX:
{
new message_length = bt_gatt_server_receive(rx_data);
if (rx_data[GATT_RX_HEADER_USER_REF_POS] == GATT_CONSOLE_REFERENCE)
{
parse_console_message(message_length);
}
}
}
}
parse_console_message(message_length)
{
/* Receive console message code start */
/* Receive console message code end */
}
main()
{
bt_init();
bt_gatt_server_init();
bt_gatt_server_console_init(GATT_CHRC_PROP_NOTIFY);
bt_gatt_server_start();
for(;;)
{
snprintf(tx_data, sizeof(tx_data), "Ticks:%d\r\nVCC:%d", get_val(Sensor_Ticks), get_val(SENSOR_VCC));
bt_gatt_server_console_write(tx_data, strlen(tx_data));
}
}