蓝牙是一个标准的无线通信协议,具备设备成本低、传输距离近和功耗低等特色,被普遍的应用在多种场合。蓝牙通常分为传统蓝牙和BLE
两种模式:传统蓝牙能够传输音频等较大数据量,距离近、功耗相对大;而BLE
则用来传输节点数据,传输数据量十分小,多数状况处于休眠状态,于是功耗十分低,被普遍的应用于智能穿戴设备。git
本文主要介绍iOS
的蓝牙BLE
开发流程,在介绍具体开发流程以前,有必要了解一下蓝牙BLE
的特色。BLE
经过属性(attribute
)在client
和server
之间进行数据交互,GATT
定义了属性协议(Profile
)来进行发现设备、读写数据和获取状态等功能。其中,在iOS
蓝牙BLE
开发过程当中,App
应用属于Central
设备,BLE
产品属于外设Peripheral
。Profile
的结构图以下:github
其中,Service
和 Characteristic
都有一个UUID
来相互区分,相似心跳、血糖等的Service
的UUID
由蓝牙SIG
统一设定,同时也容许自定义服务,但仍须要用不一样的UUID
来标识。测试
针对客户端蓝牙BLE开发,通常不须要深刻了解蓝牙协议栈,若是有兴趣,能够参考以下资料(本资料来自TI
):
TI_BLE_Description大数据
建立一个队列,而后在这个队列里面进行BLE
的各类操做spa
//建立CBCentralManager对象 dispatch_queue_t queue = dispatch_queue_create("bluetooth", DISPATCH_QUEUE_SERIAL); CBCentralManager *mgr = [[CBCentralManager alloc] initWithDelegate:self queue:queue];
参数介绍:代理
//CBCentralManagerScanOptionAllowDuplicatesKey值为 No,表示不重复扫描已发现的设备 NSDictionary *optionDic = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:NO] forKey:CBCentralManagerScanOptionAllowDuplicatesKey]; [_mgr scanForPeripheralsWithServices:nil options:optionDic];
[_mgr stopScan];
遍历扫描到的外设,而后链接外设code
for (CBPeripheral *peripheral in self.peripherals) { [_mgr connectPeripheral:peripheral options:nil]; }
获取服务server
[peripheral discoverServices:nil];
获取特征对象
[peripheral discoverCharacteristics:nil forService:service];
获取描述blog
[peripheral discoverDescriptorsForCharacteristic:characteristic]
改写特征数据
[_peripheral writeValue:data forCharacteristic:characteristic type:CBCharacteristicWriteWithResponse];
代理方法:centralManagerDidUpdateState
Central
已经更新状态,要在CBManagerStatePoweredOn
里扫描外设,由于这是蓝牙初始化工做已完成
- (void)centralManagerDidUpdateState:(CBCentralManager *)central { switch (central.state) { case CBManagerStatePoweredOn: { NSLog(@"开启蓝牙, 开始扫描"); [_mgr scanForPeripheralsWithServices:nil options:nil]; } break; case CBManagerStateUnsupported: NSLog(@"不支持蓝牙"); break; case CBManagerStatePoweredOff: NSLog(@"蓝牙未打开"); break; default: NSLog(@"蓝牙打开失败"); break; } }
代理方法: centralManager:didDiscoverPeripheral:advertisementData:RSSI:
扫描到外设
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary<NSString *,id> *)advertisementData RSSI:(NSNumber *)RSSI
代理方法: centralManager:didConnectPeripheral:
链接到外设
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral
代理方法: peripheral:didDiscoverServices:
发现服务
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error
代理方法: peripheral:didDiscoverCharacteristicsForService:error:
发现服务的特征
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error
代理方法: peripheral:didUpdateValueForCharacteristic:error:
已经更新特征的值
- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error
代理方法: peripheral:didWriteValueForCharacteristic:error:
已经写入特征的值
-(void)peripheral:(CBPeripheral *)peripheral didWriteValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error
下面的视图是基于小米手环2的测试数据,因为不是小米手环的开发者,没办法读取详细的数据,只把硬件、软件的版本信息等数据读出,以供须要开发蓝牙BLE之参考。
参考源码
https://github.com/BirdandLion/iOS-BLE.git
参考文档
http://www.jianshu.com/p/0a6c49922aad