Bluetooth technology changes the wireless communication between devices with its universality and simplicity. Because of its low power consumption and low cost, it plays an important role in the development of health care, sports fitness, home building automation, beacon and other application fields. Bluetooth brings us a lot of convenience, but for some novices, before developing Bluetooth, it is necessary to have a detailed understanding of its related knowledge. Here, the cloud things mainly introduce some knowledge points and development process of Bluetooth application development.
1、 Bluetooth development must know the concept
Central and peripheral
In the process of application development, Bluetooth has two roles: Central and peripheral. Chinese is the center and peripheral. For example, if a mobile phone is connected to a smart device, the mobile phone is central, and the smart device is peripheral. Most of the time, central connects to the peripheral scene, so let’s talk about its process. Broadcast and connectivity
Peripheral will send an advertisement. After central scans the broadcast, it can connect the device and send a connect request. After receiving the request and agreeing to connect, central and peripheral establish a connection. Each specific intelligent device of the protocol has agreed on a set of data formats, which is the data protocol. It should be noted that the device side is small end mode, so when reading data, the high byte is in the front and the low byte is in the back.
2、 General development process of Bluetooth application
Taking IOS as an example, Android is similar to this one.
① Establish a central role
② Scan peripheral (discover)
③ Connect peripherals
④Scan the services and characteristics of peripheral devices (discover) * 4.1 to obtain services * 4.2 to obtain characteristics of peripheral devices, to obtain the values of characteristics, and to obtain the values of descriptors and descriptors of characteristics
⑤Explore and interact with peripherals
⑥ Subscribe to the notification of characteristic
3、 Data exchange of Bluetooth
write，read，notify，indecate，response or not … Reading and writing are easy for everyone to understand. Indecate and notify correspond to a long connection. After indecate is established, peripheral can send data to central at any time. The difference between indecate and notify is that indecate is bound to receive data, and notify may lose data (there will be no response from central). Write can also be divided into response and norresponse. If it is response, the write will successfully recover the confirmation message from peripheral, but the write rate will be reduced. For a charateristic, its read-write subscription permission is determined by peripheral. Familiarity can be set at the same time, which is generally determined according to the functions of peripheral devices.
4、 Bluetooth OTA DFU
Bluetooth OTA, DFU (device firmware update) refers to the firmware upgrade of Bluetooth devices. In fact, it is a whole process. Different Bluetooth chips have different OTA processes. Using Ti chip as an example, the steps are: switch system, restart, transfer data, verify data, switch system, restart, complete. Note: the data transmission will also be divided into many sections to send, can not send a piece of data, do a data verification.
5、 Problems of Ota
Take ti’s chip as an example. It needs to store two images, and the space required for data transmission is relatively large. However, the speed, power consumption and storage of each intelligent device are subject to many restrictions. As a result, many devices will implement OTA functions, customize processes and data transmission methods,
As a result, many devices have their own private OTA mode and protocol, so when developing, you should carefully read the description of Ota in the device protocol.
6、 Common problems in Bluetooth development
① How to do automatic reconnection
In fact, automatic reconnection is much simpler than expected. Whether it is Android or IOS side, you only need to call it again in the delegate method of device disconnection gatt.connet Or is it centralManager.connet The method is OK. No matter whether the device is a little bit or not, it will be connected automatically when the device meets again or is connected to the connectable range.
② Connection failure handling
For the two platforms, IOS side has connection failure delegation, but this rarely happens; for the same device, Android often fails to connect, status= Bluetooth Gatt. GATT_ For the development of success and Android side, please do not put the connection failure and disconnection together, because the disconnection can directly try to re connect, and when the connection fails, you need to add some delay and need to gatt.close To clear the status, otherwise the GATT will be blocked, causing the mobile phone to no longer be able to connect to any device without restarting Bluetooth.
IOS background running, need to be in the device info.Plist jurisdiction, key:Required background Modes, value: Bluetooth central (mobile phone as central), Bluetooth peripheral (mobile phone as peripheral)
④Connect multiple devices at the same time
Android is very simple. Create multiple gattcallbacks, and each gattcallback separately manages the operation after the device is connected. It is better not to create multiple cbcentral managers for IOS. Multiple cbcentral managers can be used in theory, but there will be multiple mobile phones
There are different behaviors in the version, and there are some problems that are easy to make mistakes. I will not elaborate on this content. The same cbcentral manager is used to distinguish different devices and perform different operations and processing by entering the identifier of the delegated peripheral.
Post time: Jul-23-2020