Bluetooth Low Energy简介
Android 4.3(API 18)才正式支持BLE，也称蓝牙4.0。Android提供了相应的API，可以扫描设备、连接设备，设备通讯。对比传统的蓝牙，BLE的设计能够显著减低功耗。手机完全可以一直开着蓝牙。
PHY ： 物理层 负责2.4 GHz信号传输
LL ： 链路层 controls the RF state of the transceiver, determining whether it’s advertising, scanning, initiating, connected, or standing by.
ATT ： enables a device to reveal certain of its attributes to another device. The ATT block sets up peer-to-peer communication between an attribute server and a client to be able to exchange this information over a dedicated L2CAP channel.
SM ： 安全管理(密码、加密啥的)
GATT：defines the sub-procedures for using ATT and specifies the structure of Bluetooth profiles. All communications between Bluetooth devices are handled through GATT sub-procedures.
GAP ：provides the interface between the application and Bluetooth profiles and handles device discovery, connection, and services, including security procedures.
Generic Access Profile(GAP)
GAP主要完成蓝牙设备发现、蓝牙设备连接、蓝牙服务发现，主要的两个角色：Central devices and Peripheral devices
Peripherals devices发送广播包，广播包可以包含一些有用的信息： 设备名称、设备mac地址(iOS不能获取蓝牙设备mac地址)等
Central devices, 扫描和监听Peripherals devices的广播包如下图，扫描到目标设备后， Central device可以发起连接Peripheral device请求
当peripheral and a central device连接成功后, 就可以使用GATT services 和 characteristics 通讯了.
The sole building block of ATT is the attribute. An attribute is composed by three elements:
- a 16-bit handle;
- an UUID which defines the attribute type;
- a value of a certaing length.
Most of the ATT protocol is pure client-server: client takes the initiative, server answers. But ATT has notification and indication capabilities, in which the server takes the initiative of notifying a client that an attribute value has changed, saving the client from having to poll the attribute.
Generic Attribute Profile(GATT)
GATT is a base profile for all top-level LE profiles. It defines how a bunch of ATT attributes are grouped together into meaningful services.
The GATT client corresponds to the ATT client discussed in Attribute Protocol (ATT). It sends requests to a server and receives responses (and server-initiated updates) from it. The GATT client does not know anything in advance about the server’s attributes, so it must first inquire about the presence and nature of those attributes by performing service discovery. After completing service discovery, it can then start reading and writing attributes found in the server, as well as receiving server-initiated updates.
The GATT server corresponds to the ATT server discussed in Attribute Protocol (ATT). It receives requests from a client and sends responses back. It also sends server-initiated updates when configured to do so, and it is the role responsible for storing and making the user data available to the client, organized in attributes. Every BLE device sold must include at least a basic GATT server that can respond to client requests, even if only to return an error response.
It is worth mentioning once more that GATT roles are both completely independent of GAP roles (see Roles) and also concurrently compatible with each other. That means that both a GAP central and a GAP peripheral can act as a GATT client or server, or even act as both at the same time.
Service is collections of characteristics and relationships to other services that encapsulate the behavior of part of a device.
A characteristic is a value used in a service along with properties and configuration information about how the value is accessed and information about how the value is displayed or represented.
Characteristic descriptors are used to contain related information about the characteristic Value.
All the Information of services, characteristics and descriptors are combined together and made a Profile. Created profile data is then converted into HEX format file which is non readable by humans for security aspects. This File is used as Input for configure GATT Layer.