iOS开发 之 可穿戴设备 蓝牙4.0 BLE 开发

1 前言

当前有越来越多的可穿戴设备使用了蓝牙4.0 BLE(Bluetooth Low Energy)。对于iOS开发而言,Apple之前专门推出CoreBluetooth的Framework来支持BLE的开发。对于硬件开发有了解的朋友应该知道,在之前使用低版本的蓝牙的设备,要连接到iOS设备上,需要注册MFI,拥有MFI协议才能进行相应的开发。如果大家关注我之前对LEGO EV3的研究,就可以发现,EV3是使用了蓝牙2.1,因此需要MFI协议来进行开发。

本文将一步一步讲解如何使用CoreBluetooth框架来与各种可穿戴设备进行通信,使用 小米手环 来进行基本的测试。

2 开发环境

1 Macbook Pro Mac OS X 10.10 2 Xcode 6.3.2 3 iPhone 5s v8.1 4 小米手环

3 基本流程

要开发蓝牙,需要对整个通讯过程有个基本了解。这里我摘录一些Apple官方的文档Core Bluetooth Programming Guide的图片来加以说明。这个文档其实对于开发的流程写的是非常的清楚,大家最好可以看一下。

3.1 可穿戴设备与iOS互联方式

从上面这幅图可以看到,我们的iOS设备是Central,用来接收数据和发送命令,而外设比如小米手环是Peripheral,向外传输数据和接收命令。我们要做的就是通过Central来连接Peripheral,然后实现数据的接收和控制指令的发送。在做到这一步之后,再根据具体的硬件,对接收到的数据进行parse解析。

3.2 可穿戴设备蓝牙的数据结构

这里用的是心率设备来做说明,每个外设Peripheral都有对应的服务Service,比如这里是心率Service。一个外设可以有不止一个s、Service。每个service里面可以有多个属性Characteristic,比如这里有两个Characteristic,一个是用来测量心率,一个是用来定位位置。

那么很关键的一点是每个Service,,每个Characteristic都是用UUID来确定的。UUID就是每个Service或Characteristic的identifier。

大家可以在iPhone上下载LightBlue这个应用。可以在这里查看一些设备的UUID。

在实际使用中,我们都是要通过UUID来获取数据。这点非常重要。 在CoreBluetooth中,其具体的数据结构图如下:

4 Step-By-Step 上手BLE开发4.1 Step 1 创建CBCentralManager

从名字上大家可以很清楚的知道,这个类是用来管理BLE的。我们也就是通过这个类来实现连接。

先创建一个:

@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];

然后关键在于CBCentralManagerDelegate的使用。这个之后再讲。

4.2 Step 2 寻找CBPeripheral外设

有了CBCentralManager,接下来就是寻找CBPeripheral外设,方法很简单:

[self.centralManager scanForPeripheralsWithServices:@[] options:nil];

这里的Service就是对应的UUID,如果为空,这scan所有service。

4.3 Step 3 连接CBPeripheral

在上一步中,如果找到了设备,则CBCentralManager的delegate会调用下面的方法:

– (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI{NSLog(@”name:%@”,peripheral);if (!peripheral || !peripheral.name || ([peripheral.name isEqualToString:@””])) {return;}== CBPeripheralStateDisconnected)) {self.peripheral = peripheral;= self;NSLog(@”connect peripheral”);[self.centralManager connectPeripheral:peripheral options:nil];}}

我们在这里创建了一个CBPeripheral的对象,然后直接连接 CBPeripheral的对象也需要设置delegate.

4.4 Step 4 寻找Service

如果Peripheral连接成功的话,就会调用delegate的方法:

– (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral{if (!peripheral) {return;}[self.centralManager stopScan];NSLog(@”peripheral did connect”);[self.peripheral discoverServices:nil];}

我们这里先停止Scan,然后让Peripheral外设寻找其Service。

4.5 Step 5 寻找Characteristic

找到Service后会调用下面的方法:

– (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:nil forService:service];}}

我们根据找到的service寻找其对应的Characteristic。

4.6 Step 6 找到Characteristic后读取数据为了一些琐事吵架,然后冷战,疯狂思念对方,最后和好。

iOS开发 之 可穿戴设备 蓝牙4.0 BLE 开发

相关文章:

你感兴趣的文章:

标签云: