bt_adv_packet_read()

bt_adv_packet_read()

Description

Reads out data from Bluetooth scan module into user array.

Syntax

bt_adv_packet_read(data_array[], scan_buffer_to_readout);

Parameters

Variable

Input

data_array[]

Data array into which scanned data will be stored. It is recommended to have this array at least 250 bytes in size.

scan_buffer_to_readout

BT_FILTER_MATCH - Readout from filter match buffer.

BT_FILTER_MISMATCH - Readout from filter mismatch buffer.

Returns

Return value

Return explanation

from 1 to 255 bytes

Read data size.

Packet structure

The stored packet will possess its own structure. The following is an example of received data:

FF 48 80 33 E3 A8 00 D2 02 01 06 05 16 6E 2A 72 09 0E 09 50 20 54 20 45 4E 20 38 30 43 42 37 36

This data can be broken down as follows:

FF 48 80 33 E3 A8 00 D2 02 01 06 05 16 6E 2A 72 09 0E 09 50 20 54 20 45 4E 20 38 30 43 42 37 36

FF 48 80 33 E3 A8

00

D2

02 01 06 05 16 6E 2A 72 09 0E 09 50 20 54 20 45 4E 20 38 30 43 42 37 36

FF 48 80 33 E3 A8

00

D2

02 01 06 05 16 6E 2A 72 09 0E 09 50 20 54 20 45 4E 20 38 30 43 42 37 36

MAC address of scanned device

Message type

RSSI

RAW message data

  • Message type - there are several distinct types of messages. Reference the following table to determine the received message type:

Message type value

Value meaning:

Message type value

Value meaning:

0x00

Scannable and connectable advertising.

0x01

Directed connectable advertising.

0x02

Non-connectable and scannable advertising.

0x03

Non-connectable and non-scannable advertising.

0x04

Additional advertising data requested by an active scanner (message acquired during active scan).

0x05

Extended advertising.

  • RSSI - this value is signal strength. To convert RSSI into dBm do a byte hex to decimal conversion. In this example RSSI is -46 dBm:

    image-20240202-081025.png

 

Function call example

#include <io> #include <read> #include <float> #include <string> #include <core> #include <write> #include <define> #include <socket> #include <bluetooth> forward public callback (event); new scan_parameters[.type, .options, .interval, .window, .timeout, .interval_coded, .window_coded]; new mac_addr_filter[] = [0xFF, 0x48, 0x80, 0x33, 0xE3, 0xA8]; new scan_data_length = 0; new scanned_adv_data[255]; public callback (event) { switch(event) { case BT_FILTER_MATCH: { scan_data_length = bt_adv_packet_read(scanned_adv_data, BT_FILTER_MATCH); } case BT_FILTER_MISMATCH: { // SCAN FILTER MISMATCH CALLBACK } } return 0; } main() { scan_parameters[.type] = BT_SCAN_TYPE_ACTIVE; scan_parameters[.options] = BT_ADV_OPT_NONE; scan_parameters[.interval] = BT_SCAN_INTERVAL_FAST; scan_parameters[.window] = BT_SCAN_WINDOW_FAST; scan_parameters[.timeout] = 0; scan_parameters[.interval_coded] = 0; scan_parameters[.window_coded] = 0; bt_init(); bt_scan_init(scan_parameters); bt_scan_filter_add(BT_SCAN_FILTER_TYPE_ADDR_PUBLIC, mac_addr_filter, sizeof(mac_addr_filter)); bt_scan_filter_enable((BT_SCAN_FILTER_NAME | BT_SCAN_FILTER_ADDR), false); bt_scan_callback_enable(BT_FILTER_MATCH); bt_scan_start(); for(;;) { Delay(100); } }

Notes and Warnings

While it is feasible to utilize this function outside of a callback, it is advisable to read advertisement data within the callback to ensure that no messages are missed.