Let’s examine how an advertisement packet is structured.
The BLE packet is pictured below, with the main portion going to what’s called the Protocol Data Unit (PDU). The PDU consists of either an advertising PDU (sometimes called an advertising channel PDU) or a data PDU (sometimes called a data channel PDU), depending on whether the BLE packet is used for advertisement or data transmission.
As we can see in the image, the advertising PDU consists of a header and a payload.
The header part of the advertising PDU consists of:
ADV_IND
.More information about these fields can always be found in the Core Specification of Bluetooth LE on the Bluetooth SIG website.
The payload of the advertising PDU is divided into two sections, where the first 6 bytes represent the advertiser’s address (AdvA) and the rest goes to the actual advertisement data (AdvData).
The payload structure depends on the kind of advertising. When doing directed advertisement (ADV_DIRECT_IND
) some space is needed to also specify the receiver’s address. Therefore, the AdvData field is replaced by a receiver address field with a size of 6 bytes. Advertisement packets of this type (ADV_DIRECT_IND
) do not include a payload.
The advertisement data section is represented as shown in the figure below.
The advertisement data packet is composed of multiple structures called advertisement data structures (AD structures). Each AD structure has a length field, a field to specify the type (AD Type), and a field for the actual data itself (AD Data). Note that the most common AD type is 1 byte long.
The advertising data types are defined by the Bluetooth specification and are documented in the nRF Connect SDK here, under “EIR/AD data type definitions”.
Below are a few commonly used ones, that we will be using in following exercises.
BT_DATA_NAME_COMPLETE
): This is simply the device name, that the human user sees when scanning for nearby devices (via a smartphone, for instance).BT_DATA_NAME_SHORTENED
): A shorter version of the complete local name.BT_DATA_URI
): Used to advertise a URI like website addresses (URLs).BT_DATA_MANUFACTURER_DATA
). This is a popular type that enables companies to define their own custom advertising data, as in the case of iBeacon. Here is an example of an advertising data structure that is setting the flag BT_LE_AD_NO_BREDR
.
The advertisement flags are one-bit flags encapsulated in one byte, meaning that there are up to 8 flags that can be set. We will take a look at some of the most commonly used flags
BT_LE_AD_LIMITED
: Sets LE Limited Discoverable Mode, used with connectable advertising to indicate to a central that the device is only available for a certain amount of time before the advertising times outBT_LE_AD_GENERAL
: Sets LE General Discoverable Mode, used in connectable advertising to indicate that advertising is available for a long period of time (timeout = 0). BT_LE_AD_NO_BREDR
: Indicates that classic Bluetooth (BR/EDR) is not supportedBoth BT_LE_AD_LIMITED
& BT_LE_AD_GENERAL
are meant for a device in a peripheral role.