- pod
use_frameworks!
pod 'iOSDFULibrary'
2.桥接要ota的vc
比如:#import "MineMainViewController.h"
- 在 MineMainViewController 导入
#import<iOSDFULibrary/iOSDFULibrary-Swift.h>
- 使用方法,第一步,现在服务器下载好固件,存入本地
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];
}
网友评论