参考
【 https://blog.csdn.net/utadalight/article/details/80039022 】
【 https://www.cnblogs.com/hzl6255/p/4141505.html 】
【 https://blog.csdn.net/yueqian_scut/article/details/50752314 】
【 http://leconiot.com/download/cc2640r2f/ble_stack_app/stack/gatt/gatt.html 】
有大量删改。
CONTENTS [hide]
什么是Attribute protocol
简单来说,这个协议就是用来给Server和Client进行通信的协议。Server端保存有一个类似“属性数据库”的东西,包含了一系列的属性及其特性。而Client端可以通过ATT协议从Server端获取这些属性。再具体一些,Client可以查询(Discover)、读取(read)甚至配置(write)Server中保存的属性。通常是在配置之后,Server端可以实时的告知Client端属性值的变化。通知可以是无需Client应答的(notification),也可以是需要Client响应的(indication)。
Attribute介绍
server端保存了一系列的attribute,这些attribute由四个基本元素组成:
attribute handle attribute type attribute value attribute permissions
Attribute handle其实就是一个属性在server端的索引。它对每个属性来说都是唯一的,不可有重复的值,否则Client端无法准确定位到某一个属性。Handle的范围是 [0x0001, 0xFFFF)。
Attribute type是属性的类型,ATT中使用一个(16/128bits)的UUID来表示。在使用过程中,为了提高效率,使用的是16-bits Attribute UUID。
128-bit UUID = 16-bit Attribute UUID*2^96 + Bluetooth_Base_UUID |
Bluetooth_Base_UUID = 00000000-0000-1000-8000-00805F9B34FB |
更简单的办法如下(xxxx代表十六进制的16-bit UUID)
0000xxxx-0000-1000-8000-00805F9B34FB
Attribute value是一个属性的值。
Attribute permissions描述了一个属性的访问权限,包括
read write encryption authentication authorization
比较特殊的是,这些权限由上层定义,对于ATT来说却是不可见的,Client无法获取到Server中属性的permissions。
以上结构,可以Generic Attribute Profile(GATT Profile)当中的图来诠释:
GATT介绍
GATT(Generic Attribute Profile)描述了一种使用ATT的服务框架。该框架定义了服务(Server)和服务属性(characteristic)的过程(Procedure)及格式。
Procedure定义了characteristic的发现、读、写、通知(Notifing)、指示(Indicating)及配置characteristic的广播。
GATT可以配置为如下两种角色(Role):
– Client : 命令、请求发起方
– Server : 命令、请求接收方
GATT Profile Hierarchy
GATT指定了数据交互的结构(Structure);这个结构体定义了一些基本元素,如Service、Characteristic,这些元素存在于Attribute中。如下图所示:
GATT中最上层是Profile,Profile由一个或多个服务(Service)组成,服务是由Characteristics组成,或是其他服务的引用(Reference),Characteristic包含一个值(Value),可能包含该Value的相关信息。
一个或者多个Characteristic组成一个Service,一个多个Service组成Profile,Characteristic又由多个Attributes组成,每个Attribute由包含 Handle、Type、Permissions三个属性。
Characteristic包含以下4个Attributes:
Characteristic Value(特征值)用于characteristic的值
Characteristic Declaration(特征声明)存储特征值的属性,位置和类型的描述符
Client Characteristic Configuration(客户端特征配置) 允许GATT服务器配置要通知的特性(异步发送消息)或指示的配置(与确认异步发送消息)
Characteristic User Description(特征用户描述)描述特征的ASCII字符串
以上的每个Attributes 又由以下元素组成。
Handle(句柄)表中属性的索引(每个属性都有一个唯一的句柄)
Type(类型)指示属性数据表示什么(称为UUID [通用唯一标识符])
Permissions(权限)强制GATT客户端设备如何以及如何访问属性的值
Service概念介绍
Service是[数据]和与之关联的[完成某个特定功能的行为]/[特性]的集合
在GATT中,一个服务由服务定义(Service Defintion)来实现。
一个服务定义可能包含引用服务(Reference Service)、强制Characteristic和可选Characteristic。
为了实现旧版本的兼容,新版本中服务定义只能增加新的引用服务或可选Characteristic,新版本中的服务定义禁止从旧的服务定义中改变行为。
Server有两类:
– Primary Service : 拥有基本功能的服务,可被其他服务包含,可以通过Primary Service Discovery过程来发现
– Secondary Service : 仅用来被Primary/Other Secondary Service、高层协议引用的服务
Attribute Handle | Attribute Type | Attribute Value | Attribute Permission |
0xNNNN | 0x2800 – UUID for <Primary Service> 0x2801 – UUID for <Secondary Service> |
16-bit Bluetooth UUID 128-bit UUID for Service |
Read Only, No Authentication, No Authorization |
Included Service概念介绍
一个Included Service是一种引用已存在服务的方法,具体办法为在服务定义的开始加上Included Service的引用,这样整个Included Service定义成为新服务定义的一部分。
Attribute Handle | Attribute Type | Attribute Value | Attribute Permission | ||
0xNNNN | 0x2802 – UUID for<Include> |
Included Service Attribute Handle |
End Group | Handle Service UUID | Read Only, No Authentication, No Authorization |
Characteristic概念介绍
Characteristic由Characteristic Definition定义,包含一个Characteristic声明、Characteristic属性、值、值的描述(Optional)。
Characteristic Declaration:
Attribute Handle | Attribute Type | Attribute Value | Attribute Permission | ||
0xNNNN | 0x2803 – UUID for Characteristic |
Characteristic Properties |
Characteristic Value Attribute Handle |
Characteristic UUID |
Read Only, No Authentication, No Authorization |
Characteristic Value Declaration:
Attribute Handle | Attribute Type | Attribute Value | Attribute Permissions |
0xNNNN | 0xuuuu – 16-bit Bluetooth UUID or 128-bit UUID for Characteristic UUID |
Characteristic Value | Higher layer profile or implementation specific |
发表评论