iOS蓝牙BLE开发

蓝牙是一个标准的无线通讯协议,具有设备成本低、传输距离近和功耗低等特点,被广泛的应用在多种场合。蓝牙一般分为传统蓝牙和BLE两种模式:传统蓝牙可以传输音频等较大数据量,距离近、功耗相对大;而BLE则用来传输节点数据,传输数据量十分小,多数情况处于休眠状态,因而功耗十分低,被广泛的应用于智能穿戴设备。

蓝牙BLE简介

本文主要介绍iOS的蓝牙BLE开发流程,在介绍具体开发流程之前,有必要了解一下蓝牙BLE的特点。BLE通过属性(attribute)在clientserver之间进行数据交互,GATT定义了属性协议(Profile)来进行发现设备、读写数据和获取状态等功能。其中,在iOS蓝牙BLE开发过程中,App应用属于Central设备,BLE产品属于外设PeripheralProfile的结构图如下:

图1

其中,ServiceCharacteristic 都有一个UUID来相互区分,类似心跳、血糖等的ServiceUUID由蓝牙SIG统一设定,同时也允许自定义服务,但仍需要用不同的UUID来标识。

针对客户端蓝牙BLE开发,一般不需要深入了解蓝牙协议栈,如果有兴趣,可以参考如下资料(本资料来自TI):
TI_BLE_Description

BLE开发流程

1. 创建CBCentralManager

创建一个队列,然后在这个队列里面进行BLE的各种操作

	//创建CBCentralManager对象
	dispatch_queue_t queue = dispatch_queue_create("bluetooth", DISPATCH_QUEUE_SERIAL);
    CBCentralManager *mgr = [[CBCentralManager alloc] initWithDelegate:self queue:queue];

2. 扫描外设

参数介绍:

  • serviceUUIDs: 指定扫描包含特点服务的外设,传nil表明是所有服务
  • options: 扫描时的设置,是一个字典
	//CBCentralManagerScanOptionAllowDuplicatesKey值为 No,表示不重复扫描已发现的设备
    NSDictionary *optionDic = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:NO] forKey:CBCentralManagerScanOptionAllowDuplicatesKey];
	[_mgr scanForPeripheralsWithServices:nil options:optionDic];

3. 停止扫描

	[_mgr stopScan];

4. 连接外设

遍历扫描到的外设,然后连接外设

	for (CBPeripheral *peripheral in self.peripherals) {
        [_mgr connectPeripheral:peripheral options:nil];
    }

5. 扫描外设中的服务和特征

获取服务

	[peripheral discoverServices:nil];

获取特征

	[peripheral discoverCharacteristics:nil forService:service];

获取描述

	[peripheral discoverDescriptorsForCharacteristic:characteristic]

改写特征数据

	[_peripheral writeValue:data forCharacteristic:characteristic type:CBCharacteristicWriteWithResponse];

6. 部分CBCentralManagerDelegate方法简介

代理方法: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

7. 部分CBPeripheralDelegate方法简介

代理方法: 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

8. 实例代码

下面的视图是基于小米手环2的测试数据,由于不是小米手环的开发者,没办法读取详细的数据,只把硬件、软件的版本信息等数据读出,以供需要开发蓝牙BLE之参考。

图3
图2


参考源码

https://github.com/BirdandLion/iOS-BLE.git

参考文档

http://www.jianshu.com/p/0a6c49922aad

http://www.jianshu.com/p/4df85eba6dab

http://www.jianshu.com/p/7ba443878e7d

posted @ 2017-07-19 17:32  飞鱼湾  阅读(3509)  评论(0编辑  收藏  举报