Browse Source

优化ota升级

weicu
tanlongsheng 1 year ago
parent
commit
9ffb522dd4
  1. 18
      lib/data/services/ble_manager.dart
  2. 9
      lib/modules/ota_upgrade/controllers/ota_upgrade_controller.dart
  3. 7
      local_plugins/ble_service/ios/ble_service/Sources/ble_service/BleService.swift
  4. 5
      local_plugins/ble_service/ios/ble_service/Sources/ble_service/SwiftBleServicePlugin.swift
  5. 7
      local_plugins/ota/ios/Classes/BleManager/JLBleManager.h
  6. 37
      local_plugins/ota/ios/Classes/BleManager/JLBleManager.m
  7. 6
      local_plugins/ota/ios/Classes/OtaPlugin.swift

18
lib/data/services/ble_manager.dart

@ -701,6 +701,24 @@ class BleManager extends GetxService {
} }
} }
/// 设置是否是ota模式
Future<void> setEnterOta(bool enterOta) async {
try {
await _bleService.setEnterOta(enterOta);
} catch (e) {
Logger.error('设置是否是ota模式失败: ${e.toString()}');
}
}
/// 断开连接
Future<void> disconnect() async {
try {
await _bleService.disconnect();
} catch (e) {
Logger.error('断开连接失败: ${e.toString()}');
}
}
/// 验证设备是否可以配对 /// 验证设备是否可以配对
Future<bool> verifyDeviceCanPair(String macAddress) async { Future<bool> verifyDeviceCanPair(String macAddress) async {
try { try {

9
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:classic_bluetooth/classic_bluetooth.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:get/get.dart'; import 'package:get/get.dart';
@ -54,7 +53,6 @@ class OtaUpgradeController extends GetxController {
// 蓝牙管理服务,用于获取设备信息和通信 // 蓝牙管理服务,用于获取设备信息和通信
final _bleManager = Get.find<BleManager>(); final _bleManager = Get.find<BleManager>();
final BleService _bleService = BleService();
final maxRetryAttempts = 3; // 最大重试次数 final maxRetryAttempts = 3; // 最大重试次数
int currentRetryCount = 0; int currentRetryCount = 0;
@ -73,7 +71,7 @@ class OtaUpgradeController extends GetxController {
@override @override
void onClose() { void onClose() {
jlota.onOtaClose(); jlota.onOtaClose();
_bleService.setEnterOta(false); _bleManager.setEnterOta(false);
isUpgrade.value = false; isUpgrade.value = false;
super.onClose(); super.onClose();
} }
@ -177,8 +175,7 @@ class OtaUpgradeController extends GetxController {
_address = connectedDevices.first.address; _address = connectedDevices.first.address;
} }
await _bleService.setEnterOta(true); await _bleManager.setEnterOta(true);
await _bleService.disconnect();
await Future.delayed(const Duration(seconds: 2)); await Future.delayed(const Duration(seconds: 2));
@ -422,7 +419,7 @@ class OtaUpgradeController extends GetxController {
if (event.event == 'onError') { if (event.event == 'onError') {
type.value = OtaEvent.UPGRADE_FAILED; type.value = OtaEvent.UPGRADE_FAILED;
isUpgrade.value = false; isUpgrade.value = false;
Get.snackbar('升级失败', '请检查设备连接或升级包'); Get.snackbar('升级失败', '请检查设备连接或升级包,错误码:${event.code}');
} }
if (event.event == 'NOCONNECTION') { if (event.event == 'NOCONNECTION') {
type.value = OtaEvent.CONNECTION_DISCONNECTION; type.value = OtaEvent.CONNECTION_DISCONNECTION;

7
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) { public func connect(to peripheral: CBPeripheral) {
connectPeripheral(peripheral) connectPeripheral(peripheral)
} }
/// 自动重新连接
public func automaticReconnection() {
if let peripheral = peripheralDevice {
connectPeripheral(peripheral)
}
}
/// 统一的连接方法,支持恢复和重连 /// 统一的连接方法,支持恢复和重连
private func connectPeripheral(_ peripheral: CBPeripheral) { private func connectPeripheral(_ peripheral: CBPeripheral) {

5
local_plugins/ble_service/ios/ble_service/Sources/ble_service/SwiftBleServicePlugin.swift

@ -154,6 +154,11 @@ public class SwiftBleServicePlugin: NSObject, FlutterPlugin {
case "setEnterOta": case "setEnterOta":
let arguments = call.arguments as? [String: Any] let arguments = call.arguments as? [String: Any]
BleService.shared.isEnterOta = (arguments?["enterOta"] as? Bool) ?? false BleService.shared.isEnterOta = (arguments?["enterOta"] as? Bool) ?? false
if BleService.shared.isEnterOta {
BleService.shared.disconnect()
} else {
BleService.shared.automaticReconnection()
}
result("") result("")
case "disconnect": case "disconnect":

7
local_plugins/ota/ios/Classes/BleManager/JLBleManager.h

@ -66,11 +66,6 @@ extern NSString *kFLT_BLE_DISCONNECTED; //断开连接
@property (nonatomic, copy, nullable) FlutterEventSink flutterEventSink; @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)otaFuncCancel:(CANCEL_CALLBACK _Nonnull)result;
- (void)releaseAllResources;
@end @end
NS_ASSUME_NONNULL_END NS_ASSUME_NONNULL_END

37
local_plugins/ota/ios/Classes/BleManager/JLBleManager.m

@ -45,15 +45,6 @@ NSString *FLT_BLE_RCSP_R = @"AE02"; //命令“读”通道
@implementation JLBleManager @implementation JLBleManager
+ (instancetype)sharedInstance {
static JLBleManager *singleton = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
singleton = [[self alloc] init];
});
return singleton;
}
- (instancetype)init { - (instancetype)init {
self = [super init]; self = [super init];
if (self) { if (self) {
@ -475,6 +466,8 @@ NSString *FLT_BLE_RCSP_R = @"AE02"; //命令“读”通道
} }
} else if (result == JL_OTAResultReboot) { } else if (result == JL_OTAResultReboot) {
} else if (result == JL_OTAResultDisconnect) {
} else { } else {
if (self.flutterEventSink) { if (self.flutterEventSink) {
self.flutterEventSink(@{ self.flutterEventSink(@{
@ -572,6 +565,32 @@ NSString *FLT_BLE_RCSP_R = @"AE02"; //命令“读”通道
type:CBCharacteristicWriteWithoutResponse]; 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 @end

6
local_plugins/ota/ios/Classes/OtaPlugin.swift

@ -17,7 +17,7 @@ public class OtaPlugin: NSObject, FlutterPlugin, FlutterStreamHandler {
switch call.method { switch call.method {
case "init": case "init":
userManager = JLBleManager.sharedInstance() userManager = JLBleManager.init()
result("") result("")
case "startUpgrading": case "startUpgrading":
let address = arguments?["address"] as? String let address = arguments?["address"] as? String
@ -54,9 +54,7 @@ public class OtaPlugin: NSObject, FlutterPlugin, FlutterStreamHandler {
} }
private func onOtaClose() { private func onOtaClose() {
userManager?.stopScanBLE() userManager?.releaseAllResources()
userManager?.otaFuncCancel{code in}
userManager?.disconnectBLE()
userManager = nil userManager = nil
} }
} }

Loading…
Cancel
Save