iOS 蓝牙连接工具类

645 阅读3分钟

基础知识了解

  • 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和类蓝牙设备的几种状态:

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

作为中心模式流程:

  • 建立中心角色

  • 扫描外设(discover)

  • 连接外设(connect)

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

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

  • 订阅Characteristic的通知

  • 断开连接(disconnect)

作为外设模式流程:

  • 启动一个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 代码地址:代码传送门