iOS 蓝牙开发(三)

前期回顾

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方法中给我们反馈写入是否成功的反馈信息。

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 蓝牙开发说简单也简单,说不简单也有点难,开发人员在首次开发蓝牙前首先需要搞清楚蓝牙开发的概念,还要了解掌握蓝牙开发...
    阿道奇阅读 11,994评论 2 9
  • 原文:http://www.myexception.cn/operating-system/2052286.htm...
    KYM1988阅读 6,167评论 2 2
  • 初始化 启动中心管理器 queue设为nil,中心管理器在主线程中派发事件。 跟踪设备蓝牙状态 中心管理器创建成功...
    superKelly阅读 2,948评论 0 0
  • 本文介绍了蓝牙的概念以及具体的使用步骤. 一.蓝牙概念 蓝牙2.0为传统蓝牙,传统蓝牙也称为经典蓝牙.蓝牙4.0因...
    o惜乐o阅读 18,384评论 41 59
  • @[TOC](iOS 蓝牙开发 ) 1.蓝牙简介 蓝牙模式简介蓝牙开发分为两种模式,中心模式(central),和...
    孔雨露阅读 6,976评论 1 2

友情链接更多精彩内容