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

  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

推荐阅读更多精彩内容

  • 最近几个月都在做蓝牙的项目,趁现在有空,就把在蓝牙开发过程中的心得和踩过的坑给记录下来,分享给大家,避免大家在蓝牙...
    经天纬地阅读 4,732评论 6 20
  • 最近在开发ios平台的一款app过程中,遇到蓝牙固件升级这样的一个需求场景,从固件的线上版本比对、固件下载管理再到...
    miG92阅读 1,464评论 0 3
  • 蓝牙固件空中升级(OTA)涉及到蓝牙无线通信、固件外存分布、固件内存分布(定制链接脚本)、固件二次引导等技术,需要...
    单线程Jack阅读 1,638评论 0 1
  • Guide to BluetoothSecurity原文 本出版物可免费从以下网址获得:https://doi.o...
    公子小水阅读 6,591评论 0 6
  • 公司项目需要用到BLE以CBCentralManager的身份和硬件交互,开发过程中解决了一些遇到的问题和一些处理...
    daihz阅读 4,065评论 3 27