蓝牙 GATT 协议

蓝牙 GATT 协议

GATT (Generic Attribute Profile)通用属性配置文件 协议是 ATT 的上层协议。 GATT 协议下,将蓝牙设备分为以下两种 Client: 客户端。 发送命令(command)、请求(request)、确认信息(confirmation)给服务端。 Server:服务端

GATT (Generic Attribute Profile)通用属性配置文件 协议是 ATT 的上层协议。

GATT 协议下,将蓝牙设备分为以下两种

  1. Client: 客户端。
    • 发送命令(command)、请求(request)、确认信息(confirmation)给服务端。
  2. Server:服务端。
    • 通过 响应(response)、通知(notification)、指示(indication)返回数据给客户端。
    • 数据存储在服务器上,数据格式以一种 Profile (配置文件)的方式存储。

6. 蓝牙 GATT 协议-20240622022850548.webp

Service

Service Declaration(服务声明)

3.1 SERVICE DEFINITION

[[Core_v5.4.pdf#page=1474&selection=13,0,15,18|Core_v5.4, page 1,474]]

6. 蓝牙 GATT 协议-20240622025500048.webp

  1. Attribute Handle: 属性句柄
  2. Attribute Type: 属性格式
  3. Attribute Value: 属性值
  4. Attribute Permission: 属性权限

客户端如果读到 属性类型 是 0x2800,那么就知道这一串数据是 服务声明 的数据,而且 0x2800 指 主服务。
读到 UUID(Universally Unique Identifier,通用唯一识别码) 之后,也就知道了当前服务的 UUID,识别码[[Assigned_Numbers.pdf]]。

Service Include (服务包含)

3.2 INCLUDE DEFINITION

[[Core_v5.4.pdf#page=1475&selection=12,0,14,18|Core_v5.4, page 1,475]]

表明当前的 Service 可以包含的其他的 Service。

6. 蓝牙 GATT 协议-20240622030757178.webp

如果读到属性类型中有 0x2802,就表明当前 Service 有包含其他的 Service。

  • Include Service Attribute Handle: 起始的句柄位置。
  • End Group Handle: 结束句柄的位置。
  • Service UUID: 包含服务的 UUID。

特征值

特征值构成:

1. 特征声明(Characteristic Declaration)

3.3.1 Characteristic declaration

[[Core_v5.4.pdf#page=1476&selection=17,0,17,32|Core_v5.4, page 1,476]]

6. 蓝牙 GATT 协议-20240622031254942.webp

读取到属性类别是 0x2803,就知道当前的这一块数据描述的是特征值。

  • Characteristic Properties: 属性。
  • Characteristic Value Attribute Handel: 特征值的句柄。
  • Characteristic UUID: 特征值的 UUID。

2. 特征值声明(Characteristic Value Declaration)

3.3.2 Characteristic Value declaration

[[Core_v5.4.pdf#page=1478&selection=33,0,33,38|Core_v5.4, page 1,478]]

6. 蓝牙 GATT 协议-20240622032108126.webp

在前面获取了特征的 UUID,那么在这里就会再次读到 UUID,然后可以取得相应的特征值。

3. 可选的补充信息的(Descriptor Declaration)

此处可能包含 6 个部分。

1. 扩展属性(Extended Properties)

3.3.3.1 Characteristic Extended Properties

[[Core_v5.4.pdf#page=1479&selection=12,0,12,42|Core_v5.4, page 1,479]]

6. 蓝牙 GATT 协议-20240622033520288.webp

扩展属性的识别码是 0x2900。

2. 用户属性(User Description)

3.3.3.2 Characteristic User Description

[[Core_v5.4.pdf#page=1479&selection=66,0,66,39|Core_v5.4, page 1,479]]

6. 蓝牙 GATT 协议-20240622033713956.webp

用户属性识别码是 0x2901,特征值是一个字符串。

3. 客户端配置属性(Client Configure)

3.3.3.3 Client Characteristic Configuration

[[Core_v5.4.pdf#page=1480&selection=28,0,28,43|Core_v5.4, page 1,480]]

6. 蓝牙 GATT 协议-20240622033853626.webp

客户端属性识别码为 0x2902,可以配置服务端的行为。

4. 服务端配置属性

3.3.3.4 Server Characteristic Configuration

[[Core_v5.4.pdf#page=1481&selection=23,0,23,43|Core_v5.4, page 1,481]]

6. 蓝牙 GATT 协议-20240622034008836.webp

服务端属性识别码为 0x2903。

5. 显示格式特征值

3.3.3.5 Characteristic Presentation Format

[[Core_v5.4.pdf#page=1482&selection=37,0,37,42|Core_v5.4, page 1,482]]

6. 蓝牙 GATT 协议-20240622034231913.webp

这个属性关系到如何解析特征值。

6. 集合属性的格式(Aggregate Format)

3.3.3.6 Characteristic Aggregate Format

[[Core_v5.4.pdf#page=1484&selection=49,0,49,39|Core_v5.4, page 1,484]]

![[6. 蓝牙 GATT 协议-20240622034807706.webp|800]]
6. 蓝牙 GATT 协议-20240622034807706.webp

紧接着这一个声明之后,可能又是一个扩展属性的声明,也可能是另外一个 Service 的声明。可以通过属性种类来判断。


数据格式

通过查找表的方式,可以读取服务器上,所有服务的数据。

2.5 ATTRIBUTE PROTOCOL

[[Core_v5.4.pdf#page=1463&selection=34,0,36,18|Core_v5.4, page 1,463]]

所有的数据都遵守这一种格式:

6. 蓝牙 GATT 协议-20240622035308065.webp

  • 属性句柄,两个字节,从 0001 到 FFFF。
  • 属性类型
  • 属性的值
  • 属性权限

这个格式即 ATT 属性协议。GATT 赋予了这些属性不同的类型,根据对类型的判断来判断读取的数据是哪一种类型的数据,从而解析属性值的格式和内容,还可以读取下一条数据。

对于 GATT 来说,不但是使用 ATT协议 来存储数据,它还使用 APP(Attribute Protocol PDU)协议来发送各种数据。
蓝牙的 BLE 就是基于 GATT 的模式,来进行数据的沟通。

Comment