From single BLE peripherals to BLE Mesh development, I want to use my spare time to sort out the bits and pieces of Knowledge related to Bluetooth development.
Key word analysis
BLE
Bluetooth Low Energy, Bluetooth 4.0. Its main features are low power consumption and high penetration rate. When it comes to Bluetooth devices, most of them are 4.0 devices. The latest Bluetooth standard is Bluetooth 5.0, which only started shipping chips in 2017 and is not yet widely available.
Peripheral and Central
So most of the development is central connecting to peripheral, like the iPhoneX connecting to a bluetooth light bulb, so the iPhoneX is central, the bluetooth light bulb is peripheral. Peripheral can only be connected by a Central, which can connect multiple Peripheral. When using iPhone as Central to connect bluetooth devices, about 7 devices can be connected (unstable). If too many devices are connected, iPhone bluetooth will restart.
The service characteristic,
The relationship between services and characteristics can be understood by referring to the following figure:1. Each device contains multiple services, and each service corresponds to a UUID. Each service contains multiple characteristic, and each characteristic has a UUID.
- Characteristic is the main source of information and has three permissions: read, write and notify.
agreement
Each specific smart device has a set of data formats agreed upon, and this is the data protocol. For example, 0x01 indicates turning on and 0x00 indicates turning off.
Peripheral mode, central mode
Central mode: APP acts as the center to connect other BLE4.0 peripherals. Peripheral mode: APP simulates a BLE4.0 peripherals and can be connected and used by other devices
Development process (Central mode)
- Establish a central role
- Peripheral Scan (Discover)
- Connect peripherals
- Scan services and features in peripherals (Discover)
- 4.1 Obtaining peripheral Services
- 4.2 Obtaining Characteristics of peripherals, obtaining the values of Characteristics, and obtaining the values of Characteristics Descriptor and Descriptor
- Explore and interact with peripherals
- Subscribe to notifications
- Disconnect
Scanning bluetooth devices
Turn on bluetooth scan, then Bluetooth will scan the surrounding Bluetooth broadcast, the effective distance will be affected by the hardware device, whether there is a shield, device height. When we scan the device, we get the broadcast packet data of the device. In a lot of practical experience, my personal understanding of broadcast packets is that there are two kinds of data in the packet, one is directly written to the message of the broadcast packet, and the other is that it finds you have scanned it and starts to “load” the data for you to see (some broadcast packets will display incomplete information when scanning a large number of devices).
Filtering equipment
In CoreBluetooth’s framework API, scanned broadcast packets are returned to us as dictionary data structures in the delegate. In the dictionary of advertisementData, according to keykCBAdvDataManufacturerData can obtain the information of broadcast packets NSData or localName keykCBAdvDataLocalName access to the equipment:
Copy
NSString *localName = advertisementData[@"kCBAdvDataLocalName"];
NSData *data = advertisementData[@"kCBAdvDataManufacturerData"];
Copy the code
These two attributes are often used to determine whether a Bluetooth device is a peripheral we need to connect to.
Connected devices
After CoreBluetooth connects we get a peripheral object called CBPeripheral and a CBCentralManager object, (center -> peripheral) calling connectPeripheral: connect.
Get services and characteristic
After successfully connecting to the device, you need to find the service and characteristic specified in the protocol to communicate with the device, read and listen.
Write characteristic
Data interaction with bluetooth devices is basically reading and writing, writing data. Characteristic: The channels that write and read data. The data sent and received each time is usually referred to as packet data, and a packet data contains a maximum of 20 bytes