The Concept of GATT
To carry out BLE-related development, we must have certain basic knowledge, of course, it must be very simple.
GATT Device role:
The first thing to understand is that the distinction between these two roles is at the hardware level, and they are relative concepts that appear in pairs:
"Central device": relatively powerful, used to scan and connect peripheral devices, such as mobile phones, tablets, etc.
"Peripheral device": the function is relatively simple, the power consumption is small, and the central device is connected to provide data, such as wristbands, smart thermometers, etc.
In fact, at the most fundamental level, it should be a distinction between different roles in the process of establishing a connection. We know that if a Bluetooth device wants to let others know its existence, it needs to continuously broadcast to the outside world, while the other party needs to scan and reply to the broadcast packet, so that the connection can be established. In this process, the person responsible for broadcasting is Peripheral , and Central is responsible for scanning.
Note about the connection process between the two:
The central device can connect to multiple peripheral devices at the same time.Once the peripheral device is connected, it will stop broadcasting immediately, and continue broadcasting after disconnection.Only one device can attempt to connect at any time, queuing connections.
GATT protocol
BLE technology communicates based on GATT. GATT is an attribute transmission protocol. It can be regarded as an application layer protocol for attribute transmission.
Its structure is very simple:
You can understand it as xml:
Each GATT is composed of Services that perform different functions;
Each Service is composed of different Characteristic;
Each Characteristic consists of a value and one or more Descriptors;
Service and Characteristic are equivalent to tags (Service is equivalent to its category, and Characteristic is equivalent to its name), while value actually contains data, and Descriptor is an explanation and description of this value. Of course, we can describe and describe it from different angles. Description, so there can be multiple Descriptors.
For example:The common Xiaomi Mi Band is a BLE device, (assumed) it contains three Services, which are the Service that provides device information, the Service that provides steps, and the Service that detects heart rate;
The characteristic contained in the service of the device information includes manufacturer information, hardware information, version information, etc.; the heart rate Service includes the heart rate characteristic, etc., and the value in the heart rate characteristic actually contains the heart rate data, and the descriptor is the value. Description, such as the unit of value, description, permission, etc.
GATT C/S
With a preliminary understanding of GATT, we know that GATT is a typical C/S mode. Since it is C/S, it is necessary for us to distinguish between Server and client.
"GATT server" vs. "GATT client". The stage where these two roles exist is after the connection is established, and they are distinguished according to the status of the dialogue. It is easy to understand that the party that holds the data is called the GATT server, and the party that accesses the data is called the GATT client.
This is a concept at a different level from the device role we mentioned before, and it is necessary to distinguish it. Let's use a simple example to illustrate:
Take the example of a mobile phone and a watch to illustrate. Before the connection between the mobile phone and the mobile phone is established, we use the Bluetooth search function of the mobile phone to search for the Bluetooth device of the watch. During this process, it is obvious that the watch is broadcasting BLE so that other devices know its existence. , it is the role of peripheral in this process, and the mobile phone is responsible for the scanning task, and naturally plays the role of Center; after the two establish a GATT connection, when the mobile phone needs to read sensor data such as the number of steps from the watch, the two The interactive data is saved in the watch, so at this time the watch is the role of the GATT server, and the mobile phone is naturally the GATT client; and when the watch wants to read SMS calls and other information from the mobile phone, the guardian of the data becomes Mobile phone, so the mobile phone is the server at this time, and the watch is the client.
Service/Characteristic
We have already had a perceptual understanding of them above, and then we have some practical information:
- Characteristic is the smallest logical unit of data.
- The analysis of data stored in value and descriptor is determined by the Server engineer, there is no specification.
- Service/Characteristic has a unique UUID identification, UUID has both 16-bit and 128-bit, what we need to understand is that the 16-bit UUID is certified by the Bluetooth organization and needs to be purchased, of course there are some common ones 16-bit UUID.For example, the UUID of the Heart Rate service is 0X180D, which is expressed as 0X00001800-0000-1000-8000-00805f9b34fb in the code, and other bits are fixed. The 128-bit UUID can be customize.
- GATT connections are exclusive.