美文网首页
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 蓝牙开发(固件升级&空中升级)

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