1 Preface
There are now more and more wearable devices using Bluetooth 4.0 BLE. For iOS development, Apple has previously launched the Corebluetooth framework to support BLE development. A friend who knows about hardware development should know that before using a device with a lower version of Bluetooth, to connect to an iOS device, you need to register MFI and have MFI protocol to develop accordingly. If you look at my previous study of Lego EV3, I can see that the EV3 is using Bluetooth 2.1, so the MFI protocol is needed for development.
This article will explain in step-by-step how to use the Corebluetooth framework to communicate with various wearable devices, using Xiaomi bracelet for basic testing.
2 Development environment
1 Macbook Pro Mac OS X 10.10
2 Xcode 6.3.2
3 IPhone 5s v8.1
4 Xiaomi Bracelet
3 Basic Flow
To develop Bluetooth, you need to have a basic understanding of the entire communication process. Here I extract some pictures of Apple's official document Core Bluetooth Programming Guide. This document is actually written for the development process is very clear, we would be better to look at.
3.1 How wearable devices and iOS are connected
As you can see from the above picture, our iOS device is central for receiving data and sending commands, while peripherals such as Xiaomi bracelet are peripheral, transmitting data outward and receiving commands. All we have to do is connect the peripheral through central, and then implement the data receiving and sending of control instructions. After this step, according to the specific hardware, docking received data for parse parsing.
3.2 Data structure of wearable device Bluetooth
Here is the heart rate device to do the instructions, each peripheral peripheral have a corresponding service, such as this is the heart rate service. A peripheral can have more than one S, Service. There can be multiple attribute characteristic in each service, such as two characteristic, one for measuring the heart rate and one for locating the position.
Then the key point is that each service, each characteristic is determined by the UUID. The UUID is the identifier of each service or characteristic.
You can download the LightBlue app on your iphone. You can see the UUID of some devices here.
In actual use, we are going to get the data through the UUID. This is very important.
In Corebluetooth, the specific data structure diagram is as follows:
4 step-by-step get started BLE development 4.1 Step 1 Create Cbcentralmanager
It is clear from the name that this class is used to manage ble. We are using this class to make connections.
First create a:
@property (nonatomic,strong) CBCentralManager *centralManager;dispatch_queue_t centralQueue = dispatch_queue_create("com.manmanlai", DISPATCH_QUEUE_SERIAL); self.centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:centralQueue];
Then the key is the use of cbcentralmanagerdelegate. I'll talk about this later.
4.2 Step 2 Looking for cbperipheral peripherals
With Cbcentralmanager, the next step is to look for cbperipheral peripherals, which are simple:
[selfscanForPeripheralsWithServices:options:nil];
The service here is the corresponding UUID, if it is empty, this scan all service.
4.3 Step 3 Connection cbperipheral
In the previous step, if a device was found, Cbcentralmanager's delegate would call the following method:
- (void) Centralmanager: (Cbcentralmanager *) Central diddiscoverperipheral: (cbperipheral *) Peripheral Advertisementdata: (nsdictionary*) Advertisementdata RSSI: (NSNumber*) rssi{NSLog(@"name:%@", peripheral);if(!peripheral | |!peripheral. Name|| ([Peripheral. Nameisequaltostring:@""])) {return; }if(! Self. Peripheral|| ( Self. Peripheral. State= = cbperipheralstatedisconnected)) { Self. Peripheral= Peripheral; Self. Peripheral. Delegate= Self;NSLog(@"Connect Peripheral"); [ Self. CentralmanagerConnectperipheral:peripheral options:Nil]; }}
Here we create a Cbperipheral object and then connect directly to the
The Cbperipheral object also needs to be set delegate.
4.4 Step 4 Search Service
If the peripheral connection succeeds, the delegate method is called:
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral{ if (!peripheral) { return; } [self.centralManager stopScan]; NSLog(@"peripheral did connect"); [self.peripheral discoverServices:nil];}
Let's stop the scan here and let the peripheral peripherals look for their service.
4.5 Step 5 Search for characteristic
The following method is called when the service is found:
- (void) Peripheral: (cbperipheral *) Peripheral diddiscoverservices: (Nserror*) error{Nsarray*services =Nil;if(Peripheral! = Self. Peripheral) {NSLog(@"Wrong peripheral.\n");return; }if(Error! =Nil) {NSLog(@"Error%@\n", error);return; } services = [Peripheral Services];if(!services | |! [Services Count]) {NSLog(@"No Services");return; } for(Cbservice *service in Services) {NSLog(@"service:%@", service. UUID); [Peripheral Discovercharacteristics:NilForservice:service]; }}
We look for the corresponding characteristic based on the service found.
4.6 Step 6 reads data after characteristic is found
The following delegate method is called when characteristic is found:
- (void) Peripheral: (cbperipheral *) Peripheral Diddiscovercharacteristicsforservice: (Cbservice *) Service error: (Nserror*) error{NSLog(@"characteristics:%@", [service characteristics]);Nsarray*characteristics = [Service characteristics];if(Peripheral! = Self. Peripheral) {NSLog(@"Wrong peripheral.\n");return; }if(Error! =Nil) {NSLog(@"Error%@\n", error);return; } Self. Characteristic= [Characteristics Firstobject];//[self.peripheral readValueForCharacteristic:self.characteristic];[ Self. PeripheralSetnotifyvalue:YESForcharacteristic: Self. Characteristic];
Here we can use readvalueforcharacteristic: to read the data. If the data is constantly updated, you can use setnotifyvalue:forcharacteristic: to achieve as soon as new data is available.
4.7 Step 7 Processing data
The delegate method is called when the data is read:
- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error{ NSData *data = characteristic.value; ...}
4.8 Step 8 Writing data to the device
This is simple, just use:
[self.peripheral writeValue:data forCharacteristic:self.type:CBCharacteristicWriteWithResponse];
Data is the NSData type.
5 experiments
Using the Xiaomi bracelet experiment, the following results are obtained:
-- .-Ten -: the:31.607ketherdemo[13786:1792995] Scaning Device -- .-Ten -: the:33.474ketherdemo[13786:1793032] Name:<cbperipheral:0x1700e4380, identifier =6ff833e3- theC1- -C6-cbc0- AboutA706aaae31, name = Ls_sca16, State= disconnected> -- .-Ten -: the:33.475ketherdemo[13786:1793032]ConnectPeripheral -- .-Ten -: the:37.538ketherdemo[13786:1793031] Peripheral didConnect -- .-Ten -: the:37.984ketherdemo[13786:1793031] Service:fee7 -- .-Ten -: the:37.985ketherdemo[13786:1793031] Service:device Information -- .-Ten -: the:38.099ketherdemo[13786:1793032] Characteristics: ("<cbcharacteristic:0x17409c250, UUID = FEC8, properties = 0x20, value = (null), notifying = no>","<cbcharacteristic:0x17409c200, UUID = FEC7, properties = 0x8, value = (null), notifying = no>") -- .-Ten -: the:38.100ketherdemo[13786:1793032] Kether didConnect -- .-Ten -: the:38.101ketherdemo[13786:1793032] Kether didConnect -- .-Ten -: the:38.280ketherdemo[13786:1793031] Characteristics: ("<cbcharacteristic:0x17009f270, UUID = Manufacturer Name String, properties = 0x2, value = (null), notifying = no& gt; ","<cbcharacteristic:0x17009f2c0, UUID = Model number String, properties = 0x2, value = (null), notifying = No>" /c3>,"<cbcharacteristic:0x17009f310, UUID = Serial number String, properties = 0x2, value = (null), notifying = No>" ,"<cbcharacteristic:0x17009eb90, UUID = Hardware Revision String, properties = 0x2, value = (null), notifying = no& gt; ","<cbcharacteristic:0x17009f0e0, UUID = Firmware Revision String, properties = 0x2, value = (null), notifyi ' = no& gt; ",
6 Summary
With the above method, we can easily develop ble. It's actually easier than you think.
"This article is original article, reprint please indicate source: Blog.csdn.net/songrotek"
iOS developed wearable device Bluetooth 4.0 BLE development