BLE Advertising and Scanning

Intro to Bluetooth Low Energy #4

Generic Access Profile (GAP)

The Generic Access Profile (GAP) provides the framework that defines how BLE devices interact with each other. This includes the following aspects:

  • Modes & Roles of BLE devices.

  • Advertisements (advertising, scanning, advertising parameters, advertising data, scanning parameters).

  • Connection establishment (initiating, accepting, connection parameters).

  • Security.

The implementation of this framework is mandatory per the official specification, and it is what allows two or more BLE devices to interoperate, communicate, and be able to exchange data with each other.

Advertising State

In the advertising state, a device sends out packets containing useful data for others to receive and process. The packets are sent at a fixed interval defined as the advertising interval.

There are 40 RF channels in BLE, each separated by 2 MHz (center-to-center). Three of these channels are called the Primary Advertising Channels, while the remaining 37 channels are used for Secondary Advertisements and data packet transfer during a connection.

RF Channels in BLE

Since these are the three channels that a device starts by advertising on, and usually switches between them, they are spread apart in the frequency spectrum to avoid radio interference between a device that is advertising on one channel and another that is advertising on a different channel. Also, the locations of these primary channels were chosen within the spectrum to avoid interference with the most commonly used Wi-Fi channels.

Advertisings always start with Advertisement Packets sent on the three Primary Advertising Channels (or a subset of these channels). This allows centrals to find the advertising device (peripheral) and parse its advertisement packets. The central can then initiate a connection if the advertiser allows it.

The central can also request what is called a Scan Request, and if the advertiser supports it, it will respond with a Scan Response. Scan requests and responses allow the advertiser to send additional advertising data that would not fit in the initial advertisement packet.

  • Primary Advertisement Data is limited to 31 bytes.

  • Secondary Advertisement Data supports up to 254 bytes of data.

Some devices (Broadcasters) stay in the advertising state and do not accept connections (Connectionless), while others (Peripherals) allow the transition to the connected state if a Central initiates a connection (Connection-Oriented).

The main advantage of staying in the advertising state is that multiple centrals can discover the advertising data without the need for a connection. However, the downsides are the lack of security and the inability of the advertiser to receive data from a central (data transfer is unidirectional).

Scanning State

Centrals tune to the three Primary Advertising Channels one at a time. So, for a central to discover a peripheral, the central has to be tuned to the same channel on which the peripheral is advertising at that given point. To increase the possibility of this happening, and to make it happen quickly, a few advertising and scanning parameters can be adjusted.

Active and Passive Scanning

A device that listens for advertisements, and then sends Scan Requests is defined to be in the Active Scanning mode, while a device that passively listens to advertising packets and does not send scan requests is said to be in the Passive Scanning mode.

Advertising Events

An Advertising Event is made up of multiple advertising packets being sent on all, or a subset of, the three Primary Advertising Channels. There are seven types of advertising events (different types of advertising packets):

  • Connectable and Scannable Undirected Event
    This type allows other devices to receive the advertisement packets, send a scan request to the advertiser, and establish a connection with it.

  • Connectable Undirected Event
    This type allows other devices to receive the advertisement packets and establish a connection with the advertiser.

  • Connectable Directed Event
    This type allows a specific device to receive the advertisement packets and establish a connection with the advertiser.

  • Non-Connectable and Non-Scannable Undirected Event
    This type allows other devices to receive its advertisement packets. However, it does not allow scan requests or the establishment of a connection with the advertiser.

  • Non-Connectable and Non-Scannable Directed Event
    This type allows a specific device to receive the advertisements without the ability to establish a connection with the advertiser or to send scan requests.

  • Scannable Undirected Event
    This type allows other devices to send a scan request to the advertiser to receive additional advertisement data.

  • Scannable Directed Event
    This type allows a specific device to send a scan request to the advertiser to receive additional advertisement data.

Advertising Parameters

The different Advertising Parameters are:

  • Advertising Interval
    The most important parameter related to advertisements is the advertising interval. The advertising interval value ranges from 20 milliseconds up to 10.24 seconds in small increments of 625 microseconds. The advertising interval greatly impacts battery life and should be chosen carefully. It is recommended to choose the longest advertising interval that provides a balance between fast connectivity and reduced power consumption.

  • Advertising Data
    Let's take a look at what fields are usually included in an advertisement packet, and what the packet format looks like.

    The Advertising Data follows a format for organizing data similar to TLV (Type-Length-Value) used in data communications, except that the length comes before the type. The advertising data goes into the PDU (Protocol Data Unit) portion of the BLE packet and contains the following:

    • Length: The length of the data that follows the length value itself (includes the AD Type as well as the AD Data).

    • Advertising Data Type (AD Type): The type of advertisement data included in this specific TLV.

    • Advertising Data: The actual value of the advertisement data.

Advertising Data (AD) types are defined in the Bluetooth Core Specification Supplement document (not the Core Specification document). Some of the most commonly used AD Types:

  • Local Name: contains the device name that is read by scanners.

  • Tx Power Level: transmit power level, defined in units of dBm.

  • Flags: multiple one-bit Boolean flags, including:

    • Limited Discoverable Mode

    • General Discoverable Mode

    • BR/EDR Not Supported

    • Simultaneous LE and BR/EDR to Same Device Capable (controller)

    • Simultaneous LE and BR/EDR to Same Device Capable (host)

  • Service Solicitation: a list of one or more UUIDs (Universal Unique Identifier) indicating what services are supported and exposed by the device's GATT server. This helps central devices learn the available services exposed by a device before establishing a connection.

  • Appearance: this defines the external appearance of the device according to the Bluetooth Assigned Numbers. These include appearances such as phone, heart rate sensor, key ring and many more.

    If you cannot find an appearance that fits the nature of your device, you can use the UNKNOWN APPEARANCE value.

Please note thatScan Responses Datashare the same format as**Advertising Data.

Scanning Parameters

The three main Scanning Parameters are:

  • Scan Type: Passive or Active Scanning.

  • Scan Window: indicates how long to be scanning for advertisements.

  • Scan Interval: indicates how often to scan for advertisements.

The scanner will listen for the complete Scan Window at every Scan Interval, and in each scan window, it will listen to a different Primary Advertising Channel.

Scan Window and Scan Interval are configurable aspects of a scanner's behavior.

Scanning Parameters