蓝牙核心概念
面向软件开发人员的蓝牙技术入门,掌握核心概念、版本演进和开发选型。
💡 核心概述
蓝牙是一种工作在 2.4GHz ISM 频段 的短距离无线通信协议,采用 跳频扩频 (FHSS) 技术实现抗干扰。现代蓝牙支持两种模式:
flowchart TD
BT[蓝牙技术]
BT --> Classic[经典蓝牙 BR/EDR]
BT --> BLE[低功耗蓝牙 BLE]
Classic --> A2DP[A2DP 音频]
Classic --> HFP[HFP 通话]
Classic --> SPP[SPP 串口]
BLE --> GATT[GATT 服务]
BLE --> GAP[GAP 广播]
BLE --> Mesh[Mesh 组网]
style BT fill:#007bff,color:#fff
style Classic fill:#28a745,color:#fff
style BLE fill:#17a2b8,color:#fff
📊 版本演进与开发支持
timeline
title 蓝牙版本演进
section 经典蓝牙
1999 : 1.0 : 首次发布 1Mbps
2004 : 2.0+EDR : 3Mbps EDR
2009 : 3.0+HS : 24Mbps (802.11)
section 低功耗蓝牙
2010 : 4.0 : BLE 引入
2016 : 5.0 : 2M PHY, Long Range
2020 : 5.2 : LE Audio, LC3
2023 : 5.4 : PAwR, ESL
| 版本 | 年份 | 关键特性 | 开发影响 |
|---|---|---|---|
| 4.0 | 2010 | BLE 首次引入 | IoT 设备起点,最低支持版本 |
| 4.2 | 2014 | IPv6, 隐私特性 | 支持直接联网,隐私地址 |
| 5.0 | 2016 | 2M PHY, LE Coded, 广播扩展 | 高速/长距离模式,定位应用 |
| 5.1 | 2019 | AOA/AOD 定位 | 室内定位,方向查找 |
| 5.2 | 2020 | LE Audio, LC3, Isochronous | 音频流,多设备同步 |
| 5.3 | 2021 | 连接子事件,信道分类 | 低功耗优化,抗干扰 |
| 5.4 | 2023 | PAwR, ESL, 加密广播 | 电子货架标签,双向通信 |
🔧 开发选型指南
经典蓝牙 vs BLE 选择
flowchart LR
Start[需求分析] --> Q1{需要传输什么?}
Q1 -->|音频/大文件 | Classic[经典蓝牙]
Q1 -->|小数据/传感器 | BLE[BLE]
Classic --> A1[A2DP 音频]
Classic --> A2[HFP 通话]
Classic --> A3[SPP 串口透传]
BLE --> B1[GATT 服务]
BLE --> B2[广播 Beacon]
BLE --> B3[Mesh 组网]
style Start fill:#007bff,color:#fff
style Q1 fill:#ffc107,color:#000
style Classic fill:#28a745,color:#fff
style BLE fill:#17a2b8,color:#fff
| 特性 | 经典蓝牙 (BR/EDR) | 低功耗蓝牙 (BLE) |
|---|---|---|
| 典型速率 | 1-3 Mbps | 125kbps - 2 Mbps |
| 功耗 | 30mA (峰值) | 15mA (峰值), μA (待机) |
| 连接时间 | 数秒 | 3-6ms |
| 拓扑 | 点对点/点对多点 | 广播/星型/Mesh |
| 典型应用 | 耳机、音箱、车载 | 传感器、手环、Beacon |
| 开发 SDK | BlueZ, Android Bluetooth | Nordic SDK, Zephyr |
芯片选型参考
| 厂商 | 系列 | 特点 | 适用场景 |
|---|---|---|---|
| Nordic | nRF52/nRF53/nRF54 | 生态完善,文档丰富 | 通用 IoT 开发首选 |
| TI | CC26xx | 低功耗,多协议 | Zigbee+BLE 双模 |
| Dialog | DA145xx | 超低功耗,小尺寸 | 可穿戴设备 |
| Espressif | ESP32 | WiFi+BLE 双模,便宜 | 物联网网关 |
| Qualcomm | QCC 系列 | 音频优化,aptX | TWS 耳机,音频设备 |
| Realtek | RTL876x | 性价比高 | 消费电子产品 |
📋 核心概念速查
| 术语 | 全称 | 说明 | 开发相关 |
|---|---|---|---|
| BR/EDR | Basic Rate / Enhanced Data Rate | 经典蓝牙传输模式 | 音频、SPP 开发 |
| BLE | Bluetooth Low Energy | 低功耗蓝牙 | IoT 设备开发 |
| GAP | Generic Access Profile | 设备发现、连接管理 | 广播、扫描配置 |
| GATT | Generic Attribute Profile | 数据交换协议 | Service/Characteristic 定义 |
| PHY | Physical Layer | 物理层,射频收发 | 1M/2M/Coded 选择 |
| HCI | Host Controller Interface | 主机控制器接口 | AT 指令/USB 通信 |
| MTU | Maximum Transmission Unit | 最大传输单元 | 数据分包大小 |
| RSSI | Received Signal Strength Indicator | 接收信号强度 | 距离估算、定位 |
💻 开发者快速上手
开发环境准备
Android BLE 开发
<!-- AndroidManifest.xml -->
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.BLUETOOTH_SCAN" />
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
<uses-feature android:name="android.hardware.bluetooth_le" android:required="true" />
// Kotlin - 扫描设备
val scanner = bluetoothAdapter.bluetoothLeScanner
val scanCallback = object : ScanCallback() {
override fun onScanResult(callbackType: Int, result: ScanResult) {
val device = result.device
Log.d("BLE", "发现设备:${device.name} - ${device.address}")
}
}
scanner.startScan(scanCallback)
iOS CoreBluetooth 开发
// Info.plist
<key>NSBluetoothAlwaysUsageDescription</key>
<string>需要使用蓝牙连接设备</string>
<key>NSBluetoothPeripheralUsageDescription</key>
<string>需要连接蓝牙外设</string>
// Swift - 扫描设备
import CoreBluetooth
class BLEManager: NSObject, CBCentralManagerDelegate {
var centralManager: CBCentralManager!
func scan() {
centralManager = CBCentralManager(delegate: self, queue: nil)
}
func centralManagerDidUpdateState(_ central: CBCentralManager) {
if central.state == .poweredOn {
centralManager.scanForPeripherals(withServices: nil)
}
}
}
Linux BlueZ 开发
# 安装 BlueZ
sudo apt-get install bluez bluez-tools
# 蓝牙控制命令
bluetoothctl # 进入交互模式
scan on # 扫描设备
pair XX:XX:XX:XX:XX:XX # 配对
connect XX:XX:XX:XX:XX:XX # 连接
# Python - bleak 库
import asyncio
from bleak import BleakScanner
async def scan():
devices = await BleakScanner.discover()
for d in devices:
print(f"{d.name}: {d.address}")
asyncio.run(scan())
嵌入式 Nordic nRF52 开发
// main.c - BLE 初始化
#include "nrf_ble_gatt.h"
#include "nrf_ble_qwr.h"
void ble_stack_init(void) {
ret_code_t err_code;
// 启用 SoftDevice
nrf_clock_lf_cfg_t clock_cfg = NRF_CLOCK_LFCLK_RC;
SOFTDEVICE_HANDLER_INIT(&clock_cfg, NULL);
// 启用 BLE
ble_enable_params_t ble_enable_params;
ble_cfg_t ble_cfg;
// 配置 GAP
ble_cfg.conn_cfg.conn_cfg_tag = 1;
ble_cfg.conn_cfg.params.gap_conn_cfg.min_conn_interval = 7.5;
ble_cfg.conn_cfg.params.gap_conn_cfg.max_conn_interval = 15;
sd_ble_cfg_set(BLE_CONN_CFG_GAP, &ble_cfg, RAM_START);
sd_ble_enable(&ram_start);
}
🔍 调试工具推荐
手机 App
- nRF Connect - Nordic 官方,功能最全
- LightBlue - iOS 首选,界面友好
- BLE Scanner - 跨平台,开源
- Android 蓝牙日志 - adb logcat -s Bluetooth
PC 工具
- nRF Connect Desktop - 桌面版调试工具
- Wireshark + Ubertooth - 空口抓包
- Ellisys - 专业协议分析 (昂贵)
- Frontline - 协议分析仪
开发板
- nRF52840 DK - Nordic 官方开发板
- ESP32 DevKit - 便宜,WiFi+BLE
- CC2650 LaunchPad - TI 开发板
- Ubertooth One - 开源抓包硬件