简介
本文简单介绍蓝牙广播。
蓝牙广播数据格式
(1)广播数据结构格式
广告数据(Advertising Data, AD)和扫描回应数据(Scan Response Data, SRD)是两种主要的数据包类型,它们在格式上都相同。
| Length (1 byte) | AD Type (1 byte) | AD Data (0-31 bytes) |
- Length:
- 1 字节,表示 AD Type 和 AD Data 的总长度。范围从 1 到 31 字节。
- AD Type:
- 1 字节,表示数据类型。各个类型由 Bluetooth SIG 定义,每种类型都有特定的含义(例如设备名称、服务 UUID 等)。
- AD Data:
- 0 到 31 字节,表示实际的数据内容。数据的解释依赖于 AD Type。
(2)广播数据包例子
一个典型的广播数据包可能包含多个 AD 结构。举个例子:
static const struct bt_data ad[] = {
BT_DATA_BYTES(BT_DATA_FLAGS, (BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR)),
BT_DATA(BT_DATA_NAME_COMPLETE, CONFIG_BT_DEVICE_NAME, sizeof(CONFIG_BT_DEVICE_NAME) - 1),
};
这个例子中包含两个 AD 结构:
-
Flags:
- Length: 2 字节(1 字节类型 + 1 字节数据)
- AD Type:
0x01
(Flags) - AD Data:
0x06
(General Discoverable Mode 和 BR/EDR 不支持)
-
Complete Local Name:
- Length:
1 + strlen(CONFIG_BT_DEVICE_NAME)
- AD Type:
0x09
(Complete Local Name) - AD Data: 设备名称的字节数组
- Length:
(3)具体例子解析
假设设备名称是 "MyDevice",那么广播数据的二进制表示如下:
Flags:
Length: 0x02
AD Type: 0x01
AD Data: 0x06
Complete Local Name:
Length: 0x09 (1 byte AD Type + 8 bytes "MyDevice")
AD Type: 0x09
AD Data: 0x4D 0x79 0x44 0x65 0x76 0x69 0x63 0x65 ("MyDevice")
合并后的广播数据包可能如下所示(假设配置了两个 AD 结构):
0x02 0x01 0x06 0x09 0x09 0x4D 0x79 0x44 0x65 0x76 0x69 0x63 0x65
蓝牙广播数据类型
在蓝牙低功耗(BLE)协议中,广播数据(Advertising Data, AD)可以包含多种不同类型的信息,每种信息类型都有特定的含义和用途。以下是常见的蓝牙广播数据类型表:
数据类型 (Type) | 值 (Value) | 描述 (Description) |
---|---|---|
Flags | 0x01 | 指示设备的功能,例如通用发现模式或 BR/EDR 支持情况。 |
Incomplete List of 16-bit Service Class UUIDs | 0x02 | 未完整列出的 16 位服务类 UUID。 |
Complete List of 16-bit Service Class UUIDs | 0x03 | 完整列出的 16 位服务类 UUID。 |
Incomplete List of 32-bit Service Class UUIDs | 0x04 | 未完整列出的 32 位服务类 UUID。 |
Complete List of 32-bit Service Class UUIDs | 0x05 | 完整列出的 32 位服务类 UUID。 |
Incomplete List of 128-bit Service Class UUIDs | 0x06 | 未完整列出的 128 位服务类 UUID。 |
Complete List of 128-bit Service Class UUIDs | 0x07 | 完整列出的 128 位服务类 UUID。 |
Shortened Local Name | 0x08 | 缩短的本地设备名称。 |
Complete Local Name | 0x09 | 完整的本地设备名称。 |
Tx Power Level | 0x0A | 发射功率级别。 |
Class of Device | 0x0D | 设备类别。 |
Simple Pairing Hash C-192 | 0x0E | 简单配对的 Hash C-192。 |
Simple Pairing Randomizer R-192 | 0x0F | 简单配对的随机数 R-192。 |
Device ID | 0x10 | 设备 ID。 |
Security Manager Out of Band (OOB) Flags | 0x11 | 安全管理器 OOB 标志。 |
Slave Connection Interval Range | 0x12 | 从设备连接间隔范围。 |
Service Solicitation: 16-bit UUIDs | 0x14 | 服务请求: 16 位 UUID。 |
Service Solicitation: 32-bit UUIDs | 0x1F | 服务请求: 32 位 UUID。 |
Service Solicitation: 128-bit UUIDs | 0x15 | 服务请求: 128 位 UUID。 |
Service Data: 16-bit UUID | 0x16 | 服务数据: 16 位 UUID。 |
Service Data: 32-bit UUID | 0x20 | 服务数据: 32 位 UUID。 |
Service Data: 128-bit UUID | 0x21 | 服务数据: 128 位 UUID。 |
Public Target Address | 0x17 | 公共目标地址。 |
Random Target Address | 0x18 | 随机目标地址。 |
Appearance | 0x19 | 外观。 |
Advertising Interval | 0x1A | 广播间隔。 |
LE Bluetooth Device Address | 0x1B | 低功耗蓝牙设备地址。 |
LE Role | 0x1C | 低功耗角色。 |
Simple Pairing Hash C-256 | 0x1D | 简单配对的 Hash C-256。 |
Simple Pairing Randomizer R-256 | 0x1E | 简单配对的随机数 R-256。 |
URI | 0x24 | 统一资源标识符(URI)。 |
Indoor Positioning | 0x25 | 室内定位。 |
Transport Discovery Data | 0x26 | 传输发现数据。 |
LE Supported Features | 0x27 | 低功耗支持的功能。 |
Channel Map Update Indication | 0x28 |