iOS-Developer-OCiOS - Developer - OC 进阶大全

iOS 蓝牙连接工具类

2019-08-12  本文已影响2人  zwwuchn

基础知识了解

BLE中的开发要使用CoreBluetooth框架
CoreBluetooth框架的核心其实是两个东西,peripheral和central, 可以理解成外设和中心。对应他们分别有一组相关的API和类

蓝牙设备的几种状态:

作为中心模式流程:

作为外设模式流程:

具体实施如下

 [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

代码地址:代码传送门

上一篇下一篇

猜你喜欢

热点阅读