1、Swift
import Flutter
import UIKit
public class RtkOtaPlugin: NSObject,FlutterPlugin,BleToolDelegate{
public func bleToolProgress(_ progress: String!) {
RtkOtaPlugin.channel?.invokeMethod("sendMessage", arguments: progress)
}
//固件升级文件
var filePath = ""
//UI升级文件
var colorPath = ""
//设备地址
var address = ""
//设备地址
var deviceName = ""
static var channel:FlutterMethodChannel?;
public static func register(with registrar: FlutterPluginRegistrar) {
channel = FlutterMethodChannel(name: "rtk_ota_plugin", binaryMessenger: registrar.messenger())
let instance = RtkOtaPlugin()
registrar.addMethodCallDelegate(instance, channel: channel!)
}
override init () {
}
public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
if(call.method == "getPlatformVersion"){
result("iOS " + UIDevice.current.systemVersion)
}else if(call.method == "initIosDfuAdapter"){
let arg = call.arguments as! [String:Any]
self.deviceName = arg["name"] as! String
let path = arg["path"] as! String
self.setFilePath(path: path)
self.initDfuAdapter()
result("")//
}else if(call.method == "setAddress"){
let arg = call.arguments as! [String:Any]
self.address = arg["address"] as! String
// result("")
}else if(call.method == "setFilePath"){
let arg = call.arguments as! [String:Any]
var path = arg["path"] as! String
self.setFilePath(path: path)
// result("")
}else if(call.method == "getColorPath"){
if(self.colorPath.isEmpty){
result("")
}else{
result(colorPath+"")
}
}else if(call.method == "getFirmwarePath"){
if(self.filePath.isEmpty){
result("")
}else{
result(filePath+"")
}
}
result("iOS " + UIDevice.current.systemVersion)
}
private func initDfuAdapter(){
print("mac: \(deviceName)")
BleTool.sharedManager().setDeviceName(deviceName)
BleTool.sharedManager().initData();
}
private func setFilePath(path:String){
let unzipFiles = UnzipFirmware.init()
var url = URL.init(string: path)
var firmwareFilesURL:[URL] = unzipFiles.unzipFirmwareFiles(url) as! [URL]
for url in firmwareFilesURL{
print("RtkOtaPlugin-fileName:\(url.path)")
if url.path.range(of: "app_MP_sdk") != nil {
self.filePath = url.path
BleTool.sharedManager().setFilePath(self.filePath)
}
if url.path.range(of: "color") != nil {
self.colorPath = url.path
// BleTool.sharedManager().setf(self.colorPath)
}
}
}
}
2、Objc
#import "RtkOtaPlugin.h"
#import "BleTool.h"
#import "UnzipFirmware.h"
#import <CommonCrypto/CommonDigest.h>
@implementation RtkOtaPlugin
static FlutterMethodChannel* channel;
NSString *filePath;
NSString *colorPath;
NSString *deviceName;
+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {
channel = [FlutterMethodChannel
methodChannelWithName:@"rtk_ota_plugin"
binaryMessenger:[registrar messenger]];
RtkOtaPlugin* instance = [[RtkOtaPlugin alloc] init];
[registrar addMethodCallDelegate:instance channel:channel];
}
- (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
if ([@"getPlatformVersion" isEqualToString:call.method]) {
result([@"iOS " stringByAppendingString:[[UIDevice currentDevice] systemVersion]]);
} else if([@"initIosDfuAdapter" isEqualToString:call.method]){
NSString *path = @"";
path = [call.arguments objectForKey:@"path"];
[self setFilePath:path];
deviceName = [call.arguments objectForKey:@"name"];
[self initDfuAdapter];
result(@"");
}else if([@"setFilePath" isEqualToString:call.method]){
NSString *path = @"";
path = [call.arguments objectForKey:@"path"];
[self setFilePath:path];
result(@"");
}else if([@"md5" isEqualToString:call.method]){
NSString *path = @"";
path = [call.arguments objectForKey:@"path"];
NSString *md5 = [self getFileMD5StrFromPath:path];
if([md5 isEqualToString:@"file is null"]){
result(@"1,file is null");
}else{
result([NSString stringWithFormat:@"2,%@",md5]);
}
}else if([@"getColorPath" isEqualToString:call.method]){
if(colorPath){
result(colorPath);
}else{
result(@"");
}
}else {
result(FlutterMethodNotImplemented);
}
}
- (NSString *)getFileMD5StrFromPath:(NSString *)path
{
NSFileManager *fileManager = [NSFileManager defaultManager];
if([fileManager fileExistsAtPath:path isDirectory:nil])
{
NSData *data = [NSData dataWithContentsOfFile:path];
unsigned char digest[16];
CC_MD5( data.bytes, (CC_LONG)data.length-16, digest );
NSMutableString *output = [NSMutableString stringWithCapacity:16 * 2];
for( int i = 0; i < 16; i++ )
{
[output appendFormat:@"%02x", digest[i]];
}
return output;
}
else
return @"file is null";
}
- (void)BleToolProgress:(NSString *)progress {
if(channel){
[channel invokeMethod:@"sendMessage" arguments:progress];
}
}
-(void)initDfuAdapter{
[BleTool sharedManager].delegate = self;
[[BleTool sharedManager] setDeviceName:deviceName];
[[BleTool sharedManager] initData];
}
- (void)setFilePath:(NSString *)path {
UnzipFirmware *unzipFiles = [[UnzipFirmware alloc] init];
NSURL *url = [[NSURL alloc] initWithString:path];
NSArray *firmwareFilesURL = [unzipFiles unzipFirmwareFiles:url];
for (NSURL *url in firmwareFilesURL) {
NSString *strPath = url.path;
NSLog(@"strPaht:%@",strPath);
if([[strPath lowercaseString] containsString:@"app_mp_sdk"]){
filePath = strPath;
[[BleTool sharedManager] setFilePath:strPath];
}
if([[strPath lowercaseString] containsString:@"color565"]){
colorPath = strPath;
}
}
}
@end
网友评论