连接管理

📐 BLE 连接流程

flowchart LR A[1. 广播] --> B[2. 扫描] B --> C[3. 发起连接] C --> D[4. 参数协商] D --> E[5. 连接完成] style A fill:#28a745,color:#fff,stroke:#1e7e34 style B fill:#007bff,color:#fff,stroke:#0056b3 style C fill:#007bff,color:#fff,stroke:#0056b3 style D fill:#007bff,color:#fff,stroke:#0056b3 style E fill:#28a745,color:#fff,stroke:#1e7e34

📈 BLE 连接序列图

sequenceDiagram participant P as 从设备 (Peripheral) participant C as 主设备 (Central) Note over P,C: 广播阶段 P->>C: ADV_IND (广播) C->>P: SCAN_REQ P->>C: SCAN_RSP (设备名称等) Note over P,C: 连接阶段 C->>P: CONNECT_IND (连接请求) P->>C: LL_CONNECTION_UPDATE Note over P,C: 数据交互 C->>P: ATT_EXCHANGE_MTU P->>C: MTU Response C->>P: GATT 数据交互

📐 经典蓝牙连接流程

flowchart LR A[Inquiry] --> B[Page] B --> C[RFCOMM] C --> D[SDP] D --> E[连接建立] style A fill:#28a745,color:#fff,stroke:#1e7e34 style B fill:#007bff,color:#fff,stroke:#0056b3 style C fill:#007bff,color:#fff,stroke:#0056b3 style D fill:#007bff,color:#fff,stroke:#0056b3 style E fill:#28a745,color:#fff,stroke:#1e7e34

📈 经典蓝牙连接序列图

sequenceDiagram participant H as 主机 (Host) participant D as 从机 (Device) Note over H,D: 设备发现 H->>D: HCI_Inquiry D->>H: HCI_Inquiry_Result Note over H,D: 建立连接 H->>D: HCI_Create_Connection D->>H: HCI_Connection_Complete Note over H,D: L2CAP/RFCOMM H->>D: L2CAP_ConnectReq D->>H: RFCOMM_Connect Note over H,D: 数据传输 H->>D: 数据传输

连接参数详解

参数 范围 典型场景
interval 7.5ms - 4s 实时:7.5ms, 传感器:1s
slave_latency 0 - 499 省电:99, 低延迟:0
supervision_timeout 100ms - 32s 100ms - 32s

参数计算

// 条件:timeout > (1 + latency) * interval * 2
// 传感器:interval=1s, latency=99, timeout=10s OK
// 音频:interval=7.5ms, latency=0, timeout=500ms OK

连接优化场景

低延迟

  • interval: 7.5-15ms
  • latency: 0
  • 场景:游戏、遥控器

平衡

  • interval: 30-50ms
  • latency: 0-4
  • 场景:键盘、鼠标

低功耗

  • interval: 1-4s
  • latency: 50-99
  • 场景:传感器、手环