OTA V3 harmony SDK¶
1.集成¶
使用
ohpm以安装@sifli/sifliotasdk
ohpm install @sifli/sifliotasdk
1.1 导入¶
import { SFOtaV3Manager,OtaV3ImageID,OtaV3ResourceFileInfo,OtaV3DfuFileType,OtaV3BinFileInfo } from '@sifli/sifliotasdk';
1.2权限依赖¶
"ohos.permission.USE_BLUETOOTH",
"ohos.permission.ACCESS_BLUETOOTH",
2.接口调用¶
2.1 初始化¶
private manager:SFOtaV3Manager = SFOtaV3Manager.getInstance();
aboutToAppear(): void {
this.manager = SFOtaV3Manager.getInstance();
this.manager.init(this.getUIContext().getHostContext()!);
this.callbackImpl = new OtaV3CallbackImpl(this);
this.manager.setCallback(this.callbackImpl);
2.2 回调事件接收¶
定义一个class OtaV3CallbackImpl实现委托ISFOtaV3ManagerCallback
export class OtaV3CallbackImpl implements ISFOtaV3ManagerCallback{
private page:OtaNandPage;
constructor(page:OtaNandPage) {
this.page = page;
}
/**
* 0 空闲;1 连接中;2 工作中
* */
onManagerStatusChanged(status: number): void{
this.page.handleStatus(status);
}
/**
* 读取进度
* @param currentBytes
* @param totalBytes
* @param stage @see SFOTAProgressStage 进度阶段
* */
onProgress(currentBytes: number, totalBytes: number): void{
this.page.updateProgress(currentBytes,totalBytes);
}
/**
* 任务完成,响应结果
* @param success 是否成功
* @param error 如果success为false,则该字段为错误信息
* */
onComplete(success: boolean, error: SFError | null): void{
this.page.onComplete(success,error);
}
}
页面中实现回调函数
public handleStatus(status: number):void {
// this.status = status;
const msg = "handleStatus " + status;
SFLog.i(TAG,msg);
this.logs = [...this.logs, msg];
}
public updateProgress(current: number, total: number): void {
// this.progress = (current / total * 100).toFixed(2);
SFLog.i(TAG, `progress ${current}/${total}}`);
this.speedView.viewSpeedByCompleteBytes(current);
this.speedText = this.speedView.getSpeedTextWithBytes(current,total);
if(total > 0){
this.progress = 100 * (current / total);
}
}
public onComplete(success: boolean, error: SFError | null):void {
const msg = `onComplete success=${success},error=${error}`;
this.logs = [...this.logs, msg];
SFLog.i(TAG,msg)
}
2.3 推送接口调用¶
2.3.1 资源传输¶
调用实例
const resourceFileInfo = new OtaV3ResourceFileInfo(OtaV3DfuFileType.ZIP_RESOURCE,this.selectResFilePath!,this.withByteAlign);
this.addLog("启动ota v3 push res..." + this.otaV3Type);
//otaV3Type参见 3.附录
this.manager.startOtaV3ForResource(this.selectedMac, this.otaV3Type,resourceFileInfo,false);
接口定义
/**
* 资源传输接口
* @param mac 蓝牙设备地址
* @param otaV3Type 0-表盘,1-多语言,2-背景图,3-自定义,4-音乐,5-JS,8-4G模块,9,GUIBuilder 表盘 12 GUIBuilder App,13 GUIBuilder扩展资源。其它类型查阅文档
* @param resourceFile 资源文件信息
* @param tryResume 尝试重传
* */
public async startOtaV3ForResource(mac: string,
otaV3Type: number,
resourceFile: OtaV3ResourceFileInfo,
tryResume: boolean): Promise<void>
2.3.2 固件传输¶
调用实例
const binFileInfos = new ArrayList<OtaV3BinFileInfo>();
let ctrlFile:OtaV3BinFileInfo | null = null;
if(!StringUtil.isNullOrEmpty(this.selectCtrlFilePath)){
ctrlFile = new OtaV3BinFileInfo(OtaV3DfuFileType.OTA_CTRL_FILE,this.selectCtrlFilePath!,-1);
binFileInfos.add(ctrlFile);
}
for (const item of this.imageFiles) {
if(item.imageId < 0){
this.toast("请选择Image File Type");
return;
}
const binFileInfo = new OtaV3BinFileInfo(OtaV3DfuFileType.OTA_BIN_FILE,item.filePath,item.imageId, null);
binFileInfos.add(binFileInfo);
}
this.manager.startOtaV3ForFirmware(this.selectedMac,resourceFileInfo,binFileInfos,true);
接口定义
/**
* 固件升级
* @param mac 设备地址
* @param resourceFile 差分包文件描述信息
* @param imageFiles image file,包含ctrlpacket.bin在内。
* @param tryResume 是否为续传
* */
public async startOtaV3ForFirmware(mac: string,
resourceFile: OtaV3ResourceFileInfo | null,
imageFiles: ArrayList<OtaV3BinFileInfo>,
tryResume: boolean): Promise<void>
3.附录¶
OtaV3Type 定义
export class OtaV3Type {
/**表盘 带路径,对齐*/
public static readonly OTA_DYNAMIC_MODULE = 0;
/**多语言 带路径,对齐*/
public static readonly OTA_MUTIL_LANGUAGE = 1;
/**背景图 带路径,对齐*/
public static readonly OTA_BG_PICTURE = 2;
/**自定义文件 带路径,对齐*/
public static readonly OTA_CUSTOM_FILE = 3;
/**音乐 不带路径,*/
public static readonly OTA_MUSIC = 4;
/**qjs 带路径,对齐*/
public static readonly OTA_QJS = 5;
/**拍照预览 不带路径,未实现*/
public static readonly OTA_PHOTO_VIEW = 7;
/**4g模块 不带路径,不对齐,未实现*/
public static readonly OTA_AIC_MODULE = 8;
/**sifliguibuiler 产生的表盘 不带路径,对齐*/
public static readonly OTA_SIFLI_WATCHFACE = 9;
/**固件升级*/
public static readonly OTA_FIRMWARE = 10;
public static readonly OTA_FIRMWARE_MANAGER = 11;
/**sifliguibuilder app 不带路径,对齐*/
public static readonly OTA_SIFLI_APP = 12;
/**app 拓展资源 带路径,对齐*/
public static readonly OTA_SIFLI_APP_RES = 13;
/**app 拓展组件 带路径,对齐*/
public static readonly OTA_SIFLI_APP_WIDGET = 14;
/**app 拓展组件 带路径,对齐*/
public static readonly OTA_PYTHON_APP = 15;
}
4. 错误码¶
错误码参见harmony-sdk-error-code中的android-sdk错误码。