iOS CoreBluetooth

iOS 蓝牙开发(三)

2021-10-13  本文已影响0人  Alexander

前期回顾

iOS 蓝牙开发(一)
iOS 蓝牙开发(二)
iOS 蓝牙开发(四)

前面记录了蓝牙如何进行扫描、链接、以及获取外设的服务和特征,本篇笔记我将记录如何实现与外设做数据交互(explore and interact)

一、实现步骤

构建方法流程:链接成功->获取指定的服务与特征->订阅指定的特征值->通过具有写权限的特征值来写数据->最后在函数didUpdateValueForCharacteristic中获取蓝牙的反馈信息;

//连接成功
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral{
    //连接成功之后寻找服务,传nil表示寻找所有服务
    [peripheral discoverServices:nil];
}
//发现服务的回调
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error{
    if (error) {
        NSLog(@"didDiscoverServices : %@", [error localizedDescription]);
        return;
    }
    
    //提取出所有特征
    self.services = [NSMutableArray arrayWithArray:peripheral.services];
        for (CBService *s in peripheral.services) {
        [s.peripheral discoverCharacteristics:nil forService:s];
    }
}

//获取特征后的回调
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error
{
    if (error) {
        NSLog(@"didDiscoverCharacteristicsForService error : %@", [error localizedDescription]);
        return;
    }
    
  // 筛选自己需要的特征
    [self createCharacticWithPeripheral:peripheral service:service];
}
#pragma mark - private
// 筛选特征
-(void)createCharacticWithPeripheral:(CBPeripheral *)peripheral service:(CBService *)service
{
    if (self.UUIDString) {
        if (![service.UUID  isEqual:[CBUUID UUIDWithString:self.UUIDString]]) {
            return;
        }
    }
    
    for (CBCharacteristic *c in service.characteristics)
    {
        CBCharacteristicProperties properties = c.properties;
        if (properties & CBCharacteristicPropertyWrite) {
            self.characteristic = c;
            self.writeCharacteristic = c;
        }else if (properties & CBCharacteristicPropertyNotify) {
            self.notifyCharacteristic = c;
            [peripheral setNotifyValue:YES forCharacteristic:c];
        }else if (properties & CBCharacteristicPropertyWriteWithoutResponse) {
            self.characteristic = c;
            self.writeCharacteristic = c;
        }else if (properties & CBCharacteristicPropertyIndicate) {
            self.notifyCharacteristic = c;
        }
    }
}

//是否写入成功的回调
- (void)peripheral:(CBPeripheral *)peripheral didWriteValueForCharacteristic:(CBCharacteristic *)characteristic
             error:(NSError *)error
{
    self.resultStr = @"";
    if (!self.writeToCharacteristicBlock) {
        return;
    }
    self.responseCount ++;
    if (self.writeCount != self.responseCount) {
        return;
    }
    self.writeToCharacteristicBlock(characteristic,error);
}

// 数据接收时回调
- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error
{
    if (error) {
        //报错了
        NSLog(@"didUpdateValueForCharacteristic error : %@", error.localizedDescription);
        return;
    }
    if (!characteristic.value) {
        return;
    }
    if (self.resultStr.length <= 0 || ![self.resultStr isEqualToString:[ZJAWGStringInstance baseConversionBinaryDataToHexadecimal:characteristic.value]]) {
        self.resultStr = [ZJAWGStringInstance baseConversionBinaryDataToHexadecimal:characteristic.value];
        if (self.equipmentReturnBlock) {
            self.equipmentReturnBlock(peripheral, characteristic,self.resultStr,error);
        }
    }
}

总结:
本篇笔记大概就是在接收到服务和特征后对数据进行写入的操作的过程,笔记中的重点在于要熟悉构建特征和服务的方法流程。熟悉流程,我们就能清楚知道当在写入数据时,系统蓝牙会在函数didUpdateValueForCharacteristic方法中给我们反馈写入是否成功的反馈信息。

上一篇下一篇

猜你喜欢

热点阅读