iOS蓝牙BLE开发

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

蓝牙BLE简介

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

图1

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

针对客户端蓝牙BLE开发,通常不须要深刻了解蓝牙协议栈,若是有兴趣,能够参考以下资料(本资料来自TI):
TI_BLE_Description大数据

BLE开发流程

1. 建立CBCentralManager

建立一个队列,而后在这个队列里面进行BLE的各类操做spa

//建立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. 链接外设

遍历扫描到的外设,而后链接外设code

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

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

获取服务server

[peripheral discoverServices:nil];

获取特征对象

[peripheral discoverCharacteristics:nil forService:service];

获取描述blog

[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

相关文章
相关标签/搜索