【所有文章汇总】
文章目录

ATT之所以称作“protocol”,是因为它还比较抽象,仅仅定义了一套机制,允许client和server通过Attribute的形式共享信息。而具体共享哪些信息,ATT并不关心,这是GATT(Generic Attribute Profile)的主场。
GATT相对ATT只多了一个‘G‘,但含义却大不同,因为GATT是一个profile(更准确的说是profile framework)。
1、简介
GATT :Generic Attributes通用属性,低功耗蓝牙的设备之间的通信协议。
The Generic Attribute Profile (GATT) defines a service framework using the Attribute Protocol.
GATT使用ATT协议,定义了一套服务框架,用于发现、读写、通知、广播信息的配置。
角色的定义
为了更好的实现GATT的profile概要文件,GATT定义了如下角色:
- Client:主要向服务器发送命令和请求,接收服务器的响应
- Server:接收命令和请求,发送响应

2、GATT协议框架
GATT Profile定义了用于通信的数据结构,包括 services 和characteristics。

由上图可知,GATT profile的层次结构依次是:Profile—>Service—>characteristic
-
最顶层的是
profile,一个profile由一个或者多个service组成。 -
一个
service又由characteristics组成。 -
每个
characteristic由property、value、descriptor组成。
2.1 Profile
GATT Profile可以理解为一种规范,一个标准的通信协议,它存在于从机中。它定义了如何使用ATT协议去广播、发现、读写相关属性信息。
完整的Attribute Type的类型如下图:

2.2 Service
2.2.1 Service 定义
Service服务是完成特定功能或特性的数据和相关行为的集合。
在BLE从机中,通过有多个服务,例如电量信息服务、系统信息服务等,每个service中又包含多个characteristic特征值。每个具体的characteristic特征值才是BLE通信的主题。
比如当前的电量是80%,所以会通过电量的
characteristic特征值存在从机的profile里,这样主机就可以通过这个characteristic来读取80%这个数据。
一个Service的定义包括了:Service声明、(可选的)include定义、(可选的)characteristic定义。
2.2.2 Service 声明
Service Declaration如下:

-
Attribute Type:服务类型有两种,分别为
primary service和secondary service,即首要服务和次要服务,对应的Attribute Type为0x2800或0x2801, -
Attribute Value:是一个16位
UUID或者128位的UUID。用来表示这是什么服务。 -
Attribute permission:表示这个该声明即这个ATT属性是只读的、无需验证的、无需授权的。
2.3 include
2.3.1 include 定义
include用于去引用已定义存在的服务,相当于C语言中的include包含。
include定义仅仅包含一个include Declaration。
2.3.2 include 声明
include Declaration如下:

- Attribute Type:服务类型为
include,对应的Attribute Type为0x2802。 - Attribute Value:设置为所要引用的服务的
Attribute Handle、End Group Handle、UUID - Attribute permission:表示这个该声明即这个ATT属性是只读的、无需验证的、无需授权的。
2.4 Characteristic
2.4.1 Characteristic 定义
characteristic包含了特征和值的定义。
BLE主从机的通信均是通过characteristic来实现,可以理解为一个标签,通过这个标签可以获取或者写入想要的内容。
characteristic的定义包含了characteristic declaration,Characteristic Value declaration,characteristic descriptor declaration,其中前两个属性是必须的,最后一个属性是可选的。
characteristic = characteristic声明 + characteristic value声明 + 可选的descriptor声明。
2.4.2 Characteristic 声明
Characteristic declaration如下:

- Attribute Type:服务类型为
Characteristic,对应的Attribute Type为0x2803。 - Attribute Value:设置为
Characteristic Properties、Characteristic Value Attribute Handle、Characteristic UUID。 - Attribute permission:表示这个该声明即这个ATT属性是只读的、无需验证的、无需授权的。
Attribute Value字段如下:

一个服务,可能有多个
characteristic definitions用相同的Characteristic UUID。
- Characteristic Properties字段如下:

-
Characteristic Value Attribute Handle:包含了
characteristic value的句柄 -
Characteristic UUID:是一个16位
UUID或者128位的UUID。
2.4.3 Characteristic Value 声明
Characteristic Value Declaration 如下:

Characteristic Value是
characteristic声明后的第一个属性。
- Attribute Type:该字段的值设置为
Characteristic声明时的UUID - Attribute Value:为所声明的
Characteristic中相同UUID的属性的值。 - Attribute permission:由上层权限指定
其与Characteristic Declaration关系如下:

2.4.4 Characteristic Descriptor 声明
Characteristic descriptors主要用于包含一些与Characteristic Value相关的信息,GATT定义了多种可选descriptors,如下:

每一种
descriptor都有其特有的格式。
以
Characteristic User Description为例:

- Attribute Type:该字段的值设置为
Characteristic user description,对应的值为0x2901 - Attribute Value:为定义的字符串信息
- Attribute permission:由上层权限指定
3、参考资料
[1]:https://blog.csdn.net/chengbaojin/article/details/108145952?spm=1001.2101.3001.6661.1