设备 OTA 升级 UI 业务包

更新时间:2023-09-19 03:01:04下载pdf

设备 OTA(Over-the-air)升级指设备通过网络进行固件的下载并自动更新的过程。涂鸦固件升级 UI 业务包可以帮助您的 App 快速集成设备固件升级的能力。

接入组件

在工程的 Podfile 文件中添加 OTA 业务包组件,并执行 pod update 命令:

source "https://github.com/tuya/tuya-pod-specs"
source 'https://cdn.cocoapods.org/'
platform :ios, '11.0'

target 'your_target_name' do
  # 添加固件升级 UI 业务包
  pod 'ThingSmartOTABizBundle'
end

如果需要支持蓝牙设备 OTA 升级,请在项目的 info.Plist 文件中添加蓝牙权限的声明。

NSBluetoothAlwaysUsageDescription
NSBluetoothPeripheralUsageDescription

服务协议

ThingOTAGeneralProtocol

固件升级 UI 业务包实现 ThingOTAGeneralProtocol 协议以提供服务,在 ThingModuleServices 组件中查看 ThingOTAGeneralProtocol.h 协议文件内容为:

#import <Foundation/Foundation.h>

@class ThingSmartDeviceModel;

typedef NS_ENUM(NSUInteger, ThingOTAControllerTheme) {
    ThingOTAControllerWhiteTheme,
    ThingOTAControllerBlackTheme
};

@protocol ThingOTAGeneralProtocol <NSObject>

/**
 检查设备是否支持固件升级

 @param deviceModel 需要检查固件升级的设备
 YES: 支持
 NO: 不支持
 */
- (BOOL)isSupportUpgrade:(ThingSmartDeviceModel *)deviceModel;

/**
 检查设备固件更新,如果有更新会显示展示出固件更新提示

 @param deviceModel 需要检查固件升级的设备
 @param isManual 是否手动检测升级
  @param theme 主题色
 YES: 手动检测升级,检测时弹出loading框。当有固件新版本时(检测升级、强制升级、提醒升级),显示OTA VC。
 NO: 自动检测升级, 检测时不弹出loading框。当有强制升级时、提醒升级时,弹出固件升级提示,点确定后显示OTA VC。
 */
- (void)checkFirmwareUpgrade:(ThingSmartDeviceModel *)deviceModel isManual:(BOOL)isManual theme:(ThingOTAControllerTheme)theme;

@end

ThingOTAGeneralExternalProtocol

若需要自定义处理强制升级返回逻辑(默认为 popToRootViewController),您需要实现 ThingOTAGeneralExternalProtocol 提供的协议方法。

@protocol ThingOTAGeneralExternalProtocol <NSObject>

@optional
/// @brief 取消强制升级事件自定义处理
///
/// 1. 强制升级情况,检测升级中,升级弹框点击“取消” 按钮事件。
/// 2. 强制升级情况,点击 navigationBar 返回按钮(有进度情况会有弹框,点击“确定”按钮)。
///
/// @return YES 自定义处理,NO 默认 `[self.navigationController popToRootViewControllerAnimated:YES]`
- (BOOL)didTapCancelForceUpgrade;

@end

使用指南

任何接口调用之前,务必确保用户已登录。

检测升级和进入升级页面

Objective-C 示例

#import <ThingModuleServices/ThingModuleServices.h>
#import <ThingSmartBizCore/ThingSmartBizCore.h>

+ (void)test:(ThingSmartDeviceModel *)device {
    id<ThingOTAGeneralProtocol> otaImp = [[ThingSmartBizCore sharedInstance] serviceOfProtocol:@protocol(ThingOTAGeneralProtocol)];

    if ([otaImp isSupportUpgrade:device]) {
        [otaImp checkFirmwareUpgrade:device isManual:YES theme:ThingOTAControllerWhiteTheme];
    } else {
        NSLog(@"unsupported");
    }
}

Swift 示例

func test(_ deviceModel: ThingSmartDeviceModel) {
	guard let otaImp = ThingSmartBizCore.sharedInstance().service(of: ThingOTAGeneralProtocol.self) as? ThingOTAGeneralProtocol else { return }

	if otaImp.isSupportUpgrade(deviceModel) {
		otaImp.checkFirmwareUpgrade(deviceModel, isManual: true, theme: .whiteTheme)
	} else {
		print("unsupported")
	}
}

自定义强制升级返回逻辑

强制升级 被取消后,默认调用 [self.navigationController popToRootViewControllerAnimated:YES]。如果需要自定义行为,您需要注册并实现 ThingOTAGeneralExternalProtocol

Objective-C 示例:在适合地方注册 [ThingOTAObjcDemo registerService];

#import <ThingModuleServices/ThingModuleServices.h>
#import <ThingSmartBizCore/ThingSmartBizCore.h>

@interface ThingOTAObjcDemo () <ThingOTAGeneralExternalProtocol>

@end
@implementation ThingOTAObjcDemo

/// 注册协议
+ (void)registerService {
    [[ThingSmartBizCore sharedInstance] registerService:@protocol(ThingOTAGeneralExternalProtocol) withInstance:self];
}

/// 实现协议
#pragma mark - ThingOTAGeneralExternalProtocol -
- (BOOL)didTapCancelForceUpgrade {
    NSLog(@"didTapCancelForceUpgrade");
    return YES;
}

@end

Swift 示例:在适合地方注册 OTASwiftDemo.registerService()

import Foundation

class OTASwiftDemo: NSObject, ThingOTAGeneralExternalProtocol {
    func registerService() {
        ThingSmartBizCore.sharedInstance().registerService(ThingOTAGeneralExternalProtocol.self, withInstance: self)
    }

    // MARK: - ThingOTAGeneralExternalProtocol -
    func didTapCancelForceUpgrade() -> Bool {
        print("didTapCancelForceUpgrade")
        return true
    }
}