learn better from others,
be the better one.
—— "Weika Zhixiang"
The length of this article is 3150 words , and it is expected to read for 8 minutes
foreword
It has been more than a month since the last article was published, let me state first, I will not stop updating. There are three reasons for not updating the article for such a long time:
The first reason is that there are really many things at work. I traveled to 17 cities in the past month, basically either at the customer site or on the plane or high-speed train to the next place.
The second reason is that after the last update of " Android Monitoring Message (2) - Phone and SMS Monitoring ", we will start to study the communication of data between the two devices. It is a portable device itself, so the best communication method is Bluetooth. I have done communication with PDAs and Bluetooth printers before, but that is the traditional Bluetooth mode, and it is relatively simple. After researching, I still think Bluetooth BLE is good, but it is different from the traditional one. Bluetooth communication methods are very different, so it is also learning while researching, and the relative time will be longer.
Reason three is nothing but laziness. It is estimated that it is the cycle of the great uncle, but it has been slowly adjusted.
This article is mainly to introduce Bluetooth BLE first, because I am also new to contact, just introduce what I know first.
BlueTooth BLE
Micro card Zhixiang
Introduction
The full name of BLE is Bluetooth Low Energy, Bluetooth low energy technology, which saves more power than traditional Bluetooth, and can perform short-distance communication between Android devices. BLE only connects when needed, which effectively reduces energy consumption. Although the BLE transmission speed is low, it can realize real-time data transmission and two-way communication, which is very suitable for transmitting small amounts of data. In addition, BLE can also be connected with a variety of devices, and can also use the broadcast mode to send information, such as heart rate monitors, fitness equipment, etc.
Bluetooth BLE has two roles, namely central device and peripheral device
-
Peripheral device: Refers to a device with lower power consumption that will continue to broadcast until it is connected to the central device
-
Central device: can scan, look for peripheral broadcasts, and get data from broadcasts
broadcast and connect
It is mentioned above that the peripheral device sends out the broadcast, so I regard it as the Server, and the central device scans and connects, that is the Client.
In terms of the entire connection process, the peripheral device needs to add services first, then send a broadcast, the central device starts scanning, connects after scanning, and then interacts with data.
There are several key points when scanning:
-
We cannot scan the broadcast in the bluetooth setting in the system, and it must be implemented in the App.
-
Scanning needs to apply for Bluetooth permission, so remember to turn this on. Considering that LE beacons are usually associated with locations, the ACCESS_FINE_LOCATION permission must also be declared. Without this permission, the scan will not return any results. In Android 10, you need to turn on GPS, otherwise Bluetooth is not available.
-
In order to prevent the abuse of BLE scanning in Android7, Google has made some restrictions, that is, do not repeatedly turn on and turn off Bluetooth scanning for more than 5 times within 30s. Suggestion: Set the scan cycle to >6s. After the user clicks the scan, do not repeat the scan. You can set a flag to indicate whether it is scanning. If it is scanning, it will not repeat the scan.
GATT protocol
Two roles are defined in the GATT protocol, one is Service and the other is Characteristic. Each Service can contain multiple Characteristics, and they all have specific UUIDs, such as strings like 0000ff00-0000-1000-8000-00805f9b34fb. Each Service represents the ability to provide a certain service, such as a Service related to heart rate; Characteristic represents a key-value pair, and Service achieves the purpose of transmitting data through each key-value pair.
After the master device and the slave device are connected to the GATT protocol, they can ask what services the slave device can provide. After getting the feedback message from the slave device, they can obtain the Service service through the UUID negotiated by both parties, and then use the The UUID obtains the readable Characteristic and the writable Characteristic, and the specific data communication can be realized by operating the Characteristic.
About UUIDs
UUID is "Universally Unique Identifier" and is used to identify Bluetooth services and communication feature access attributes. Different Bluetooth services and attributes use different access methods.
The Bluetooth SIG definition UUID shares a basic UUID:
0x0000xxxx-0000-1000-8000-00805F9B34FB. 128 bits in total
To further simplify the base UUID, each attribute defined by the Bluetooth SIG has a unique 16-bit UUID, replacing the 'x' part of the base UUID above. Use 16-bit UUID for easy memory and operation. For example, SIG defines the 16-bit UUID of "Device Information" as 0x180A.
Therefore, when we communicate with Bluetooth, we can define the above xxxx part to identify our own Bluetooth mode, and we may filter out unnecessary Bluetooth broadcasts through the defined UUID when scanning Bluetooth.
Features and Descriptors
FeatureCharacteristic
Characteristic is the smallest logical unit of Gatt communication. A characteristic contains a single value variable and 0-n descriptors used to describe the characteristic variable. Similar to the service, each characteristic is identified by a 16bit or 32bit uuid. In actual communication, read and write communication is also performed through the characteristic.
Descriptor Descriptor
Its definition is to describe the defined attributes of the GattCharacteristic value, such as specifying readable attributes, acceptable ranges, etc., such as adding descriptors for written characteristics.
Later in my Demo, data communication is mainly performed through Characteristic after connection.
Several key classes in Android BLE
# | class name | illustrate |
---|---|---|
1 | BluetoothAdapter | The Bluetooth module of the device is mapped and obtained through BluetoothManager, but it is finally obtained by calling BluetoothAdapter.getDefaultAdapter() |
2 | BluetoothLeScanner | Classes for scanning and stopping scanning, common methods startScan and stopScan |
3 | BluetoothDevice |
Bluetooth device (that is, peripheral device), the acquisition method getRemoteDevice (Mac address connection), and the above Bluetooth startScan scan, connectGatt connects to Gatt and returns a BluetoothGatt, which is the core method of BLE connection |
4 | BluetoothGatt |
The core class , this object is the encapsulation of the GATT protocol, and the Boolean type parameter indicates whether to reconnect after disconnection. Since information is obtained from a remote device, the Bluetooth device is the server and the mobile phone is the client. The BluetoothGatt object can perform related operations on the client. |
5 | BluetoothGattCallback |
As a parameter of the connectGatt method, it is very important to realize the callback of BluetoothGatt |
6 | BluetoothGattService |
Bluetooth Gatt service, core class, get characteristic through getCharacteristic, getuuid get service UUID |
7 | BluetoothGattCharacteristic |
Characteristics, core classes, data transmission between devices by modifying Characteristic characteristics |
8 | BluetoothGattDescriptor |
Descriptor, core class, ditto |
-
A Gatt contains multiple services; a service contains multiple features; a feature contains multiple descriptors;
-
A descriptor corresponds to a feature; a feature corresponds to a service; a service corresponds to a Gatt
postscript
The Android BLE Bluetooth section is simply introduced. If you understand these basic things, you will not understand anything when running the code. In the next article, you will do a demo to realize Bluetooth BLE communication.
Now the demo is still in progress, mainly because I want to do the demo and add some new things to practice hands, so the demo is also written directly in MVI, and the related communication in BLE is also integrated into a public class as much as possible, so that It can also be directly copied and used during the development stage, and the result is that it takes longer to make.
The study plan for the second half of the year is mainly in Android, mainly in the aspects of multi-process architecture, Android Framework and Jetpack Compose. Of course, if there is something of interest, it may also be inserted.
over
Wonderful review of the past
Android monitoring message (2) - phone and SMS monitoring
Android listening message (1) - application message capture
Test the phone mirroring effect of the new version of Android Studio