bt_notification_read()
Description
Function reads notification data.
Syntax
bt_notification_read(notification_data[]);
Parameters
Variable | Input |
|---|---|
| Data array where notification data will be stored |
Returns
Return value | Return explanation |
SCRIPT_OPERATION_FAILED | Read operation failed. |
Data length | In case of success function will return number of bytes read from received notification. |
Read notification data will follow this format:
Notification data: 0x00 0x00 0x0F 0x42 0x75 0x7A 0x7A 0x65 0x72 0x20 0x4F 0x4E
0x00 - Connection ID from which notification was received
0x00 0x0F - Notification value handle (big endian)
0x42 0x75 0x7A 0x7A 0x65 0x72 0x20 0x4F 0x4E - Notification data (big endian)
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 chr_val_handle = 0x0F;
new chr_val_ccc_handle = 0x10;
new notification_size = 0;
new notification_data[50];
forward public callback (event);
public callback (event)
{
switch(event)
{
case BT_NOTIFICATION_RX:
{
notification_size = bt_notification_read(notification_data);
}
}
return 0;
}
main()
{
bt_init();
bt_gatt_client_init();
while (bt_gatt_client_connect(FIRST_CONNECTION, BT_ADDR_RANDOM, peer_mac_address) != SCRIPT_OPERATION_SUCCESS)
{
Delay(100);
}
bt_gatt_client_subscribe(FIRST_CONNECTION, chr_val_handle, chr_val_ccc_handle, BT_GATT_CCC_NOTIFY);
for(;;)
{
Delay(100);
}
}
Debug example
This example initializes BLE, then initializes GATT Client module, connects to peer device, subscribes to attribute and when subscription data arrives reads it. 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 chr_val_handle = 0x0F;
new chr_val_ccc_handle = 0x10;
new notification_size = 0;
new notification_data[50];
new return_code = SCRIPT_OPERATION_SUCCESS;
forward public callback (event);
public callback (event)
{
switch(event)
{
case BT_NOTIFICATION_RX:
{
notification_size = bt_notification_read(notification_data);
debug_print("Notification data: ");
for (new i = 0; i < notification_size; i++)
{
debug_print("%X ", notification_data[i]);
}
debug_print("\r\n")
}
}
return 0;
}
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_subscribe(FIRST_CONNECTION, chr_val_handle, chr_val_ccc_handle, BT_GATT_CCC_NOTIFY);
if (return_code != SCRIPT_OPERATION_SUCCESS)
{
// Handle failed bt_gatt_client_subscribe here
debug_print("bt_gatt_client_subscribe failed\r\n");
}
else
{
debug_print("bt_gatt_client_subscribe succeeded\r\n");
}
for(;;)
{
Delay(1000);
}
}