低功耗蓝牙——GATT协议详解

2,294 阅读4分钟

GATT协议建立了通过BLE传输user data的方式。GAP协议定义了设备直接交互的协议,而GATT专注于数据传输。GATT使用ATT(Attribute Protocol)传输数据,而ATT利用一组characteristics传输数据,后面会详细介绍。

1.角色

蓝牙协议经常通过定义角色,来开始制定规则。GATT的角色定义为:Client和Server。

Client

Client发送一个requests(and server-initiated updates),并收到responses。BLE连接建立后,由于GATT Client事先无法知道GATT Server的任何属性(attributes),所以Client需要先查询Server提供了哪些服务(Service),这个服务,可以理解为包含一系列characteristics的面向对象的类,Service具体代表什么服务,是根据实际应用场景,双方约定的。在完成了服务搜索之后,如果Client找到了自己需要的Service,就可以对它进行读写操作了。

Server

Server收取Client发送的request,发送responses给Client。服务端也会发送server-initiated updates(这个后面会说明)。Server负责存储和向Client提供这些数据,每一个在销售的BLE设备,都必须包含至少一个GATT Server,甚至是返回error的response。

GATT的这两种角色,和GAP的central/peripheral角色是相互独立的,它们都能作为Client或者Server,甚至能同时作为Client和Server。

2.UUID

UUID是一个128-bit(16bytes)的全球唯一的数字。128-bit占用Link Layer的空间太多,为了提高效率,SIG提供了16-bit和32-bit版本的UUID,这些UUID由Bluetooth SIG管理。

苹果公司的UUID: image.png 心率Service的UUID: image.png GATT Characteristic的UUID: image.png Protocol Identifier的UUID: image.png 要将其转换回128-bitUUID,将其插入xxxxxxxx-0000-1000-8000-00805F9B34FB 即可。 可以看到,公司名称、Service、Characteristic、甚至协议本身、Service的修饰符等等都有一个UUID来表示。 没有在SIG注册的UUID叫做vendor-specific UUIDs,只能全程使用128-bit的值。

3.Attributes

Attributes是GATT中数据的最小单元,Attributes是一段可寻址的、可包含user data的信息。Client和Server交互的所有信息都必须用这种格式。Attributes只是一种概念上的定义,并没有规定一个特定的存储格式。因为其中包含的user data通常是传感器发送的某种数据,这种数据的格式应当保证灵活多变。总的来说,Attributes一般包含Attributes自身的信息和实际数据。以下是Attributes的组成部分: image.png

Handle(句柄)

Handle是一个独特的不可变的16-bit identifier(标识符),handle使得每个Attribute是可寻址的,直接这么说会让人感到迷惑。我们想一下,client端在第一次连接GATT server的时候,不知道server提供了哪些service,客户端可以指定一个handle range,在找到第一个Service之后。

Type

attribute type是个16-, 32-, or 128-bit的UUID,service 、 characteristic就是一种type,厂商自己定义的代表公司名称或者心率等数据的UUID也是一种type。

Permissions

Permissions规定了某个ATT操作是否能在特定的Attributes上执行,Permissions包含:

Access Permissions

规定了client端的读写权限:

  • None - 该attribute不能被client读或写

  • Readable - 该attribute能被client读

  • Writable - 该attribute能被client写

  • Readable and writable - 该attribute能被client读或写

Encryption

决定了加密等级:

  • No encryption required (Security Mode 1, Level 1) - 不加密,明文传输

  • Unauthenticated encryption required (Security Mode 1, Level 2) - 需要加密,但是秘钥可以不用authenticated

  • Authenticated encryption required (Security Mode 1, Level 3) - 需要一个authenticated key

Authorization

是否需要授权:

    • No authorization required 无需授权

    • Authorization required 需要授权

server可以决定attribute之间独立授权或者组合授权

Value

用于放实际数据,没有数据类型限制,大小限制在512字节。用户在有相关权限的情况下,可以自由读写这部分数据。Attribute的其他组成部分则不能修改,也不能直接access,可以通过handle和UUID来访问。 下面看一个# Attribute的整体层级图:

image.png

Attribute在GATT server中必定以上图组合的形式组织:一个GATT server拥有1到多个service,一个service有0到多个characteristics,一个characteristic可以包含0到多个descriptors, 所有使用BLE传输的数据都必须遵循此格式。 Indications and Notifications的区别:

Indications and Notifications are a way for a GATT Client to subscribe to data provided by a GATT Server. A Notification is an unacknowledged message or update while an Indication is an acknowledged message or update. These Notifications and Indications are sent any time the relevant data in the GATT table on the GATT Server is updated. (You must "subscribe" to the data that you would like to be Notified or Indicated of) In a way Indications and Notifications are much like TCP and UDP packets. TCP requires that when data is sent, the receiver acknowledges that the data has been received by sending back an ACKnowledgement packet. UDP just sends off data without any concern whether it is actually confirmed to be received or not. In this sense Indications are akin to TCP and Notifications are akin to UDP.