蓝牙通信建立过程要看是经典蓝牙(BR/EDR)还是BLE,两者流程类似但协议细节不同。
作为嵌入式开发工程师,通常最常接触的是:
- BLE:Advertising → Scan → Connect → Pair → Data
- Classic:Inquiry → Paging → Connect → Pair → Data
一、BLE通信建立过程
以手机连接心率带为例。
1. Advertising(广播)
BLE设备上电后先发送广播包:
Heart Rate Sensor ↓ Advertising Channel (37/38/39)广播包中包含:
Device Name UUID MAC Address Flags例如:
Name = HRM_001 Service UUID = 0x180D2. Scanning(扫描)
手机作为 Central:
Scan Request监听:
Ch37 Ch38 Ch39收到广播:
Advertisement Packet如果需要更多信息:
Scan Request ↓ Scan Response3. Connection(建立连接)
手机发出:
CONNECT_IND告诉设备:
Connection Interval Slave Latency Supervision Timeout例如:
Interval = 50 ms Latency = 0 Timeout = 4 s从这一刻开始:
Peripheral <-> Central进入连接状态。
4. Connection Event
连接后不会一直发数据。
而是:
50ms ↓ Connection Event ↓ Sleep ↓ 50ms ↓ Connection Event例如:
0ms 50ms 100ms 150ms ...每个连接事件可以交换数据。
这也是BLE省电的原因。
5. Pairing(配对)
如果需要安全通信:
Pairing Request ↓ Pairing Response协商:
Just Works Passkey Numeric Comparison OOB例如:
123456用户确认数字。
6. Bonding(绑定)
双方保存:
LTK (Long Term Key) IRK (Identity Key) CSRK (Signing Key)以后再次连接:
直接加密 无需再次输入密码7. GATT发现
连接建立后:
Service Discovery查找设备支持什么服务。
例如:
Heart Rate Service Battery Service Device Info ServiceService
0x180D Heart Rate Service下面有多个Characteristic。
Characteristic
Heart Rate ValueUUID:
0x2A37 ``支持:
Read Write Notify `8. 数据传输
例如开启Notify:
Enable Notification设备主动上报:
HeartRate = 72 HeartRate = 73 HeartRate = 74无需不停Read。
BLE完整流程
Power On ↓ Advertising ↓ Scanning ↓ Connection ↓ Pairing ↓ Encryption ↓ Service Discovery ↓ Read/Write/Notify二、经典蓝牙(BR/EDR)通信建立过程
比BLE复杂。
1. Inquiry(设备发现)
主设备发:
Inquiry附近设备响应:
Inquiry Response返回:
BD_ADDR Class of Device Clock Offset例如:
00:11:22:33:44:55 Bluetooth Speaker2. Paging(寻呼)
知道设备地址后:
Page Request建立物理链路。
Master ↓ Page ↓ Slave成功后建立:
ACL Link3. Link Establishment
创建基带连接:
Baseband LMP ``交换能力:
Feature Exchange Version Info4. Pairing
双方协商:
PIN Passkey SSP现代蓝牙一般是:
Secure Simple Pairing5. Authentication
验证身份:
Challenge Response确认合法设备。
6. Encryption
开启链路加密:
E0 AES(新版本主要使用AES相关机制)
7. Profile建立
经典蓝牙最重要的是Profile。
例如耳机:
A2DP AVRCP HFP串口:
SPP文件传输:
OPP8. 数据传输
建立RFCOMM后:
UART Data ↓ RFCOMM ↓ L2CAP ↓ ACL开发时经常看到:
SPP Connected就是已经完成所有连接过程。
Classic Bluetooth完整流程
Power On ↓ Inquiry ↓ Page ↓ ACL Connection ↓ Pairing ↓ Authentication ↓ Encryption ↓ Profile Setup ↓ Data Transfer三、从协议栈角度看BLE
嵌入式面试特别喜欢问这一张图:
Application ↑ GATT ↑ ATT ↑ L2CAP ↑ HCI ↑ Link Layer ↑ PHY建立连接时各层作用:
PHY
负责:
2.4GHz无线收发Link Layer
负责:
Advertising Scanning Connecting Channel HoppingHCI
负责:
Host Controller之间通信。
常见日志:
HCI_LE_Create_ConnectionL2CAP
负责:
数据分包 重组ATT
负责:
Attribute访问GATT
负责:
Service Characteristic Descriptor四、实际抓包看到的连接过程
使用 Ellisys 或 Frontline 抓BLE包时经常看到:
ADV_IND ADV_IND ADV_IND ↓ SCAN_REQ ↓ SCAN_RSP ↓ CONNECT_IND ↓ LL_FEATURE_REQ ↓ LL_VERSION_IND ↓ MTU Exchange ↓ Service Discovery ↓ Notification如果你在做BLE协议栈开发,这个时序图几乎每天都会看到。
面试时最简洁的答案:
BLE: Advertising → Scanning → Connection → Pairing/Bonding → Service Discovery → Read/Write/Notify Classic Bluetooth: Inquiry → Paging → ACL Connection → Pairing → Authentication → Profile Setup → Data Transfer这基本涵盖了蓝牙通信建立的完整流程。