bt_adv_conn_data_set()

bt_adv_conn_data_set()

Description

Sets data to advertise with a pre-configured advertisement configuration for connectable advertising. Data is added to the advertising indication packet in the connectable advertising set. This does not override device name in GATT server settings, which is advertised in the scan response packet.

Syntax

bt_adv_conn_data_set(adv_data[], data_len);

Parameters

Variable

Input

Variable

Input

adv_data[]

An array of data to advertise. Must follow the BLE data format.

data_len

Length of the data to be advertised. Messages up to 31 bytes are supported.

Refer to the example below on how to construct the BLE payload.

Returns

Return value

Return explanation

SCRIPT_OPERATION_SUCCESS

Successfully added advertisement data.

SCRIPT_OPERATION_FAILED

Failed to add advertisement data.

SCRIPT_PARAM_INVALID

Given data length is bigger than data array.

SCRIPT_NO_ACCESS

This error signifies that Bluetooth communication was not initialized.

Constructing beacon data

A useful tool to help create Bluetooth LE advertisements: Bluetooth LE Advertisement Builder. The constructed payload can then be initialised as an array using the code example below.

To create custom advertised data, first select the data type you want to advertise. For example, to advertise specific "Service data" and "Manufacturer specific data" use data types 0x16 and 0xFF respectively, as indicated in the assigned numbers document:

Secondly, each data type has its own data:

  • For data type 0x16 (Service data), we'll advertise: 0x09, 0x6C.

  • For data type 0xFF (Manufacturer-specific data), we'll advertise: 0xAB, 0x41, 0x30.

After deciding on which data to advertise, the length of each data type entry needs to be calculated. Note that the data type byte is also added to the calculations.

  • For data type 0x16 (Service data), the data length is 3 (two data bytes and one data type byte).

  • For data type 0xFF (Manufacturer-specific data), the data length is 4 (three data bytes and one data type byte).

Finally, construct the BLE advertisement. Each data type follows this pattern: data length byte → AD type byte → data bytes. Multiple data types can be included in one message. The maximum total payload is 31 bytes.

Some BT scanners cannot discover beacons without the “discoverable” flag set. To enable this, add three bytes to the start of the advertisement payload: 0x02, 0x01, 0x06. This sets both the discoverable and BLE-only flags.

So the newly formed array (with BT flags) would look like this = {0x02, 0x01, 0x06, 0x03, 0x16, 0x09, 0x6C, 0x04, 0xFF, 0xAB, 0x41, 0x30}

Byte

Role

Notes

Byte

Role

Notes

0x02

Length

2 bytes follow (flags element)

0x01

AD type

Flags

0x06

Flags value

General discoverable + BLE-only

0x03

Length

3 bytes follow: 0x16, 0x09, 0x6C

0x16

AD type

Service data type

0x09

Data

 

0x6C

0x04

Length

4 bytes follow: 0xFF, 0xAB, 0x41, 0x30

0xFF

AD type

Manufacturer specific data

0xAB

Company ID (41AB)

In LE format

0x41

0x30

Data

 

f21c180a-d10d-4a4f-bb21-e85abc1ef3f8.png

Function call example

#include <io> #include <read> #include <float> #include <string> #include <core> #include <write> #include <define> #include <socket> #include <bluetooth> new demo_adv_array[] = [0x02, 0x01, 0x06, 0x03, 0x16, 0x09, 0x6C, 0x04, 0xFF, 0xAB, 0x41, 0x30]; main() { bt_init(); /* GATT server init code start */ /* GATT server init code end */ bt_adv_conn_data_set(demo_adv_array, sizeof(demo_adv_array)); for(;;) { Delay(100); } }