From bc21a4c559c7cb592732aa65ef54503c3ef969e5 Mon Sep 17 00:00:00 2001 From: tanlongsheng <252620078@qq.com> Date: Sat, 14 Jun 2025 15:59:03 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96ota=E5=8D=87=E7=BA=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/data/services/ble_manager.dart | 18 +++++++++ .../controllers/ota_upgrade_controller.dart | 9 ++--- .../Sources/ble_service/BleService.swift | 7 ++++ .../ble_service/SwiftBleServicePlugin.swift | 5 +++ .../ota/ios/Classes/BleManager/JLBleManager.h | 7 +--- .../ota/ios/Classes/BleManager/JLBleManager.m | 37 ++++++++++++++----- local_plugins/ota/ios/Classes/OtaPlugin.swift | 6 +-- 7 files changed, 65 insertions(+), 24 deletions(-) diff --git a/lib/data/services/ble_manager.dart b/lib/data/services/ble_manager.dart index 4fd99d91e..f0bd05289 100644 --- a/lib/data/services/ble_manager.dart +++ b/lib/data/services/ble_manager.dart @@ -701,6 +701,24 @@ class BleManager extends GetxService { } } + /// 设置是否是ota模式 + Future setEnterOta(bool enterOta) async { + try { + await _bleService.setEnterOta(enterOta); + } catch (e) { + Logger.error('设置是否是ota模式失败: ${e.toString()}'); + } + } + + /// 断开连接 + Future disconnect() async { + try { + await _bleService.disconnect(); + } catch (e) { + Logger.error('断开连接失败: ${e.toString()}'); + } + } + /// 验证设备是否可以配对 Future verifyDeviceCanPair(String macAddress) async { try { diff --git a/lib/modules/ota_upgrade/controllers/ota_upgrade_controller.dart b/lib/modules/ota_upgrade/controllers/ota_upgrade_controller.dart index 32e94afa3..c175e332d 100644 --- a/lib/modules/ota_upgrade/controllers/ota_upgrade_controller.dart +++ b/lib/modules/ota_upgrade/controllers/ota_upgrade_controller.dart @@ -1,4 +1,3 @@ -import 'package:ble_service/ble_service.dart'; import 'package:classic_bluetooth/classic_bluetooth.dart'; import 'package:flutter/material.dart'; import 'package:get/get.dart'; @@ -54,7 +53,6 @@ class OtaUpgradeController extends GetxController { // 蓝牙管理服务,用于获取设备信息和通信 final _bleManager = Get.find(); - final BleService _bleService = BleService(); final maxRetryAttempts = 3; // 最大重试次数 int currentRetryCount = 0; @@ -73,7 +71,7 @@ class OtaUpgradeController extends GetxController { @override void onClose() { jlota.onOtaClose(); - _bleService.setEnterOta(false); + _bleManager.setEnterOta(false); isUpgrade.value = false; super.onClose(); } @@ -177,8 +175,7 @@ class OtaUpgradeController extends GetxController { _address = connectedDevices.first.address; } - await _bleService.setEnterOta(true); - await _bleService.disconnect(); + await _bleManager.setEnterOta(true); await Future.delayed(const Duration(seconds: 2)); @@ -422,7 +419,7 @@ class OtaUpgradeController extends GetxController { if (event.event == 'onError') { type.value = OtaEvent.UPGRADE_FAILED; isUpgrade.value = false; - Get.snackbar('升级失败', '请检查设备连接或升级包'); + Get.snackbar('升级失败', '请检查设备连接或升级包,错误码:${event.code}'); } if (event.event == 'NOCONNECTION') { type.value = OtaEvent.CONNECTION_DISCONNECTION; diff --git a/local_plugins/ble_service/ios/ble_service/Sources/ble_service/BleService.swift b/local_plugins/ble_service/ios/ble_service/Sources/ble_service/BleService.swift index 7b1ba6dfe..93673ada8 100644 --- a/local_plugins/ble_service/ios/ble_service/Sources/ble_service/BleService.swift +++ b/local_plugins/ble_service/ios/ble_service/Sources/ble_service/BleService.swift @@ -213,6 +213,13 @@ public class BleService: NSObject { public func connect(to peripheral: CBPeripheral) { connectPeripheral(peripheral) } + + /// 自动重新连接 + public func automaticReconnection() { + if let peripheral = peripheralDevice { + connectPeripheral(peripheral) + } + } /// 统一的连接方法,支持恢复和重连 private func connectPeripheral(_ peripheral: CBPeripheral) { diff --git a/local_plugins/ble_service/ios/ble_service/Sources/ble_service/SwiftBleServicePlugin.swift b/local_plugins/ble_service/ios/ble_service/Sources/ble_service/SwiftBleServicePlugin.swift index cff8e6565..99e8340cc 100644 --- a/local_plugins/ble_service/ios/ble_service/Sources/ble_service/SwiftBleServicePlugin.swift +++ b/local_plugins/ble_service/ios/ble_service/Sources/ble_service/SwiftBleServicePlugin.swift @@ -154,6 +154,11 @@ public class SwiftBleServicePlugin: NSObject, FlutterPlugin { case "setEnterOta": let arguments = call.arguments as? [String: Any] BleService.shared.isEnterOta = (arguments?["enterOta"] as? Bool) ?? false + if BleService.shared.isEnterOta { + BleService.shared.disconnect() + } else { + BleService.shared.automaticReconnection() + } result("") case "disconnect": diff --git a/local_plugins/ota/ios/Classes/BleManager/JLBleManager.h b/local_plugins/ota/ios/Classes/BleManager/JLBleManager.h index b72e4247f..381be12ca 100644 --- a/local_plugins/ota/ios/Classes/BleManager/JLBleManager.h +++ b/local_plugins/ota/ios/Classes/BleManager/JLBleManager.h @@ -66,11 +66,6 @@ extern NSString *kFLT_BLE_DISCONNECTED; //断开连接 @property (nonatomic, copy, nullable) FlutterEventSink flutterEventSink; -/** - * 单例 - */ -+ (instancetype)sharedInstance; - /** 开始搜索 */ @@ -120,6 +115,8 @@ typedef void(^CANCEL_CALLBACK)(uint8_t status); - (void)otaFuncCancel:(CANCEL_CALLBACK _Nonnull)result; +- (void)releaseAllResources; + @end NS_ASSUME_NONNULL_END diff --git a/local_plugins/ota/ios/Classes/BleManager/JLBleManager.m b/local_plugins/ota/ios/Classes/BleManager/JLBleManager.m index a7087124e..1147c552d 100644 --- a/local_plugins/ota/ios/Classes/BleManager/JLBleManager.m +++ b/local_plugins/ota/ios/Classes/BleManager/JLBleManager.m @@ -45,15 +45,6 @@ NSString *FLT_BLE_RCSP_R = @"AE02"; //命令“读”通道 @implementation JLBleManager -+ (instancetype)sharedInstance { - static JLBleManager *singleton = nil; - static dispatch_once_t onceToken; - dispatch_once(&onceToken, ^{ - singleton = [[self alloc] init]; - }); - return singleton; -} - - (instancetype)init { self = [super init]; if (self) { @@ -475,6 +466,8 @@ NSString *FLT_BLE_RCSP_R = @"AE02"; //命令“读”通道 } } else if (result == JL_OTAResultReboot) { + } else if (result == JL_OTAResultDisconnect) { + } else { if (self.flutterEventSink) { self.flutterEventSink(@{ @@ -572,6 +565,32 @@ NSString *FLT_BLE_RCSP_R = @"AE02"; //命令“读”通道 type:CBCharacteristicWriteWithoutResponse]; } +- (void)releaseAllResources { + // 停止扫描 + [self stopScanBLE]; + // 断开蓝牙连接 + [self disconnectBLE]; + // 释放 delegate + self.bleManager.delegate = nil; + self.pairHash.delegate = nil; + self.otaManager.delegate = nil; + // 清空 eventSink + self.flutterEventSink = nil; + // 清空回调 + self.getCallback = nil; + // 清空当前设备和属性 + self.bleCurrentPeripheral = nil; + self.mService = nil; + self.mRcspWrite = nil; + self.mRcspRead = nil; + self.currentEntity = nil; + self.blePeripheralArr = nil; + self.selectedOtaFilePath = nil; + self.connectByUUID = nil; + self.lastAddress = nil; + self.lastFilePath = nil; +} + @end diff --git a/local_plugins/ota/ios/Classes/OtaPlugin.swift b/local_plugins/ota/ios/Classes/OtaPlugin.swift index 1b6514e57..d9b9f31fd 100644 --- a/local_plugins/ota/ios/Classes/OtaPlugin.swift +++ b/local_plugins/ota/ios/Classes/OtaPlugin.swift @@ -17,7 +17,7 @@ public class OtaPlugin: NSObject, FlutterPlugin, FlutterStreamHandler { switch call.method { case "init": - userManager = JLBleManager.sharedInstance() + userManager = JLBleManager.init() result("") case "startUpgrading": let address = arguments?["address"] as? String @@ -54,9 +54,7 @@ public class OtaPlugin: NSObject, FlutterPlugin, FlutterStreamHandler { } private func onOtaClose() { - userManager?.stopScanBLE() - userManager?.otaFuncCancel{code in} - userManager?.disconnectBLE() + userManager?.releaseAllResources() userManager = nil } }