What's GATT and GAP
First, let’s get to learn some basic introductions related to the protocol:
Profile is a specification, a standard communication protocol, which exists in the Bluetooth slave (server);
Bluetooth SIG stipulates some standard profiles, such as HID OVER GATT, anti-lost device, heart rate meter, etc.;
Each profile will contain multiple services, and each service represents a capability of the slave.
Service can be understood as a service, there are multiple services in the BLE slave, for example: power information service, system information service, etc.;
Each service contains multiple characteristic values.
Each specific characteristic value is the subject of BLE communication. For example, the current power is 80%, and the characteristic value of the power is stored in the profile of the slave, so that the host can read the 80% data through this characteristic.
Characteristic, BLE master-slave communication is realized through characteristic, which can be understood as a label, through which the desired content can be obtained or written.
UUID, unified identification code, service and characteristic need a unique UUID to identify;
Each slave will have a profile, whether it is a custom simple profile or a standard anti-lost profile, they are composed of some services, each service contains multiple characteristics, communication between the host and the slave. Are realized through characteristic.
What’s BLE GATT?
Bluetooth Low Energy (BLE) connections are now established on the GATT (Generic Attribute Profile) protocol. GATT is a general specification for sending and receiving short data segments over a Bluetooth connection. These short data segments are called attributes.
GAP (Generic Access Profile), which is used to control device connection and broadcast.
GAP makes your device visible to other devices, and determines whether or how your device can interact with the contract device;
e.g: Beacon device only broadcasts to the outside and does not support connection. Some bracelet can connect to the central device.
GAP defines several roles for devices, the main two of which are: Peripheral (Peripheral-Slave-Server) and Central Device (Central-Host-Client).
This is generally a very small or simple low-power device that is used to provide data and connect to a more relatively powerful central device, such as bracelet;
The central device is relatively powerful and used to connect to other peripheral devices. For example, mobile phones, etc.;
In GAP, peripheral devices broadcast data in two ways:
Advertising Data Payload and Scan Response Data Payload
Each type of data can contain up to 31 bytes. Broadcasting data is necessary here, because peripherals must continuously broadcast to the outside to let the central device know its existence;
The scan response is optional. The central device can request a scan response from the peripheral, which contains some additional information about the device, such as the name of the device.
Broadcast network topology
In some cases, peripherals broadcast themselves to let the central device discover itself, and establish a GATT connection, so as to exchange more data;
In some cases, connection is not required, as long as the peripheral broadcasts its own data, the main purpose of this method is to allow the peripheral device to send its own information to multiple central devices;
Because based on the GATT connection method, only one peripheral can be connected to a central device. The most typical application using broadcast is Apple's iBeacon.
The full name of GATT is Generic Attribute Profile, which defines two BLE devices to communicate through Service and Characteristic;
GATT uses the ATT (Attribute Protocol) protocol. The ATT protocol saves the data corresponding to Service and Characteristic in a lookup table. The lookup table uses 16bit ID as the index of each item;
Once the two devices have established a connection, GATT starts to work, which also means that you must complete the previous GAP protocol;
What needs to be explained here is that the GATT connection must go through the GAP protocol first. In fact, in our Android development, we can directly use the MAC address of the device to initiate a connection without going through the scanning step;
This does not mean that there is no need to go through GAP. In fact, it has already been done for you at the chip level. The Bluetooth chip initiates a connection. It always scans the device first, and then initiates the connection after the scan is reached;
GATT connection needs special attention: GATT connection is exclusive. That is, a BLE peripheral can only be connected by a central device at the same time;
Once the peripheral is connected, it will immediately stop broadcasting, so that it is invisible to other devices. When the device is disconnected, it will start broadcasting again;
If the central equipment and peripherals need two-way communication, the only way is to establish a GATT connection.
Network topology of GATT connection
A peripheral can only be connected to one central device, and a central device can be connected to multiple peripherals. Once the connected topology has established a connection, the communication is two-way. Compared with the previous GAP broadcast network topology, GAP communication is one-way. If you want the peripherals of two devices to communicate, you can only transfer through the central device.
GATT communication transaction
The two parties of GATT communication have a C/S relationship, and the peripheral acts as a GATT server (Server), which maintains the ATT look-up table and the definition of service and characteristic;
The central device is a GATT client (Client), which initiates a request to the Server. It should be noted that all communication events are initiated by the client (also called the master device, Master) and receive the server (also called the slave device). , Slave) response;
Once the connection is established, the peripheral will suggest a connection interval (Connection Interval) to the central device, so that the central device will try to reconnect at each connection interval to check whether there is new data;
However, this connection interval is just a suggestion. Your central device may not strictly follow this interval. For example, your central device is busy connecting to other peripherals, or the central device resources are too busy;
GATT transactions are based on nested Profiles, Services and Characteristics.
Profile does not actually exist on BLE peripherals, it is just a collection of Services predefined by Bluetooth SIG or peripheral designers;
For example, the Heart Rate Profile (Heart Rate Profile) is a combination of Heart Rate Service and Device Information Service;
Service is to divide the data into independent logical items. It contains one or more Characteristic. Each Service has a unique UUID. The UUID is 16bit or 128bit. The 16bit UUID is officially certified and costs money. Buy, 128 bit is customized, this can be set by yourself;
Taking Heart Rate Service as an example, you can see that its official 16bit UUID is 0x180D, which contains 3 Characteristic: Heart Rate Measurement, Body Sensor Location and Heart Rate Control Point, and it is defined that only the first one is necessary, it is Optional implementation;
The lowest category of Characteristic in GATT transactions is Characteristic. Characteristic is the smallest logical data unit. Of course, it may contain a group of associated data, such as the X/Y/Z three-axis value of the accelerometer.
Similar to Service, each Characteristic uses a 16bit or 128bit UUID unique identification. You can use the standard Characteristic officially defined by the Bluetooth SIG for free. Using the official definition can ensure that the BLE software and hardware can understand each other;
Heart Rate Measurement Characteristic, this is the Characteristic that the Heart Rate Service mentioned above must implement, and its UUID is 0x2A37. Its data structure is that the heart rate data format is defined in 8 bits at the beginning, and the actual heart rate data in the corresponding format is next;
The main way to deal with BLE peripherals is through Characteristic. You can read data from Characteristic or write data to Characteristic, so that two-way communication is realized. So you can implement a service similar to a serial port (UART). This Service contains two characteristics, one is configured as a read-only channel (RX), and the other is configured as a write-only channel (TX).