bt_gatt_client_read()

bt_gatt_client_read()

Description

Function reads data from a specific attribute via handle.

Syntax

bt_gatt_client_read(connection_id, handle, offset, read_len, data_array[]);

Parameters

Variable

Input

Variable

Input

connection_id

Connection id which was described in bt_gatt_client_connect function. Could be either 0 or 1.

handle

Handle of attribute to read data from. Handle limits are from 0x0001 to 0xFFFF.

offset

Data offset.

read_len

Returned amount of data that’s been read in bytes. This is NOT the amount of data to read, this is the amount of data that’s been read. User should pass a variable here, not a number.

data_array[]

Data array where read data will be stored. If the returned data exceeds the array's capacity, it will be truncated to fit.

Returns

Return value

Return explanation

SCRIPT_OPERATION_SUCCESS

Read operation was successful.

SCRIPT_OPERATION_FAILED

Read operation failed.

SCRIPT_NO_ACCESS

Bluetooth GATT client is not initialized.

SCRIPT_PARAM_INVALID

Could be one of the following:

  • Incorrect connection id or connection is not present

  • Handle too high or too low.

  • Passed data array is smaller than requested read length.

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 handle_to_read_from = 0x0D; new data_read_offset = 0; new number_of_bytes_read = 0; new read_data_array[20]; 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_read(FIRST_CONNECTION, handle_to_read_from, data_read_offset, number_of_bytes_read, read_data_array); for(;;) { Delay(100); } }

 

Debug example

This example initializes BLE, then initializes GATT Client module, connects to peer device and reads data from an attribute. 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 handle_to_read_from = 0x0D; new data_read_offset = 0; new number_of_bytes_read = 0; new read_data_array[20]; new return_code = SCRIPT_OPERATION_SUCCESS; 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_read(FIRST_CONNECTION, handle_to_read_from, data_read_offset, number_of_bytes_read, read_data_array); if (return_code != SCRIPT_OPERATION_SUCCESS) { // Handle failed bt_gatt_client_read here debug_print("bt_gatt_client_read failed\r\n"); } else { debug_print("bt_gatt_client_read succeeded\r\n"); debug_print("Read data: "); for (new i = 0; i < number_of_bytes_read; i++) { debug_print("%02X ", read_data_array[i]); } debug_print("\r\n") } for(;;) { Delay(1000); } }