【蓝牙精品系列文档】【02_gatt进阶】02_GATT服务开发
2026/7/13 14:47:17 网站建设 项目流程

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 UUID

UUID 类型长度范围
Bluetooth SIG 官方 UUID16-bit0x0000 ~ 0xFFFF
自定义 UUID128-bit基于 Bluetooth Base UUID 替换

照明相关的 SIG 标准 UUID

UUID名称用途
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-00805F9B34FB

Characteristic Properties

BitProperty说明典型用途
0Broadcast广播较少使用
1Read可读状态查询
2Write Without Response无响应写入快速控制
3Write有响应写入可靠控制
4Notify通知状态上报
5Indicate指示重要状态上报
6Authenticated Signed Writes签名写入安全写入
7Extended Properties扩展属性

常用 Descriptor

UUID名称说明
0x2902Client Characteristic Configuration (CCC)开启/关闭 Notify/Indicate
0x2901Characteristic 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 通知 App

MTU 协商

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

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询