How Bluetooth broadcasts and scans

This article involves the following BLE issues:

  • How BLE devices are discovered

  • How to quickly find BLE devices or how to reduce the power consumption in the advertising phase

  • PDU on BLE advertising channel /channel (channle)

1. Purpose of Broadcasting

  •  Let others discover themselves. For a device that does not broadcast, the surrounding devices cannot feel its existence. Therefore, in order for other devices to discover it, it must broadcast to the outside. Rich data can be carried in the broadcast, such as equipment capabilities, device names, and other custom data, there is a second possibility.
  • Broadcast data to applications that do not need to establish a connection, such as a BLE thermometer, which itself does not need to receive any connection, but can choose to send the temperature through broadcasting. Detectors can obtain the current temperature as long as they listen to the broadcast.

The scanner can establish a connection with the broadcaster only after receiving the broadcast data. Broadcasting is to periodically send broadcast data from the broadcast channel. There are several problems here:

1. Broadcast channel, the broadcast data will be broadcast on the 37/38/39 channel, of course, it can also be configured to be sent only on a certain broadcast channel

2. Broadcast data: the PDU of the broadcast type, the information carried by the broadcast.

3. Periodicity: the advertising interval

A broadcast packet is broadcasted at 37, 38, and 39 as a broadcast event (Advertising event), 

Broadcast data at a certain time interval, this interval is called advertising interval, in order to improve the compatibility of devices, a random delay time of 0-10ms is added between the two broadcast time intervals.

The time relationship is as follows:

Advertising event的周期= advertising interval + delay

Among them: delay is a random number of 0-10ms.

The range of advertising interval: 20ms – 10.24s, and the requirement is an integer multiple of 0.625ms.

2. Scan

Scanning is to obtain device information and discover surrounding devices. There are two situations:

Passive scan: In this case, the scanner does not send any information, only listens to the broadcast data, the scanner can receive the broadcast, and the broadcaster does not know the existence of the scanning device:

Active scanning: After receiving the broadcast data (ADV_IND), the scanner initiates a scanning request (SCAN_REQ), and the broadcaster receives the scanning request and responds to scanning (SCAN_RSP), and the scanning response (SCAN_RSP) can also carry broadcaster data:

However, it should be noted that the SCAN_REQ request sent by the scanner cannot carry valid user data (refer to the PDU Payload of the scanning type), so there is a one-way communication between the broadcast and the scanner. The scanner can know the information of the broadcaster, but the broadcaster cannot know Scanner information.

The broadcaster only sends broadcast data in the broadcast channel, and does not know the existence of any scanner. There is no synchronization method between the advertiser and the scanner. Only when the channels where the broadcast and the scan are located coincide randomly, the broadcast packet can be received by the scanning device. Therefore, like broadcasting devices, scanning devices also have some timing parameter requirements.

Scanning has two important parameters: scan interval and scan window

Scan interval: defines how often to send a scan request,

Scan window: Defines how long the scan lasts

The value range of Scan interval is: 2.5ms-10.24s

There is no synchronization method between the advertiser and the scanner. Only when the channels where the advertisement and the scanner are located coincide randomly, can both parties find out.

For broadcasting and scanning, the power consumption is mainly consumed during sending or receiving data. Therefore, the broadcasting interval and scanning interval have a great impact on power consumption. Different applications should set appropriate intervals according to the actual situation.

3 Initializing the connection (Initiating)

After receiving the broadcast data, the scanner can initiate a connection establishment request, and the data packet (CONNECT_REQ) of the connection establishment request is initiated on the broadcast channel. As follows:

The CONNECT_REQ request will carry important connection parameters, including the connection time interval, supervision timeout, and parameters related to frequency hopping, and after the connection is established, switch to the data channel to exchange data. These will be described in detail in connection establishment.

4. Expand the PDU on the broadcast channel

The PDU structure of the broadcast channel is further expanded as follows:

1. PDU Type: Indicates the type of PDU. There are seven PDUTypes, as follows:

                          

There are 7 types of PDUs, divided into three categories:

  • Four broadcast PDUs of broadcast type

  • Two scan type PDUs

  • An Initiate Connection PDU

2 RFU: reserved

3 TxAdd and RxAdd: The meaning is determined by PDUType, and the following chapters will be analyzed together with PDUType;

4 Length: The length of the PDU, 6 bits, effective range 6-37 octets,

 5 Payload: depends on the PDUType, the following chapters will be analyzed together with the PDU Type;

The broadcast type PDU includes a header of 16 bits and a payload of (0-31) bytes. The most important field in the header is PDU Type, Txaddr, RxAddr and payload are all determined by the PDU type. Now the PDUType Analyze seven situations:

According to the above information, at present, there are still two structures that have not been expanded: AdvData and LLData,

Let's take ADV_IND and CONNECT_REQ as examples to analyze the corresponding PDU

5. ADV_IND PDU expansion

ADV_IND PDU

In the end, see that AdvData is composed of AD Structures, and each advStructure includes three parts:

· AD length, length of AD Type and ADData

· AD Type: the data meaning of AD Data,

· AD Data: the data indicated by AD Type

AD type is defined in Core Specification Supplement (CSS) v7 https://www.bluetooth.com/specifications/adopted-specifications

Let's first look at the types supported by AD type from the code:

When the last AD Type = 0xFF, private data can be extended.

Example of packet capture (the packet capture software will parse or attach some frame header data):

This is the data that the tool has parsed. The raw data of the physical layer is as follows:

According to the PDU structure, it is parsed as follows:

The original data packet of SCAN_RSP is as follows:

After parsing:

CONNECT_REQ PDU is also a PDU on the broadcast channel, we will continue to analyze it later.

6. Interaction process of devices in the broadcasting phase

In general, the interaction process of devices in the broadcast phase is as follows:

Guess you like

Origin blog.csdn.net/u010196944/article/details/128136536