【蓝牙精品系列文档】【02_gatt进阶】02_GATT服务开发
GATT 服务开发核心概念GATT(Generic Attribute Profile)定义了 BLE 属性数据的层次组织方式,是 BLE 应用开发的核心。GATT 层次结构GATT Server ├── Service A (UUID: 0x1800 - Generic Access) │ ├── Characteristic A1 (UUID: 0x2A00 - Device Name) │ │ ├── Value: "MyLight" │ │ └── Descriptor: CCC (0x2902) ← 不适用于 Read/Write Char │ ├── Characteristic A2 (UUID: 0x2A01 - Appearance) │ │ └── Value: 0x0580 (Generic Light) │ └── ... ├── Service B (UUID: 自定义 - Light Control) │ ├── Characteristic B1 (UUID: 自定义 - OnOff) │ │ ├── Value: 0x00/0x01 │ │ ├── Properties: Read | Write | Notify │ │ └── Descriptor: CCC (0x2902) │ ├── Characteristic B2 (UUID: 自定义 - Brightness) │ │ ├── Value: 0x00~0xFF │ │ ├── Properties: Read | Write | Notify │ │ └── Descriptor: CCC (0x2902) │ └── Characteristic B3 (UUID: 自定义 - Color Temp) │ ├── Value: uint16 │ ├── Properties: Read | Write | Notify │ └── Descriptor: CCC (0x2902) └── ...GATT UUIDUUID 类型长度范围Bluetooth SIG 官方 UUID16-bit0x0000 ~ 0xFFFF自定义 UUID128-bit基于 Bluetooth Base UUID 替换照明相关的 SIG 标准 UUIDUUID名称用途0x1800Generic Access设备名称、外观等0x1801Generic AttributeGATT 服务变化通知0x180ADevice Information厂商信息、固件版本0x2A00Device Name设备名称0x2A01Appearance设备外观类型0x2A19Battery Level电池电量自定义 UUID 规范使用 128-bit UUID,基于 Bluetooth Base UUID 替换:XXXXXXXX-0000-1000-8000-00805F9B34FB │ └── 替换 Bluetooth Base UUID 前 4 字节 Bluetooth Base UUID: 00000000-0000-1000-8000-00805F9B34FB 自定义: 将前 4 字节替换为自定义值 示例: FEE0xxxx-0000-1000-8000-00805F9B34FBCharacteristic PropertiesBitProperty说明典型用途0Broadcast广播较少使用1Read可读状态查询2Write Without Response无响应写入快速控制3Write有响应写入可靠控制4Notify通知状态上报5Indicate指示重要状态上报6Authenticated Signed Writes签名写入安全写入7Extended Properties扩展属性—常用 DescriptorUUID名称说明0x2902Client Characteristic Configuration (CCC)开启/关闭 Notify/Indicate0x2901Characteristic User Description人类可读描述0x2903Server Characteristic Configuration广播配置0x2904Presentation Format数据格式描述0x2905Aggregate Format聚合格式CCC (Client Characteristic Configuration) 详解CCC Value (2 bytes): Bit 0: Notification Enable (0x0001) Bit 1: Indication Enable (0x0002) 写入 0x0001 → 开启 Notification 写入 0x0002 → 开启 Indication 写入 0x0000 → 关闭所有照明 GATT 服务设计示例Light Control Service// 自定义 UUID 定义#defineUUID_LIGHT_SERVICE0xFEE0// 灯控服务#defineUUID_LIGHT_ONOFF0xFEE1// 开关#defineUUID_LIGHT_BRIGHTNESS0xFEE2// 亮度#defineUUID_LIGHT_COLOR_TEMP0xFEE3// 色温// GATT Service 结构Service:UUID_LIGHT_SERVICE(0xFEE0)├── Characteristic:OnOff(0xFEE1)│ ├── Properties:READ|WRITE|NOTIFY │ ├── Value:uint8(0=关,1=开)│ └── Descriptor:CCC ├── Characteristic:Brightness(0xFEE2)│ ├── Properties:READ|WRITE|NOTIFY │ ├── Value:uint8(0~255)│ └── Descriptor:CCC └── Characteristic:ColorTemp(0xFEE3)├── Properties:READ|WRITE|NOTIFY ├── Value:uint16(2000~6500K)└── Descriptor:CCC操作流程1. 手机 App 连接灯设备 2. App 发现 Light Control Service 3. App 写入 CCC 开启 Notify 4. App 写入 OnOff = 1 → 灯亮 5. App 写入 Brightness = 128 → 灯亮度 50% 6. 灯状态变化时 Notify 通知 AppMTU 协商MTU(Maximum Transmission Unit)BLE 4.0/4.1 默认 MTU = 23 字节(ATT Value 最大 20 字节)BLE 4.2+ 支持协商更大的 MTU协商流程:Client Server │── Exchange MTU Request ────────│ Client Rx MTU = 247 │── Exchange MTU Response ──────│ Server Rx MTU = 247 │ │ │ 实际 MTU = min(Client, Server) │ │ = min(247, 247) = 247 │MTU 对照明的影响默认 20 字节:一次只能传输少量数据大 MTU:可以一次传输更多属性值,减少交互次数建议:照明场景协商 MTU 到 247,减少延迟GATT 缓存Client 可以缓存 Server 的 GATT 数据库结构如果 Serv