美文网首页
iOS 蓝牙开发(固件升级&空中升级)

iOS 蓝牙开发(固件升级&空中升级)

作者: xieyinghao | 来源:发表于2020-03-06 10:39 被阅读0次
  1. pod
 use_frameworks!
      pod 'iOSDFULibrary'

2.桥接要ota的vc

  比如:#import "MineMainViewController.h"
  1. 在 MineMainViewController 导入
 #import<iOSDFULibrary/iOSDFULibrary-Swift.h>
  1. 使用方法,第一步,现在服务器下载好固件,存入本地
  NSURL *url = [NSURL URLWithString:@"https://**********"];
    AFHTTPSessionManager *mange = [AFHTTPSessionManager manager];
        [[mange downloadTaskWithRequest:[NSURLRequest requestWithURL:url] progress:^(NSProgress * _Nonnull downloadProgress) {
            NSString *progress = [NSString stringWithFormat:@"%.0f%%",1.0 * downloadProgress.completedUnitCount / downloadProgress.totalUnitCount *100];

            dispatch_async(dispatch_get_main_queue(), ^{
                self.hud.labelText = NSStringFormat(@"下载进度...%@",progress);
                self.hud.progress = 1.0 * downloadProgress.completedUnitCount / downloadProgress.totalUnitCount;
            });


        } destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {

            NSURL *downloadURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];
            return [downloadURL URLByAppendingPathComponent:@"zipFile.zip"];

        } completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {
            
            //蓝牙升级
            [self uploadFileToBlueDevice:filePath];

        }] resume];
        
    }];

5.执行蓝牙升级

/**
 执行升级文件发送到固件操作
 */
- (void)uploadFileToBlueDevice:(NSURL *)filePath{

    CBPeripheral *peripheral = [AYBlueHelp shareBlue].peripheral;

    DFUFirmware *selectedFirmware = [[DFUFirmware alloc] initWithUrlToZipFile:filePath];
 
    DFUServiceInitiator *initiator = [[DFUServiceInitiator alloc] initWithCentralManager: self.manage target:peripheral];
    [initiator withFirmware:selectedFirmware];
    initiator.delegate = self; // - to be informed about current state and errors
    initiator.logger = self;
    initiator.progressDelegate = self;
    [initiator start];
}

6.最后一步,遵守协议,实现代理方法,监听升级状态

#pragma mark - LoggerDelegate
- (void)logWith:(enum LogLevel)level message:(NSString *)message{
    DLog(@"logWith---------level = %d,-------message,%@",level,message);
}
#pragma mark - DFUServiceDelegate


#pragma mark - DFUProgressDelegate

- (void)dfuProgressDidChangeFor:(NSInteger)part outOf:(NSInteger)totalParts to:(NSInteger)progress currentSpeedBytesPerSecond:(double)currentSpeedBytesPerSecond avgSpeedBytesPerSecond:(double)avgSpeedBytesPerSecond{
    
    self.hud.labelText = NSStringFormat(@"升级中...%zd %%",progress);
    self.hud.progress = progress * 0.01;
 
}

- (void)dfuStateDidChangeTo:(enum DFUState)state{
    DLog(@"dfuStateDidChangeTo-----------state = %d",state);
    
    if (state == 6) {
        [self.hud hide:YES];
        [MBProgressHUD wj_showSuccess:@"升级成功!"];

        //重新连接
        CBPeripheral *peripheral = [AYBlueHelp shareBlue].peripheral;
        [self.manage cancelPeripheralConnection:peripheral];
        
        self.manage = [[CBCentralManager alloc] initWithDelegate:self queue:dispatch_get_main_queue()];
        [AYBlueHelp shareBlue].manage = self.manage;
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self.manage scanForPeripheralsWithServices:nil options:nil];
        });

    }
}
- (void)dfuError:(enum DFUError)error didOccurWithMessage:(NSString *)message{
//    DLog(@"dfuError-----------error = %d,-------------message = %@",error,message);
    [self.hud hide:YES];
    [MBProgressHUD wj_showError:message];
}

参考:https://www.jianshu.com/p/8a95c9ce3f98

相关文章

  • iOS 蓝牙开发(固件升级&空中升级)

    pod 2.桥接要ota的vc 在 MineMainViewController 导入 使用方法,第一步,现在服务...

  • iOS蓝牙空中升级(固件升级)

    空中升级又叫固件升级,指你手机从服务器下载下来的包或者数据,通过蓝牙传输给你的外设升级固件。如果你能把蓝牙的基础搞...

  • iOS 蓝牙开发(固件升级&空中升级)

    最近几个月都在做蓝牙的项目,趁现在有空,就把在蓝牙开发过程中的心得和踩过的坑给记录下来,分享给大家,避免大家在蓝牙...

  • iOS蓝牙固件升级解决方案

    何为固件升级? 固件为什么需要升级? 大家也许有做过硬件(蓝牙)固件升级的开发,举个栗子:用手机设备中心通过蓝牙硬...

  • 蓝牙设备空中升级(OTA)

    蓝牙固件空中升级(OTA)涉及到蓝牙无线通信、固件外存分布、固件内存分布(定制链接脚本)、固件二次引导等技术,需要...

  • iOS蓝牙空中(固件)升级实现

    最近项目中有用到固件升级功能,即使用手机去升级硬件设备的固件。项目中硬件端使用的单片机程序是一个 xxx.bin ...

  • 【IOS】BLE-蓝牙空中升级(OAD)

    蓝牙为CC2541 ios升级蓝牙固件; 1.头文件 #import

  • ios开发-蓝牙固件升级流程

    最近在开发ios平台的一款app过程中,遇到蓝牙固件升级这样的一个需求场景,从固件的线上版本比对、固件下载管理再到...

  • iOS 蓝牙固件升级

    升级介绍 蓝牙固件升级是使用手机给固件进行版本升级,以达到修复bug或者添加新功能的作用。升级的大概流程是:首先,...

  • 蓝牙固件升级

    升级介绍 蓝牙固件升级是使用手机给固件进行更新,以达到修复bug,完善功能的作用。升级的大概流程是:首先,当手环的...

网友评论

      本文标题:iOS 蓝牙开发(固件升级&空中升级)

      本文链接:https://www.haomeiwen.com/subject/lquvrhtx.html