iOS 蓝牙链接工具类

基础知识了解git

  • peripheral 外设,被链接的设备为perilheral
  • central 发起链接的时central
  • service and characteristic 服务和特征 每一个设备都会提供服务和特征,相似于服务端的api,可是因为结构不一样,每一个外设会有不少的服务,每一个服务中又包含不少字段,这些字段的权限通常分为 读read,写write,通知notiy几种,就是咱们链接设备后具体须要操做的内容。
  • Description 每一个characteristic能够对应一个或多个Description用户描述characteristic的信息或属性
  • Characteristic 一个characteristic包括一个单一变量和0-n个用来描述characteristic变量的descriptor,characteristic能够被认为是一个类型,类 似于类。
  • Descriptor Descriptor用来描述characteristic变量的属性。例如,一个descriptor能够规定一个可读的描述,或者一个characteristic变量可接受的 范围,或者一个characteristic变量特定的测量单位。
  • Service service是characteristic的集合

BLE中的开发要使用CoreBluetooth框架 CoreBluetooth框架的核心实际上是两个东西,peripheral和central, 能够理解成外设和中心。对应他们分别有一组相关的API和类蓝牙设备的几种状态:github

  • 准备(standby)
  • 广播(advertising)
  • 监听扫描(Scanning
  • 发起链接(Initiating)
  • 已链接(Connected)

做为中心模式流程:api

  • 创建中心角色bash

  • 扫描外设(discover)框架

  • 链接外设(connect)工具

  • 扫描外设中的服务和特征(discover)ui

    • 获取外设的services
    • 获取外设的Characteristics,获取Characteristics的值,获取Characteristics的Descriptor和Descriptor的值
  • 与外设作数据交互(explore and interact)spa

  • 订阅Characteristic的通知代理

  • 断开链接(disconnect)code

做为外设模式流程:

  • 启动一个Peripheral管理对象
  • 本地Peripheral设置服务,特性,描述,权限等等
  • Peripheral发送广告
  • 设置处理订阅、取消订阅、读characteristic、写characteristic的委托方法

具体实施以下

  • 首先导入工具类, 设置代理
[BLEManager.shareInstance setDelegate:self];
复制代码
  • 搜索设备
#pragma mark- 开始搜索
- (void)beginScan{
    [self.cbCM scanForPeripheralsWithServices:nil options:nil];
}
复制代码
  • 链接设备
#pragma mark- 链接设备
- (void)connect:(CBPeripheral *)peripheral{
    [self.cbCM stopScan];
    [self.cbCM connectPeripheral:peripheral options:nil];
}
复制代码
  • 判断是否链接成功
#pragma mark- 链接成功
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral{
    connectPeripheral = peripheral;
    peripheral.delegate = self;
    [peripheral discoverServices:nil];
    if ([self.delegate respondsToSelector:@selector(didConnectPeripheral:)]) {
        [self.delegate didConnectPeripheral:peripheral];
    }
}
#pragma mark- 链接失败
- (void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error{
    if ([self.delegate respondsToSelector:@selector(didFailToConnectPeripheral:error:)]) {
        [self.delegate didFailToConnectPeripheral:peripheral error:error];
    }
}
复制代码
  • 设置服务和特征
//发现服务
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error{
    for (CBService *s in peripheral.services) {
        if ([s.UUID.UUIDString isEqualToString:self->serviceUUIDString]) {
            //这里能够经过service的UUID属性来辨识你要的服务
            [peripheral discoverCharacteristics:nil forService:s];
        }
    }
}
//设置特征属性值
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error{
    for (CBCharacteristic *characteristic in service.characteristics) {
        if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:self->notify]]){
            // 订阅, 实时接收
            [peripheral setNotifyValue:YES forCharacteristic:characteristic];
        }
    }
    for (CBCharacteristic *characteristic in service.characteristics) {
        if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:self->write]]){
            self.writeCharacteristic = characteristic;
        }
    }
}
复制代码
  • 发送数据
#pragma mark- 发送数据
- (void)write:(NSData *)data withResponse:(BOOL)withResponse{
    if(!_writeCharacteristic){
        NSLog(@"writeCharacteristic is nil!");
        return;
    }
    [connectPeripheral writeValue:data forCharacteristic:self.writeCharacteristic type:withResponse ? CBCharacteristicWriteWithResponse : CBCharacteristicWriteWithoutResponse];
}
复制代码
  • 接收回调数据
#pragma mark- 蓝牙数据接收
- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error{
    if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:self->serviceUUIDString]]) {
        NSData *data = characteristic.value;
        if (data) {
            if ([self.delegate respondsToSelector:@selector(bleGattServiceDataReceived:)]) {
                [self.delegate bleGattServiceDataReceived:data];
            }
        }
    }
}

复制代码
  • 实时监测蓝牙状态
#pragma mark- 蓝牙状态的改变
- (void)centralManagerDidUpdateState:(nonnull CBCentralManager *)central {
    switch (central.state) {
        case CBManagerStateUnknown:
            //未知状态
            break;
        case CBManagerStateResetting:
            //蓝牙重置中
            break;
        case CBManagerStateUnsupported:
            //蓝牙不支持
            break;
        case CBManagerStateUnauthorized:
            //没有权限
            break;
        case CBManagerStatePoweredOff:
            //蓝牙为开启
            break;
        case CBManagerStatePoweredOn:
            //蓝牙已开启
            break;
        default:
            break;
    }
}
复制代码

蓝牙链接操做基本流程即为上文所述, 如需详细代码解析, 请查看demo 代码地址:代码传送门

相关文章
相关标签/搜索