低功耗藍牙兩類報文 : 廣播報文 和 數據報文。
本文討論廣播報文數據段,不包括完整報文其他部分,比如前導,接入地址等
藍牙設備通過廣播表明自己的存在,等待被連接, 就好象一個人站在接口大喊“我要脫單,我要脫單,快來牽手...”。
BLE 考慮功耗, 使用了3個廣播信道,順序廣播。
兩個藍牙設備想要建立連接, 第一步是 從機(server) 向外廣播, 主機(client) 搜索到后發起請求。 從機廣播中包含設備的相關信息,比如設備名稱,設備具有的服務uuid 等。
廣播包類型
- 廣播包 (Advertising Data)
- 響應包 (Scan Response)
主機主動掃描的情況下, 發送掃描請求給從機, 從機返回掃描響應數據。
廣播數據包格式

每個包都是 31 字節,數據包中分為有效數據(significant)和無效數據(non-significant)兩部分。
-
有效數據部分
包含若干個廣播數據單元,稱為 AD Structure 。如圖所示,AD Structure 的組成是:- 長度 Len ,表示這個 AD Structure 的長度(除去 len本身 1)
- 類型 AD Type
標記這段廣播數據代表什么, 比如設備名, uuid 等。 - 數據 AD data
-
無效數據部分
廣播包的長度必須是 31 個 byte,如果有效數據部分不到 31 自己,剩下的就用 0 補全。這部分的數據是無效的。
Flags
對於低功耗藍牙設備, 廣播中需要包括的一個 Structure, 包含一個byte 的標記, 標記設備
Flags used over the LE physical channel are:
- 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)
廣播設備的服務uuid
假如藍牙設備有心率等profile, 可以在廣播中添加那對應的服務uuid ,這樣其他設備可以通過廣播直接了解設備具備的功能。
GAP 和 GATT 服務的 UUID 不應該出現在廣播中, 這對於每個設備都是具有的。
廣播中包含服務uuid 包括六種類型, 對應不同程度 uuid 和列表完整性(complete 和 incomplete)
- 16-bit Bluetooth Service UUIDs
- 32-bit Bluetooth Service UUIDs
- Global 128-bit Service UUIDs
complete 和 incomplete 的區別
比如, 我設備有兩個服務對應的 16 bit uuid 分別是 0x1122 和 0x 2233。
- 如果我只想廣播其中一個
value | note |
---|---|
0x03 | len |
0x02 | Incomplete List of 16-bit Service Class UUIDs |
0x22 | uuid_L |
0x11 | uuid_H |
- 如果我兩個都廣播
value | note |
---|---|
0x05 | len |
0x03 | Complete List of 16-bit Service Class UUIDs |
0x22 | uuid1_L |
0x11 | uuid1_H |
0x22 | uuid2_L |
0x33 | uuid2_H |
廣播中的廠商信息
這個一段的廣播標記時 0XFF, 對應用於標記設備的生產商和其他信息。
數據前兩個字節時廠商ID, 其他自定義。
具體其他廣播數據段類型詳見 參考。
官方提供例子
Value | Notes |
---|---|
0x02 | Length of this Data |
0x01 | Flags |
0x06 | 廣播標記頭 |
0x06 | Length of this Data |
0x09 | Complete Local Name |
0x50 | 'P' |
0x68 | 'h' |
0x6F | 'o' |
0x6E | 'n' |
0x65 | 'e' |
0x05 | Length of this Data |
0x03 | Complete list of 16-bit Service UUIDs, 列出所有服務uuid ,否則使用imcomplete |
0x15 | PANU service class UUID |
0x11 | |
0x1F | Hands-free Audio Gateway service class UUID |
0x11 | |
0x01 | Length of this data |
0x05 | Complete list of 32-bit Service UUIDs |
0x01 | Length of this data |
0x07 | Complete list of 128-bit Service UUIDs |
0x00 | End of Data (Not transmitted over the air) |