From e310d79c172e116a0be404dcf4f96c4de616d79f Mon Sep 17 00:00:00 2001 From: tanlongsheng <252620078@qq.com> Date: Thu, 12 Jun 2025 11:40:09 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9ota?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controllers/ota_upgrade_controller.dart | 2 +- .../ota/ios/Classes/BleManager/JLBleManager.h | 2 +- .../ota/ios/Classes/BleManager/JLBleManager.m | 22 +++++++++++-------- local_plugins/ota/ios/Classes/OtaPlugin.swift | 12 ++++++---- local_plugins/ota/lib/ota.dart | 7 ++++-- local_plugins/ota/lib/ota_method_channel.dart | 10 ++++----- .../ota/lib/ota_platform_interface.dart | 6 ++--- 7 files changed, 36 insertions(+), 25 deletions(-) diff --git a/lib/modules/ota_upgrade/controllers/ota_upgrade_controller.dart b/lib/modules/ota_upgrade/controllers/ota_upgrade_controller.dart index dbbc4fa1a..8787ef5b4 100644 --- a/lib/modules/ota_upgrade/controllers/ota_upgrade_controller.dart +++ b/lib/modules/ota_upgrade/controllers/ota_upgrade_controller.dart @@ -367,7 +367,7 @@ class OtaUpgradeController extends GetxController { // 开始升级 void startUpgrading(String path) async { - jlota.startUpgrading(path); + jlota.startUpgrading(address: 'E9:09:05:E0:18:7A', filePath: path); return; // isUpgrade.value = false; } diff --git a/local_plugins/ota/ios/Classes/BleManager/JLBleManager.h b/local_plugins/ota/ios/Classes/BleManager/JLBleManager.h index d0dadb523..f768de83a 100644 --- a/local_plugins/ota/ios/Classes/BleManager/JLBleManager.h +++ b/local_plugins/ota/ios/Classes/BleManager/JLBleManager.h @@ -71,7 +71,7 @@ extern NSString *kFLT_BLE_DISCONNECTED; //断开连接 /** 开始搜索 */ --(void)startScanBLE; +-(void)startScanBLE:(NSString * _Nullable)address filePath:(NSString * _Nullable)filePath; /** 停止搜索 diff --git a/local_plugins/ota/ios/Classes/BleManager/JLBleManager.m b/local_plugins/ota/ios/Classes/BleManager/JLBleManager.m index 7ad22268e..1fd281682 100644 --- a/local_plugins/ota/ios/Classes/BleManager/JLBleManager.m +++ b/local_plugins/ota/ios/Classes/BleManager/JLBleManager.m @@ -37,6 +37,9 @@ NSString *FLT_BLE_RCSP_R = @"AE02"; //命令“读”通道 @property (strong, nonatomic) GET_DEVICE_CALLBACK getCallback; +@property (strong, nonatomic) NSString *lastAddress; +@property (strong, nonatomic) NSString *lastFilePath; + @end @@ -83,8 +86,10 @@ NSString *FLT_BLE_RCSP_R = @"AE02"; //命令“读”通道 #pragma mark - 扫描设备相关 #pragma mark 开始扫描 -- (void)startScanBLE { +- (void)startScanBLE:(NSString * _Nullable)address filePath:(NSString * _Nullable)filePath { kJLLog(JLLOG_DEBUG, @"BLE ---> startScanBLE."); + self.lastAddress = address; + self.lastFilePath = filePath; _blePeripheralArr = [NSMutableArray new]; if (_bleManager) { if (_bleManager.state == CBManagerStatePoweredOn) { @@ -135,7 +140,7 @@ NSString *FLT_BLE_RCSP_R = @"AE02"; //命令“读”通道 - (void)connectPeripheralWithUUID:(NSString*)uuid { _connectByUUID = uuid; - [self startScanBLE]; + [self startScanBLE:self.lastAddress filePath:self.lastFilePath]; } -(void)findHid:(NSString *)uuid{ @@ -198,12 +203,11 @@ NSString *FLT_BLE_RCSP_R = @"AE02"; //命令“读”通道 NSString *ble_name = advertisementData[@"kCBAdvDataLocalName"]; NSData *ble_AD = advertisementData[@"kCBAdvDataManufacturerData"]; - NSDictionary *info = [JLAdvParse bluetoothAdvParse:self.pairKey AdvData:advertisementData]; if (ble_name.length == 0) return; kJLLog(JLLOG_DEBUG, @"发现 ----> NAME:%@ RSSI:%@ AD:%@", ble_name,RSSI,ble_AD); - [self addPeripheral:peripheral RSSI:RSSI Name:ble_name Info:info]; + [self addPeripheral:peripheral RSSI:RSSI Name:ble_name Info:ble_AD]; [[NSNotificationCenter defaultCenter] postNotificationName:kFLT_BLE_FOUND object:_blePeripheralArr userInfo:nil]; @@ -213,11 +217,11 @@ NSString *FLT_BLE_RCSP_R = @"AE02"; //命令“读”通道 } } -- (void)addPeripheral:(CBPeripheral*)peripheral RSSI:(NSNumber *)rssi Name:(NSString*)name Info:(NSDictionary*)info{ - if ([name isEqualToString:@"DeapSound"]) { - [self stopScanBLE]; - [self connectBLE:peripheral]; - } +- (void)addPeripheral:(CBPeripheral*)peripheral RSSI:(NSNumber *)rssi Name:(NSString*)name Info:(NSData*)blead{ +// if ([name isEqualToString:@"DeapSound"]) { +// [self stopScanBLE]; +// [self connectBLE:peripheral]; +// } } #pragma mark 设备连接回调 diff --git a/local_plugins/ota/ios/Classes/OtaPlugin.swift b/local_plugins/ota/ios/Classes/OtaPlugin.swift index 26a83de96..7286ad364 100644 --- a/local_plugins/ota/ios/Classes/OtaPlugin.swift +++ b/local_plugins/ota/ios/Classes/OtaPlugin.swift @@ -10,9 +10,13 @@ public class OtaPlugin: NSObject, FlutterPlugin { } public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) { + let arguments = call.arguments as? [String: Any] + switch call.method { case "startUpgrading": - startUpgrading() + let address = arguments?["address"] as? String + let filePath = arguments?["filePath"] as? String + startUpgrading(address: address, filePath: filePath) result("") case "onOtaClose": onOtaClose() @@ -22,10 +26,10 @@ public class OtaPlugin: NSObject, FlutterPlugin { } } - private func startUpgrading() { + private func startUpgrading(address: String?, filePath: String?) { if userManager.mBleManagerState == CBManagerState.poweredOn { - userManager.startScanBLE() - DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) { + userManager.startScanBLE(address, filePath: filePath) + DispatchQueue.main.asyncAfter(deadline: .now() + 10.0) { self.userManager.stopScanBLE() } } diff --git a/local_plugins/ota/lib/ota.dart b/local_plugins/ota/lib/ota.dart index a866431dd..2919d9107 100644 --- a/local_plugins/ota/lib/ota.dart +++ b/local_plugins/ota/lib/ota.dart @@ -132,8 +132,11 @@ class Ota { // _isEventListenerSet = true; // } - Future startUpgrading(String? filePath) { - return OtaPlatform.instance.startUpgrading(filePath); + Future startUpgrading({String? address, String? filePath}) { + return OtaPlatform.instance.startUpgrading( + address: address, + filePath: filePath, + ); } Future onOtaClose() { diff --git a/local_plugins/ota/lib/ota_method_channel.dart b/local_plugins/ota/lib/ota_method_channel.dart index c76816b38..cc3890635 100644 --- a/local_plugins/ota/lib/ota_method_channel.dart +++ b/local_plugins/ota/lib/ota_method_channel.dart @@ -10,11 +10,11 @@ class MethodChannelOta extends OtaPlatform { final methodChannel = const MethodChannel('ota'); @override - Future startUpgrading(String? filePath) async { - await methodChannel.invokeMethod( - 'startUpgrading', - {'filePath': filePath}, // 传递参数 - ); + Future startUpgrading({String? address, String? filePath}) async { + await methodChannel.invokeMethod('startUpgrading', { + 'address': address, + 'filePath': filePath, + }); } @override diff --git a/local_plugins/ota/lib/ota_platform_interface.dart b/local_plugins/ota/lib/ota_platform_interface.dart index cfcc1b285..026414e86 100644 --- a/local_plugins/ota/lib/ota_platform_interface.dart +++ b/local_plugins/ota/lib/ota_platform_interface.dart @@ -23,11 +23,11 @@ abstract class OtaPlatform extends PlatformInterface { _instance = instance; } - Future startUpgrading(String? filePath) { - throw UnimplementedError('platformVersion() has not been implemented.'); + Future startUpgrading({String? address, String? filePath}) { + throw UnimplementedError('startUpgrading() has not been implemented.'); } Future onOtaClose() { - throw UnimplementedError('platformVersion() has not been implemented.'); + throw UnimplementedError('onOtaClose() has not been implemented.'); } }