目录
GATT(通用属性协议,Generic Attribute Profile)是 BLE(Bluetooth Low Energy,蓝牙低功耗)通信的核心协议,定义了客户端(Client)和服务器(Server)之间的数据交互方式。它构建在 ATT(Attribute Protocol)之上,负责服务发现、数据读写、通知等操作,适用于各种 BLE 设备,如智能家居、智能穿戴、健康监测等。
1. GATT 结构及基本概念
1.1 GATT 设备角色
在 GATT 交互中,设备分为以下两种角色:
- GATT 服务器(Server):存储数据并向客户端提供访问,如蓝牙窗帘电机。
- GATT 客户端(Client):请求数据或写入数据,如手机 App。
1.2 GATT 关键组件
GATT 设备的数据组织结构如下:
- Profile(配置文件):设备的功能集合,例如心率监测、智能窗帘控制等。
- Service(服务):一组相关的功能集合,如“窗帘电机控制服务”。
- Characteristic(特征):服务内的基本数据单元,如“当前窗帘位置”。
- Descriptor(描述符):存储关于特征的额外信息,如通知的使能状态。
2. GATT 数据组织
GATT 使用层级结构组织数据:
Profile(配置文件)
├── Service(服务)
│ ├── Characteristic 1(特征 1)
│ │ ├── Descriptor 1(描述符)
│ │ ├── Descriptor 2(描述符)
│ ├── Characteristic 2(特征 2)
│ ├── Characteristic 3(特征 3)
示例:蓝牙窗帘 GATT 结构
Curtain Control Profile
├── Curtain Motor Service (UUID: 0xFFF0)
│ ├── Curtain Position Characteristic (UUID: 0xFFF1) - Read, Notify
│ ├── Curtain Control Characteristic (UUID: 0xFFF2) - Write
│ ├── Motor Speed Characteristic (UUID: 0xFFF3) - Read, Write
3. GATT 服务(Services)
3.1 标准 BLE 服务
BLE SIG(蓝牙联盟)定义了一些通用服务,常见的有:
UUID | Service Name | 说明 |
---|---|---|
0x1800 | Generic Access | 设备基础信息(如名称、连接间隔) |
0x1801 | Generic Attribute | GATT 相关的服务 |
0x180A | Device Information | 设备信息(型号、固件版本) |
0x181D | Weight Scale | 体重秤数据 |
0x180F | Battery Service | 设备电量信息 |
3.2 自定义服务
设备厂商可以定义自己的 UUID 进行 GATT 交互,如:
UUID | Service Name | 说明 |
---|---|---|
0xFFF0 | Curtain Motor Service | 窗帘电机控制服务 |
0xFFF5 | Temperature Service | 自定义温度数据 |
0xFFE0 | UART Over BLE | 串口透传服务 |
4. GATT 特征(Characteristic)
**Characteristic(特征)**是 GATT 交互的核心,包含:
- Handle(句柄):特征的唯一索引,用于 GATT 操作。
- UUID:唯一标识特征。
- Properties(属性):特征支持的操作(如读、写、通知)。
- Value(值):存储的数据,如温度、电机状态等。
4.1 Characteristic 属性
特征的 Properties 决定了客户端可以执行的操作:
Property | 说明 |
---|---|
Read | 客户端可以读取数据 |
Write | 客户端可以写入数据 |
Write Without Response | 客户端可以无确认写入 |
Notify | 服务器可以主动发送数据(无确认) |
Indicate | 服务器可以主动发送数据(有确认) |
4.2 示例:窗帘控制特征
UUID | Name | Property | 说明 |
---|---|---|---|
0xFFF1 | Curtain Position | Read, Notify | 获取窗帘当前位置 |
0xFFF2 | Curtain Control | Write | 发送开关指令 |
0xFFF3 | Motor Speed | Read, Write | 读取或设置电机速度 |
5. GATT 数据交互流程
GATT 设备的交互通常包括以下步骤:
5.1 发现服务
客户端连接 BLE 设备后,首先需要发现可用的 GATT 服务:
- Discover Primary Services:获取所有服务的 UUID 和句柄。
- Discover Characteristics:获取服务下的所有特征。
5.2 读取数据
客户端想要获取某个特征值时:
- 发送
Read Request
请求特征值。 - 服务器返回
Read Response
并附带特征值。
示例:读取窗帘位置
Client: Write Request -> UUID: 0xFFF2, Value: 0x01
Server: (执行命令,不返回响应)
5.3 写入数据
客户端向设备写入数据:
- 发送
Write Request
请求修改特征值。 - 服务器返回
Write Response
(如果是 Write Without Response,则不需要返回)。
示例:控制窗帘打开
Client: Subscribe Notification -> UUID: 0xFFF1
Server: Notify -> Value: 0x60 (60%)
Server: Notify -> Value: 0x80 (80%)
Server: Notify -> Value: 0xFF (100%)
5.4 订阅通知
客户端订阅 Notify
以监听特征值变化:
- 向 CCC(Client Characteristic Configuration) 写
0x01
启用通知。 - 服务器在特征值变化时主动发送
Notification
。
示例:窗帘移动
Client: Subscribe Notification -> UUID: 0xFFF1
Server: Notify -> Value: 0x60 (60%)
Server: Notify -> Value: 0x80 (80%)
Server: Notify -> Value: 0xFF (100%)
6. GATT 操作命令
操作 | 说明 |
---|---|
Discover Services | 发现 GATT 服务器的服务 |
Discover Characteristics | 发现服务下的特征 |
Read Request | 读取特征值 |
Write Request | 写入特征值(需要响应) |
Write Without Response | 写入特征值(无响应) |
Enable Notifications | 订阅特征值更新 |
Enable Indications | 订阅带确认的特征值更新 |
7. 真实应用案例:蓝牙窗帘电机
7.1 GATT 结构
UUID | 名称 | Property |
---|---|---|
0xFFF0 | Curtain Motor Service | - |
0xFFF1 | Curtain Position | Read, Notify |
0xFFF2 | Curtain Control | Write |
0xFFF3 | Motor Speed | Read, Write |
7.2 交互流程
- App 连接窗帘电机
- 读取窗帘位置 Read(0xFFF1) -> 0x50 (50%)
- 写入控制命令 Write(0xFFF2, 0x01) -> 窗帘打开
- 接收窗帘位置更新 Notify(0xFFF1) -> 0x60, 0x70, 0xFF