Commit d8691416 authored by Sky's avatar Sky

~

parent 7ffe7ac4
......@@ -5,11 +5,11 @@ ohos {
signingConfigs {
release {
storeFile file('D:/HuaweiWatchClient/huawei_watch_overseas.p12')
storePassword '0000001B37FBF75B69C866355CBEFC208DDF92764DE954F1C754CBD53E1B481433E2DBB6E2C3132D049A89'
storePassword '0000001BFDB36C87CF75CE5C9C35B4EB247F0B973A3CEBFCE636F022CE70CBF7EA5D483CB9896BA93C3146'
keyAlias 'huawei_watch_overseas Release'
keyPassword '0000001BE894FABE766383262AD346CB8151121BA00764539557B67C6917C8C4B031ADCF67E1384F5FE120'
profile file('D:/HuaweiWatchClient/com.company.fit.provisionRelease.p7b')
certpath file('D:/HuaweiWatchClient/com.company.fit.overseas_release.cer')
keyPassword '0000001B32D22EDA409673EBC987E06A1784506F044E7486B4A2590B645408BBD960C712D03072C22FE304'
profile file('D:\\HuaweiWatchClient\\com.company.fit.provisionRelease.p7b')
certpath file('D:\\HuaweiWatchClient\\com.company.fit.overseas_release.cer')
signAlg 'SHA256withECDSA'
}
}
......@@ -30,7 +30,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.huawei.ohos:hap:2.4.1.4'
classpath 'com.huawei.ohos:hap:2.4.2.5'
}
}
......
......@@ -4,20 +4,20 @@ ohos {
signingConfigs {
debug {
certpath file('D:/HuaweiWatchClient/com.company.fit.overseas_debug.cer')
profile file('D:/HuaweiWatchClient/com.company.fit.proDebug.p7b')
profile file('D:/HuaweiWatchClient/FitSmartWatch.p7b')
signAlg 'SHA256withECDSA'
keyPassword '0000001B17F8F53E0CF14D52675968C4FD60AA48E6580B4EEEE4AE8348FA556850E01CC92EEA6DE834CA36'
keyPassword '0000001B329C15F1A79D6DE1A35FC33EAC9D748ED2C9769503449638BCD341685E783C445341B6EF7252A0'
keyAlias 'huawei_watch_overseas Release'
storePassword '0000001B16A4C8B18003852BF6E890B38F6D624C8314D7E55CE2FD87CBC3F2C9E2DA9403C977F0C988ABAE'
storePassword '0000001BED3DA1157AED6BA705ACB0E5A74DF33AB09917EF841F6048AA1D583907E32B547CE23E3D6415F8'
storeFile file('D:/HuaweiWatchClient/huawei_watch_overseas.p12')
}
release {
storeFile file('D:/HuaweiWatchClient/huawei_watch_overseas.p12')
storePassword '0000001B76038D1DF78CD8E9F7D8F4E133ECE3FABA895EE4FEE9DDC4225267E3D2176904B14F7BF71AF47D'
storePassword '0000001BE711A291624105B6065E58CF3582A64BD480021F7FA44D831AF7F47D3BAA28EE72E1ED5200671F'
keyAlias 'huawei_watch_overseas Release'
keyPassword '0000001B4BECAFAB52D2A2F0F1D06B36F5343980B0FD548D1BB1A84211DB54738785744DD4DF2640AAD1B5'
keyPassword '0000001B9650C5F04495DB8963B0EA3435C222F7566D397075C0BCA00938DC2AC4A57E46138E9C0F70324C'
signAlg 'SHA256withECDSA'
profile file('D:/HuaweiWatchClient/com.company.fit.provisionRelease.p7b')
profile file('D:\\HuaweiWatchClient\\com.company.fit.provisionRelease.p7b')
certpath file('D:/HuaweiWatchClient/com.company.fit.overseas_release.cer')
}
}
......@@ -27,14 +27,14 @@ ohos {
compatibleSdkVersion 3
}
}
sourceSets{
main{
java{
sourceSets {
main {
java {
srcDir 'build\\generated\\source\\r\\debug'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
testCompile'junit:junit:4.12'
testCompile 'junit:junit:4.12'
}
......@@ -2,8 +2,8 @@
"app": {
"bundleName": "com.company.fit",
"version": {
"code": 20,
"name": "1.0.2"
"code": 25,
"name": "1.0.3"
},
"apiVersion": {
"compatible": 3,
......@@ -20,7 +20,7 @@
{
"name": "default",
"icon": "$media:icon",
"label": "Fit健身",
"label": "$string:app_name",
"type": "page"
}
],
......@@ -35,6 +35,11 @@
"extra": "",
"name": "supportLists",
"value": "com.sportq.fit:B1B788A1BBD5F459BC641DA8CE690EA065C96957F58E883764982B5C46E53E06"
},
{
"extra": "",
"name": "hwc-theme",
"value": "androidhwext:style/Theme.Emui.Dark.NoActionBar"
}
]
},
......@@ -57,7 +62,7 @@
],
"when": "always"
},
"name": "harmonyos.permission.READ_HEALTH_DATA"
"name": "ohos.permission.READ_HEALTH_DATA"
}
]
}
......
/*
* Copyright (c) 2020 Huawei Device Co., Ltd.
* Copyright (c) 2021 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
......@@ -11,96 +11,145 @@
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* Version: 5.0.1.301
* Version: 5.0.2.300
* Description: wearEngine SDK
*/
import wearengine from "@system.wearengine";
var P2pClient = (function () {
var WearEngineConst = {
DELAY_FILEAPP_VALUE: "internal://app/", // 三方应用默认文件存储路径
DEFAULT_EMPTY_VALUE: "", // 默认空字符串
};
var ErrorCode = {
MSG_ERROR_PING_WATCH_APP_NOT_EXIST: 200, // 手表应用未安装。
MSG_ERROR_PING_WATCH_APP_NOT_RUNNING: 201, // 手表应用已安装未启动。
MSG_ERROR_PING_WATCH_APP_EXIST_RUNNING: 202, // 手表应用已安装已启动。
MSG_ERROR_PING_OTHER: 203, // 其他错误。
MSG_ERROR_PING_PHONE_APP_NOT_EXIST: 204, //手机应用未安装。
MSG_ERROR_PING_PHONE_APP_NOT_RUNNING: 205, // 手机应用已安装。
MSG_ERROR_SEND_FAIL: 206, // 发送消息失败。
MSG_ERROR_SEND_SUCCESS: 207, // 发送消息成功。
MSG_ERROR_CODE_VERSION_TOO_LOW: 208 // 手表wearEngine版本太低。
};
var MessageType = {
MESSAGE_TYPE_DATA: 0, // 文本消息
MESSAGE_TYPE_FILE: 1 // 文件消息
};
var WEARENGINE_SERVICE_VISION_302 = 302;
var WEARENGINE_SERVICE_VISION_303 = 303;
var WEARENGINE_SDK_VERSION = "3";
var FILE_PUNLIC_PATH_INTERCETPED = 15;
var DEFAULT_EMPTY_FINGERPRINT = " ";
var P2pClient = (function() {
var peerPkgName;
var peerFingerPrint;
var version;
function P2pClient(context) {
function P2pClient() {
try {
var jsSdk = this;
var getVersionCallBack = function (data) {
var getVersionCallBack = function(data) {
if (data) {
var versionArray = data.split(".");
jsSdk.version = versionArray[versionArray.length-1];
console.log("service sdk version is: " + data);
}else{
jsSdk.version = versionArray[versionArray.length - 1];
console.info("service sdk version is: " + data);
} else {
jsSdk.version = 0;
console.log("get service sdk version failed");
console.info("get service sdk version failed");
}
};
wearengine.getWearEngineVersion({
complete: getVersionCallBack,
sdkVersion: "2",
sdkVersion: WEARENGINE_SDK_VERSION
});
} catch(error) {
} catch (error) {
jsSdk.version = 0;
console.log("service sdk version is too low" + error.message);
console.info("service sdk version is too low" + error.message);
}
}
/**
* 设置手机应用的packageName
* peerPkgName: string
*/
P2pClient.prototype.setPeerPkgName = function (peerPkgName) {
P2pClient.prototype.setPeerPkgName = function(peerPkgName) {
var successCallBack = function() {
console.info("setPeerPkgName success.");
};
var failCallBack = function() {
console.info("setPeerPkgName fail.");
};
this.peerPkgName = peerPkgName;
if (this.version < WEARENGINE_SERVICE_VISION_303) {
console.info("ERROR_CODE_VERSION_TOO_LOW setPackageName is invalid");
return;
}
wearengine.setPackageName({
appName: peerPkgName,
complete: successCallBack,
fail: failCallBack
});
};
/**
* 设置手机侧指纹信息
* fingerPrint: string
*/
P2pClient.prototype.setPeerFingerPrint = function (fingerPrint) {
if (this.version < 302) {
console.log("setPeerFingerPrint interface invalid");
P2pClient.prototype.setPeerFingerPrint = function(fingerPrint) {
if (this.version < WEARENGINE_SERVICE_VISION_302) {
console.info("setPeerFingerPrint interface invalid");
var obj = {
data: "ERROR_CODE_VERSION_TOO_LOW",
code: 208,
code: ErrorCode.MSG_ERROR_CODE_VERSION_TOO_LOW
};
return obj;
}
if (isEmpty(fingerPrint)) {
fingerPrint = DEFAULT_EMPTY_FINGERPRINT;
}
this.peerFingerPrint = fingerPrint;
var successCallBack = function () {
console.log("fingerPrint setup succeeded");
var successCallBack = function() {
console.info("setPeerFingerPrint success.");
};
var failCallBack = function () {
console.log("fingerPrint setup failed");
var failCallBack = function() {
console.error("setPeerFingerPrint failed.");
};
wearengine.setFingerprint({
appName: this.peerPkgName,
appCert: fingerPrint,
complete: successCallBack,
fail: failCallBack,
fail: failCallBack
});
};
/**
* 检测对端设备侧是否已经安装指定应用
* pingCallback: object:onSuccess(),onFailure(),onPingResult(resultCode)
*/
P2pClient.prototype.ping = function (pingCallback) {
P2pClient.prototype.ping = function(pingCallback) {
var successCode = {
data: "ERROR_CODE_P2P_PHONE_APP_EXIT",
code: 205,
data: " ERROR_CODE_P2P_PHONE_APP_EXIST",
code: ErrorCode.MSG_ERROR_PING_PHONE_APP_NOT_RUNNING
};
var successCallBack = function () {
var successCallBack = function() {
pingCallback.onSuccess();
pingCallback.onPingResult(successCode);
console.info("ping success.");
};
var notInstallCode = {
data: "ERROR_CODE_P2P_PHONE_APP_NOT_EXIT",
code: 204,
data: "ERROR_CODE_P2P_PHONE_APP_NOT_EXIST",
code: ErrorCode.MSG_ERROR_PING_PHONE_APP_NOT_EXIST
};
var failCode = {
data: "ERROR_CODE_P2P_OTHER_ERROR",
code: 203,
code: ErrorCode.MSG_ERROR_PING_OTHER
};
var failCallBack = function (data, code) {
if (isEmpty(this.peerPkgName)) {
pingCallback.onFailure();
pingCallback.onPingResult(notInstallCode);
return;
}
var failCallBack = function(data, code) {
if (!data && !code) {
pingCallback.onFailure();
pingCallback.onPingResult(notInstallCode);
......@@ -108,43 +157,71 @@ var P2pClient = (function () {
pingCallback.onFailure();
pingCallback.onPingResult(failCode);
}
console.error("ping failed.");
};
FeatureAbility.detect({
bundleName: this.peerPkgName,
success: successCallBack,
fail: failCallBack,
fail: failCallBack
});
};
/**
* 注册消息监听接口
* receiver:object:onSuccess(),onFailure(),onReceiveMessage(message)
*/
P2pClient.prototype.registerReceiver = function (receiver) {
P2pClient.prototype.registerReceiver = function(receiver) {
if (!receiver) {
return;
}
var successCallBack = function (data) {
receiver.onSuccess();
if (this.version > WEARENGINE_SERVICE_VISION_302) {
if (isEmpty(this.peerPkgName)) {
console.error("peerPkgName cannot be empty");
receiver.onFailure();
return;
}
var setPeerPkgNameSuccessCallBack = function() {
console.info("registerReceiver setPeerPkgName success.");
};
var setPeerPkgNameFailCallBack = function() {
console.info("registerReceiver setPeerPkgName fail.");
return;
};
wearengine.setPackageName({
appName: this.peerPkgName,
complete: setPeerPkgNameSuccessCallBack,
fail: setPeerPkgNameFailCallBack
});
}
if (isEmpty(this.peerFingerPrint)) {
console.error("peerFingerPrint cannot be empty");
receiver.onFailure();
return;
}
var successCallBack = function(data) {
if (data) {
if (data.isFileType) {
if (data.isRegister) {
receiver.onSuccess();
} else if (data.isFileType){
var fileObj = {
isFileType: true,
name: data.file,
mode: "",
mode2: "",
mode2: ""
};
receiver.onReceiveMessage(fileObj);
console.info("receive file name:" + fileObj.name);
} else {
receiver.onReceiveMessage(data.message);
console.info("receive message:" + data.message);
}
} else {
receiver.onReceiveMessage(data);
receiver.onSuccess();
}
};
FeatureAbility.subscribeMsg({
success: successCallBack,
fail: receiver.onFailure,
fail: receiver.onFailure
});
};
/**
......@@ -153,30 +230,43 @@ var P2pClient = (function () {
* sendCallback: object:onSuccess(),onFailure(),onSendResult(resultCode),onSendProgress(count)
* resultCode: SUCCESS 207, FAILURE 206
*/
P2pClient.prototype.send = function (message, sendCallback) {
P2pClient.prototype.send = function(message, sendCallback) {
if (!message || !sendCallback) {
return;
}
if (message.getType() == 0) {
var successCallBack = function () {
var failCode = {
data: "peerPkgName or peerFingerPrint empty",
code: ErrorCode.MSG_ERROR_SEND_FAIL
};
if (isEmpty(this.peerPkgName) || isEmpty(this.peerFingerPrint)) {
console.error("peerPkgName or peerFingerPrint cannot be empty");
sendCallback.onFailure();
sendCallback.onSendResult(failCode);
return;
}
if (message.getType() == MessageType.MESSAGE_TYPE_DATA) {
var successCallBack = function() {
var successCode = {
data: "ERROR_CODE_COMM_SUCCESS",
code: 207,
code: ErrorCode.MSG_ERROR_SEND_SUCCESS
};
sendCallback.onSuccess();
sendCallback.onSendResult(successCode);
sendCallback.onSendProgress(100 + "%");
console.info("send message success.");
};
var failCallBack = function (errorMessage, code) {
var failCallBack = function(errorMessage, code) {
var failCode = {
data: errorMessage,
code: 206,
code: ErrorCode.MSG_ERROR_SEND_FAIL
};
sendCallback.onFailure();
sendCallback.onSendResult(failCode);
sendCallback.onSendProgress(0 + "%");
console.error("send message failed.");
};
FeatureAbility.sendMsg({
deviceId: "remote",
......@@ -184,47 +274,64 @@ var P2pClient = (function () {
abilityName: "",
message: message.getData(),
success: successCallBack,
fail: failCallBack,
fail: failCallBack
});
} else {
if (this.version < 302) {
console.log("not support send file");
if (this.version < WEARENGINE_SERVICE_VISION_302) {
console.info("not support send file");
var obj = {
data: "ERROR_CODE_VERSION_TOO_LOW",
code: 208,
code: ErrorCode.MSG_ERROR_CODE_VERSION_TOO_LOW
};
sendCallback.onFailure();
sendCallback.onSendResult(obj);
return;
}
var successCallBack = function () {
var fileName = message.getFile().name;
// service 版本 303以下不支持 "internal://app/" 路径
if (this.version < WEARENGINE_SERVICE_VISION_303) {
var TempPath = fileName.substring(0, FILE_PUNLIC_PATH_INTERCETPED);
if (TempPath == WearEngineConst.DELAY_FILEAPP_VALUE) {
var obj = {
data: "ERROR_CODE_VERSION_TOO_LOW",
code: ErrorCode.MSG_ERROR_CODE_VERSION_TOO_LOW
};
sendCallback.onFailure();
sendCallback.onSendResult(obj);
console.info("wearEngine service version too low, file path not support");
return;
}
}
var successCallBack = function() {
var successCode = {
data: "ERROR_CODE_COMM_SUCCESS",
code: 207,
code: ErrorCode.MSG_ERROR_SEND_SUCCESS
};
sendCallback.onSuccess();
sendCallback.onSendResult(successCode);
};
var failCallBack = function (errorMessage, code) {
var failCallBack = function(errorMessage, code) {
var failCode = {
data: errorMessage,
code: 206,
code: ErrorCode.MSG_ERROR_SEND_FAIL
};
sendCallback.onFailure();
sendCallback.onSendResult(failCode);
};
var progressCallBack = function (data) {
var progressCallBack = function(data) {
sendCallback.onSendProgress(data.progressNum + "%");
console.info("progress of sending file: " + data.progressNum + "%");
};
var fileName = message.getFile().name;
wearengine.uploadFile({
fileName: fileName,
filePath: this.peerPkgName,
peerPackageName: this.peerPkgName,
success: successCallBack,
fail: failCallBack,
progress: progressCallBack,
progress: progressCallBack
});
}
};
......@@ -232,80 +339,93 @@ var P2pClient = (function () {
* 注销监听接口
* receiver: onSuccess()
*/
P2pClient.prototype.unregisterReceiver = function (receiver) {
P2pClient.prototype.unregisterReceiver = function(receiver) {
FeatureAbility.unsubscribeMsg();
receiver.onSuccess();
};
/**
* 判断参数是否为空
* parameter: string
*/
function isEmpty(parameter) {
return parameter == "undefined" || parameter == null || parameter == "" || parameter == " ";
}
return P2pClient;
})();
/**
* 文件格式
* 文件格式类型
* name: file name with path
* mode: 'text' or 'binary'
* mode2: 'R', 'W', 'RW'
*/
var File = (function () {
var File = (function() {
var name;
var mode;
var mode2;
function File() {}
return File;
})();
var Builder = (function () {
var Builder = (function() {
var messageInfo;
function Builder(context) {}
function Builder() {}
Builder.prototype.setDescription = function (description) {
Builder.prototype.setDescription = function(description) {
this.messageInfo = description;
this.messageType = 0;
this.messageType = MessageType.MESSAGE_TYPE_DATA;
};
/**
* 设置messge信息(两种格式任选其一)
* data: ArrayBuffer
* data: File(暂时不支持)
*/
Builder.prototype.setPayload = function (data) {
Builder.prototype.setPayload = function(data) {
if (!data) {
return;
}
if (typeof data == "object" && data.name) {
this.messageType = 1;
this.messageType = MessageType.MESSAGE_TYPE_FILE;
return this.setFilePlayload(data);
} else {
this.messageType = 0;
this.messageType = MessageType.MESSAGE_TYPE_DATA;
return this.setBufferPlayload(data);
}
};
Builder.prototype.setBufferPlayload = function (data) {
Builder.prototype.setBufferPlayload = function(data) {
this.messageInfo = String.fromCharCode.apply(null, new Uint16Array(data));
};
Builder.prototype.setFilePlayload = function (data) {
Builder.prototype.setFilePlayload = function(data) {
this.messageInfo = JSON.stringify(data);
};
return Builder;
})();
var Message = (function () {
var Message = (function() {
var builder = new Builder();
function Message(context) {}
Message.prototype.describeContents = function () {
function Message() {}
Message.prototype.describeContents = function() {
return this.builder.messageInfo;
};
/**
* 获取传送时的信息
*/
Message.prototype.getData = function () {
Message.prototype.getData = function() {
return this.builder.messageInfo;
};
Message.prototype.getDescription = function () {
Message.prototype.getDescription = function() {
return this.builder.messageInfo;
};
/**
* 获取文件信息
*/
Message.prototype.getFile = function () {
if (this.builder.messageType == 1) {
Message.prototype.getFile = function() {
if (this.builder.messageType == MessageType.MESSAGE_TYPE_FILE) {
return JSON.parse(this.builder.messageInfo);
}
return null;
......@@ -315,14 +435,10 @@ var Message = (function () {
* 0 string
* 1 File
*/
Message.prototype.getType = function () {
Message.prototype.getType = function() {
return this.builder.messageType;
};
return Message;
})();
export {
P2pClient,
Message,
Builder
};
export { P2pClient, Message, Builder };
......@@ -17,13 +17,14 @@ export default {
let _this = this;
//获取客户端信息
_this.getMsg();
},
onShow() {
let _this = this;
//更新页面数据
_this.acCalorie = _this.endCalorie;
_this.fitTotalDuration = _this.endTotalTime;
_this.averHeartRate = _this.endAverHeartRate;
_this.maxHeartRate = _this.endMaxHeartRate;
},
onShow() {
//取消常亮
brightness.setKeepScreenOn({
keepScreenOn: false,
......@@ -35,6 +36,7 @@ export default {
},
onDestroy() {
//取消接收手机侧第三方应用发过来的消息
console.error("end--onDestroy:unregisterReceiver")
p2pClient.unregisterReceiver({
onSuccess: function () {
},
......
......@@ -193,11 +193,12 @@
/**已训练**/
.train-new-title {
font-size: 38px;
height: 38px;
height: 40px;
margin-top: 60px;
width: 350px;
color: #FFFFFF;
text-align: center;
line-hight: 40px;
}
.train-time {
justify-content: center;
......
......@@ -38,6 +38,7 @@ export default {
},
onInit() {
p2pClient.setPeerPkgName("com.sportq.fit");
p2pClient.setPeerFingerPrint("B1B788A1BBD5F459BC641DA8CE690EA065C96957F58E883764982B5C46E53E06");
},
onReady() {
let _this = this;
......@@ -129,7 +130,7 @@ export default {
},2 * 60 * 1000);
}
//数据重置
_this.init();
_this.initData();
_this.canReceiveMessage = true;
if (_this.isFirstInit) {
_this.isFirstInit = false;
......@@ -368,7 +369,7 @@ export default {
},
onSendResult: function (resultCode) {
// send任务执行结束后返回的错误码和信息进行处理
console.log(resultCode.data + resultCode.code);
console.log("sendMsg:" + resultCode.data + resultCode.code);
},
}
if (sendMessage != null && sendCallback != null) {
......@@ -424,9 +425,11 @@ export default {
var receiver = {
onSuccess: function () {
// 处理注册接收手机侧简短消息或文件成功的回调函数
console.error("index--getMsg:registerReceiver:onSuccess")
},
onFailure: function () {
// 处理注册接收手机侧简短消息或文件失败的回调函数
console.error("index--getMsg:registerReceiver:onFailure")
},
onReceiveMessage: function (data) {
_this.start(JSON.parse(data));
......@@ -554,11 +557,6 @@ export default {
this.fitDuration = "00:00";
this.progress = "", //进度条
this.countdownT = "", // 倒计时
//取消接收手机侧第三方应用发过来的消息
p2pClient.unregisterReceiver({
onSuccess: function () {
},
});
sensor.unsubscribeHeartRate() //取消监听心率
},
......@@ -566,7 +564,7 @@ export default {
/**
*数据重置
*/
init() {
initData() {
this.isStop = false;
this.sendFlg = false;
this.stopFlg = false;
......
/*
* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. All rights reserved.
* Wear Engine Library For wearable device v5.0.1.301
*
* Copyright (c) Huawei Technologies Co., Ltd. 2020-2021. All rights reserved.
*
* Date: 2021-04-06
*/
import wearengine from "@system.wearengine";
import file from "@system.file";
var P2pClient;
var Builder;
const injectRef = Object.getPrototypeOf(global) || global;
try {
const device = require("@system.device");
device.getInfo({
success: function(data) {
globalThis.isLite = !(data.product.includes("GLL") || data.product.includes("Galileo"));
},
fail: function(data) {
globalThis.isLite = !(data.product.includes("GLL") || data.product.includes("Galileo"));
}
});
} catch (error) {
console.error("not support system.device, service sdk version is too low" + error.message);
}
injectRef.regeneratorRuntime = require("@babel/runtime/regenerator");
var FeatureAbilityOption = {
BUNDLE_NAME: "com.huawei.watch.kit.hiwearability", // 设置AA侧服务应用包名称
......@@ -41,8 +32,9 @@ var WearEngineConst = {
DELAY_TIME_VALUE: 1000, // 消息发送延时默认值
DELAY_FILEAPP_VALUE: "internal://app/", // 三方应用默认文件存储路径
DEFAULT_EMPTY_VALUE: "", // 默认空字符串
MAX_TRANSFER_SIZE: 1024, // 分段发送文件时,每段的大小
FILE_TO_BE_CONTINUED: 1 // 需要继续发送文件的返回码
MAX_TRANSFER_SIZE: 1024 * 50, // 分段发送文件时,每段的大小
FILE_TO_BE_CONTINUED: 1, // 需要继续发送文件的返回码
END_FLAG: 1 // 手机给手表发送文件已经结束的标志
};
var ErrorCode = {
MSG_ERROR_PING_WATCH_APP_NOT_EXIST: 200, // 手表应用未安装。
......@@ -62,313 +54,7 @@ var MessageType = {
var isFunction = obj => {
return typeof obj === "function" && typeof obj.nodeType !== "number";
};
var WEARENGINE_SERVICE_VISION_303 = 303;
var WEARENGINE_SDK_VERSION = "3";
if (typeof globalThis == "undefined" || globalThis.isLite) {
P2pClient = (function() {
var peerPkgName;
var peerFingerPrint;
var version;
function P2pClient() {
try {
var jsSdk = this;
var getVersionCallBack = function(data) {
if (data) {
var versionArray = data.split(".");
jsSdk.version = versionArray[versionArray.length - 1];
console.log("service sdk version is: " + data);
} else {
jsSdk.version = 0;
console.log("get service sdk version failed");
}
};
wearengine.getWearEngineVersion({
complete: getVersionCallBack,
sdkVersion: WEARENGINE_SDK_VERSION
});
} catch (error) {
jsSdk.version = 0;
console.log("service sdk version is too low" + error.message);
}
}
/**
* 设置手机应用的packageName
* peerPkgName: string
*/
P2pClient.prototype.setPeerPkgName = function(peerPkgName) {
var successCallBack = function() {
console.info("setPeerPkgName success.");
};
var failCallBack = function() {
console.info("setPeerPkgName fail.");
};
this.peerPkgName = peerPkgName;
if (this.version < WEARENGINE_SERVICE_VISION_303) {
console.log("ERROR_CODE_VERSION_TOO_LOW setPackageName is invalid");
return;
}
wearengine.setPackageName({
appName: peerPkgName,
complete: successCallBack,
fail: failCallBack
});
};
/**
* 设置手机侧指纹信息
* fingerPrint: string
*/
P2pClient.prototype.setPeerFingerPrint = function(fingerPrint) {
if (this.version < 302) {
console.log("setPeerFingerPrint interface invalid");
var obj = {
data: "ERROR_CODE_VERSION_TOO_LOW",
code: ErrorCode.MSG_ERROR_CODE_VERSION_TOO_LOW
};
return obj;
}
this.peerFingerPrint = fingerPrint;
var successCallBack = function() {
console.info("setPeerFingerPrint success.");
};
var failCallBack = function() {
console.error("setPeerFingerPrint failed.");
};
wearengine.setFingerprint({
appName: this.peerPkgName,
appCert: fingerPrint,
complete: successCallBack,
fail: failCallBack
});
};
/**
* 检测对端设备侧是否已经安装指定应用
* pingCallback: object:onSuccess(),onFailure(),onPingResult(resultCode)
*/
P2pClient.prototype.ping = function(pingCallback) {
var successCode = {
data: " ERROR_CODE_P2P_PHONE_APP_EXIST",
code: ErrorCode.MSG_ERROR_PING_PHONE_APP_NOT_RUNNING
};
var successCallBack = function() {
pingCallback.onSuccess();
pingCallback.onPingResult(successCode);
console.info("ping success.");
};
var notInstallCode = {
data: "ERROR_CODE_P2P_PHONE_APP_NOT_EXIST",
code: ErrorCode.MSG_ERROR_PING_PHONE_APP_NOT_EXIST
};
var failCode = {
data: "ERROR_CODE_P2P_OTHER_ERROR",
code: ErrorCode.MSG_ERROR_PING_OTHER
};
if (isEmpty(this.peerPkgName)) {
pingCallback.onFailure();
pingCallback.onPingResult(notInstallCode);
return;
}
var failCallBack = function(data, code) {
if (!data && !code) {
pingCallback.onFailure();
pingCallback.onPingResult(notInstallCode);
} else {
pingCallback.onFailure();
pingCallback.onPingResult(failCode);
}
console.error("ping failed.");
};
FeatureAbility.detect({
bundleName: this.peerPkgName,
success: successCallBack,
fail: failCallBack
});
};
/**
* 注册消息监听接口
* receiver:object:onSuccess(),onFailure(),onReceiveMessage(message)
*/
P2pClient.prototype.registerReceiver = function(receiver) {
if (!receiver) {
return;
}
if (isEmpty(this.peerPkgName)) {
console.error("registerReceiver faile because of peerPkgName");
receiver.onFailure();
return;
}
var successCallBack = function(data) {
receiver.onSuccess();
if (data) {
if (data.isFileType) {
var fileObj = {
isFileType: true,
name: data.file,
mode: "",
mode2: ""
};
receiver.onReceiveMessage(fileObj);
console.info("receive message or file name:" + fileObj.name);
} else {
receiver.onReceiveMessage(data.message);
console.info("receive message or file name:" + data.message);
}
} else {
receiver.onReceiveMessage(data);
}
};
FeatureAbility.subscribeMsg({
success: successCallBack,
fail: receiver.onFailure
});
};
/**
* 发送消息接口
* message: Message
* sendCallback: object:onSuccess(),onFailure(),onSendResult(resultCode),onSendProgress(count)
* resultCode: SUCCESS 207, FAILURE 206
*/
P2pClient.prototype.send = function(message, sendCallback) {
if (!message || !sendCallback) {
return;
}
if (message.getType() == 0) {
var successCallBack = function() {
var successCode = {
data: "ERROR_CODE_COMM_SUCCESS",
code: ErrorCode.MSG_ERROR_SEND_SUCCESS
};
sendCallback.onSuccess();
sendCallback.onSendResult(successCode);
sendCallback.onSendProgress(100 + "%");
console.info("send message success.");
};
var failCallBack = function(errorMessage, code) {
var failCode = {
data: errorMessage,
code: ErrorCode.MSG_ERROR_SEND_FAIL
};
sendCallback.onFailure();
sendCallback.onSendResult(failCode);
sendCallback.onSendProgress(0 + "%");
console.error("send message failed.");
};
FeatureAbility.sendMsg({
deviceId: "remote",
bundleName: this.peerPkgName,
abilityName: "",
message: message.getData(),
success: successCallBack,
fail: failCallBack
});
} else {
if (this.version < 302) {
console.log("not support send file");
var obj = {
data: "ERROR_CODE_VERSION_TOO_LOW",
code: ErrorCode.MSG_ERROR_CODE_VERSION_TOO_LOW
};
sendCallback.onFailure();
sendCallback.onSendResult(obj);
return;
}
var successCallBack = function() {
var successCode = {
data: "ERROR_CODE_COMM_SUCCESS",
code: ErrorCode.MSG_ERROR_SEND_SUCCESS
};
sendCallback.onSuccess();
sendCallback.onSendResult(successCode);
console.info("send file success.");
};
var failCallBack = function(errorMessage, code) {
var failCode = {
data: errorMessage,
code: ErrorCode.MSG_ERROR_SEND_FAIL
};
sendCallback.onFailure();
sendCallback.onSendResult(failCode);
console.error("send file failed.");
};
var progressCallBack = function(data) {
sendCallback.onSendProgress(data.progressNum + "%");
console.info("progress of sending file: " + data.progressNum + "%");
};
var fileName = message.getFile().name;
wearengine.uploadFile({
fileName: fileName,
filePath: this.peerPkgName,
peerPackageName: this.peerPkgName,
success: successCallBack,
fail: failCallBack,
progress: progressCallBack
});
}
};
/**
* 注销监听接口
* receiver: onSuccess()
*/
P2pClient.prototype.unregisterReceiver = function(receiver) {
FeatureAbility.unsubscribeMsg();
receiver.onSuccess();
};
/**
* 判断参数是否为空
*
* @param parameter
*/
function isEmpty(parameter) {
return parameter == "undefined" || parameter == null || parameter == "" || parameter == " ";
}
return P2pClient;
})();
Builder = (function() {
var messageInfo;
function Builder() {}
Builder.prototype.setDescription = function(description) {
this.messageInfo = description;
this.messageType = 0;
};
/**
* 设置messge信息(两种格式任选其一)
* data: ArrayBuffer
* data: File(暂时不支持)
*/
Builder.prototype.setPayload = function(data) {
if (!data) {
return;
}
if (typeof data == "object" && data.name) {
this.messageType = 1;
return this.setFilePlayload(data);
} else {
this.messageType = 0;
return this.setBufferPlayload(data);
}
};
Builder.prototype.setBufferPlayload = function(data) {
this.messageInfo = String.fromCharCode.apply(null, new Uint16Array(data));
};
Builder.prototype.setFilePlayload = function(data) {
this.messageInfo = JSON.stringify(data);
};
return Builder;
})();
} else {
const file = require("@system.file");
P2pClient = (function() {
const injectRef = Object.getPrototypeOf(global) || global;
injectRef.regeneratorRuntime = require("@babel/runtime/regenerator");
var P2pClient = (function () {
var peerPkgName;
var peerFingerPrint;
......@@ -387,7 +73,6 @@ if (typeof globalThis == "undefined" || globalThis.isLite) {
var initSuccess = false;
function P2pClient() {
this.setPeerFingerPrint(WearEngineConst.DEFAULT_EMPTY_VALUE, true);
this.subscribeAbilityEvent();
}
......@@ -398,7 +83,7 @@ if (typeof globalThis == "undefined" || globalThis.isLite) {
var action = this.getRequestHeader(RequestCode.SUBSCRIBE_ABILITY_CODEVALUE);
var that = this;
await FeatureAbility.subscribeAbilityEvent(action, function(callbackData) {
await FeatureAbility.subscribeAbilityEvent(action, function (callbackData) {
var callbackJson = JSON.parse(callbackData);
var receivedMessage = JSON.parse(JSON.stringify(callbackJson.data));
......@@ -451,9 +136,6 @@ if (typeof globalThis == "undefined" || globalThis.isLite) {
* @param peerPkgName 目标应用包名称
*/
P2pClient.prototype.setPeerPkgName = async function(peerPkgName) {
if (!peerPkgName) {
return;
}
this.peerPkgName = peerPkgName;
};
......@@ -462,34 +144,16 @@ if (typeof globalThis == "undefined" || globalThis.isLite) {
*
* @param fingerPrint 目前应用指纹参数
*/
P2pClient.prototype.setPeerFingerPrint = async function(fingerPrint, isInit) {
var initInterval = globalThis.setInterval(() => {
if (!isInit && !initSuccess) {
return;
}
globalThis.clearInterval(initInterval);
P2pClient.prototype.setPeerFingerPrint = async function(fingerPrint) {
this.peerFingerPrint = fingerPrint;
var actionData = {
dstPackageName: this.peerPkgName,
dstFingerPrint: this.peerFingerPrint
dstFingerPrint: fingerPrint
};
var action = this.getRequestHeader(RequestCode.INIT_CODEVALUE, actionData);
if (!fingerPrint) {
FeatureAbility.callAbility(action).then(resultStr => {
var resultObj = JSON.parse(resultStr);
if (resultObj.abilityResult == 1) {
console.info("remove p2pClient object success.");
initSuccess = true;
} else {
console.error("remove p2pClient object fail.");
}
});
} else {
globalThis.setTimeout(() => {
FeatureAbility.callAbility(action).then(
resultStr => {
var resultObj = JSON.parse(resultStr);
......@@ -503,9 +167,6 @@ if (typeof globalThis == "undefined" || globalThis.isLite) {
console.error("setPeerFingerPrint failed.");
}
);
}, WearEngineConst.DELAY_INIT_TIME_VALUE);
}
}, WearEngineConst.DELAY_INIT_TIME_VALUE);
};
/**
......@@ -529,12 +190,12 @@ if (typeof globalThis == "undefined" || globalThis.isLite) {
code: ErrorCode.MSG_ERROR_PING_OTHER
};
var successCallBack = function() {
var successCallBack = function () {
isFunction(pingCallback.onSuccess) && pingCallback.onSuccess();
isFunction(pingCallback.onPingResult) && pingCallback.onPingResult(successCode);
};
var failCallBack = function(code) {
var failCallBack = function (code) {
if (code == ErrorCode.MSG_ERROR_PING_PHONE_APP_NOT_EXIST) {
isFunction(pingCallback.onFailure) && pingCallback.onFailure();
isFunction(pingCallback.onPingResult) && pingCallback.onPingResult(notInstallCode);
......@@ -570,15 +231,15 @@ if (typeof globalThis == "undefined" || globalThis.isLite) {
return;
}
var successCallBack = function() {
var successCallBack = function () {
isFunction(receiver.onSuccess) && receiver.onSuccess();
};
var failCallBack = function() {
var failCallBack = function () {
isFunction(receiver.onFailure) && receiver.onFailure();
};
var messageCallBack = function(data) {
var messageCallBack = function (data) {
isFunction(receiver.onReceiveMessage) && receiver.onReceiveMessage(data);
};
......@@ -608,15 +269,15 @@ if (typeof globalThis == "undefined" || globalThis.isLite) {
return;
}
var successCallBack = function() {
var successCallBack = function () {
isFunction(receiver.onSuccess) && receiver.onSuccess();
};
var failCallBack = function() {
var failCallBack = function () {
isFunction(receiver.onFailure) && receiver.onFailure();
};
var messageCallBack = function(data) {
var messageCallBack = function (data) {
isFunction(receiver.onReceiveMessage) && receiver.onReceiveMessage(data);
};
......@@ -657,17 +318,17 @@ if (typeof globalThis == "undefined" || globalThis.isLite) {
that.allMessage =
that.allMessage == undefined ? message.abilityResult : that.allMessage + message.abilityResult;
if (message.seqId && message.seqId != message.cnt) {
if (message.end != WearEngineConst.END_FLAG) {
return;
}
file.delete({
uri: WearEngineConst.DELAY_FILEAPP_VALUE + fileName,
success: function() {
success: function () {
writeFile(fileName, that.allMessage);
that.allMessage = "";
},
fail: function(data, code) {
fail: function (data, code) {
writeFile(fileName, that.allMessage);
that.allMessage = "";
}
......@@ -692,10 +353,10 @@ if (typeof globalThis == "undefined" || globalThis.isLite) {
file.writeArrayBuffer({
uri: WearEngineConst.DELAY_FILEAPP_VALUE + fileName,
buffer: fileContent,
success: function() {
success: function () {
console.info("write Text to file success.");
},
fail: function(data, code) {
fail: function (data, code) {
console.error("write Text to file failed. code: " + code + ", data: " + data);
}
});
......@@ -711,7 +372,7 @@ if (typeof globalThis == "undefined" || globalThis.isLite) {
return;
}
var successCallBack = function() {
var successCallBack = function () {
var successCode = {
data: "ERROR_CODE_COMM_SUCCESS",
code: ErrorCode.MSG_ERROR_SEND_SUCCESS
......@@ -722,7 +383,7 @@ if (typeof globalThis == "undefined" || globalThis.isLite) {
isFunction(sendCallback.onSendProgress) && sendCallback.onSendProgress("100%");
};
var failCallBack = function() {
var failCallBack = function () {
var failCode = {
data: "ERROR_CODE_COMM_FAILED",
code: ErrorCode.MSG_ERROR_SEND_FAIL
......@@ -733,7 +394,7 @@ if (typeof globalThis == "undefined" || globalThis.isLite) {
isFunction(sendCallback.onSendProgress) && sendCallback.onSendProgress("0%");
};
var progressCallBack = function(data) {
var progressCallBack = function (data) {
isFunction(sendCallback.onSendProgress) && sendCallback.onSendProgress(data + "%");
};
......@@ -770,7 +431,16 @@ if (typeof globalThis == "undefined" || globalThis.isLite) {
var fileContent = message.getFile().fileContent;
var cnt = Math.floor(fileContent.length / WearEngineConst.MAX_TRANSFER_SIZE) + 1;
sendFilePart(0, cnt, fileContent, message.getFile().fileName, successCallBack, failCallBack, this);
sendFilePart(
0,
fileContent,
message.getFile().fileName,
successCallBack,
failCallBack,
0,
WearEngineConst.MAX_TRANSFER_SIZE,
this
);
progressCallbackMap[this.peerPkgName] = progressCallBack;
}, WearEngineConst.DELAY_TIME_VALUE);
......@@ -780,22 +450,20 @@ if (typeof globalThis == "undefined" || globalThis.isLite) {
/**
* 递归分段发送文件
* @param {*} index 文件内容分段索引
* @param {*} cnt 总分段数
* @param {*} fileContent 所有的文件内容
* @param {*} fileName 文件名
* @param {*} successCallBack 成功后的回调
* @param {*} failCallBack 失败后的回调
* @param {*} hasSent 已经发送的文件大小
* @param {*} transferSize 分段大小
* @param {*} that this指代
*/
function sendFilePart(index, cnt, fileContent, fileName, successCallBack, failCallBack, that) {
function sendFilePart(index, fileContent, fileName, successCallBack, failCallBack, hasSent, transferSize, that) {
var actionData = {
messageContent: fileContent.substring(
WearEngineConst.MAX_TRANSFER_SIZE * index,
Math.min(WearEngineConst.MAX_TRANSFER_SIZE * (index + 1), fileContent.length)
),
seqId: index + 1,
messageContent: fileContent.substring(hasSent, Math.min(hasSent + transferSize, fileContent.length)),
seqId: hasSent,
fileName,
cnt
cnt: fileContent.length
};
var action = that.getRequestHeader(RequestCode.SENDFILE_CODEVALUE, actionData);
......@@ -803,12 +471,35 @@ if (typeof globalThis == "undefined" || globalThis.isLite) {
resultStr => {
var resultObj = JSON.parse(resultStr);
if (resultObj.abilityResult == WearEngineConst.FILE_TO_BE_CONTINUED) {
sendFilePart(index + 1, cnt, fileContent, fileName, successCallBack, failCallBack, that);
var newTransferSize = Math.min(WearEngineConst.MAX_TRANSFER_SIZE, transferSize * 2);
sendFilePart(
index + 1,
fileContent,
fileName,
successCallBack,
failCallBack,
Math.min(hasSent + transferSize, fileContent.length),
newTransferSize,
that
);
} else if (resultObj.abilityResult != ErrorCode.MSG_ERROR_SEND_SUCCESS) {
console.error("error:" + resultStr);
if (resultObj.code == 2) {
sendFilePart(
index,
fileContent,
fileName,
successCallBack,
failCallBack,
hasSent,
Math.floor(transferSize / 2),
that
);
} else {
failCallBack();
console.error("send file failed.");
} else if (index == cnt - 1) {
}
} else if (hasSent == fileContent.length) {
successCallBack();
console.info("send file success.");
}
......@@ -846,7 +537,7 @@ if (typeof globalThis == "undefined" || globalThis.isLite) {
* @param actionData 请求参数
* @return action 对象
*/
P2pClient.prototype.getRequestHeader = function(code, actionData) {
P2pClient.prototype.getRequestHeader = function (code, actionData) {
var destInfo = {
dstPackageName: this.peerPkgName,
dstFingerPrint: this.peerFingerPrint
......@@ -862,14 +553,14 @@ if (typeof globalThis == "undefined" || globalThis.isLite) {
};
};
return P2pClient;
})();
Builder = (function() {
})();
var Builder = (function () {
var messageInfo;
function Builder() {}
function Builder() {
}
Builder.prototype.setDescription = function(description) {
Builder.prototype.setDescription = function (description) {
this.messageInfo = description;
this.messageType = MessageType.MESSAGE_TYPE_DATA;
};
......@@ -879,7 +570,7 @@ if (typeof globalThis == "undefined" || globalThis.isLite) {
*
* @param data ArrayBuffer File(暂时不支持)
*/
Builder.prototype.setPayload = function(data) {
Builder.prototype.setPayload = function (data) {
if (!data) {
return;
}
......@@ -892,11 +583,11 @@ if (typeof globalThis == "undefined" || globalThis.isLite) {
}
};
Builder.prototype.setBufferPlayload = function(data) {
Builder.prototype.setBufferPlayload = function (data) {
this.messageInfo = String.fromCharCode.apply(null, new Uint16Array(data));
};
Builder.prototype.setFilePlayload = function(data) {
Builder.prototype.setFilePlayload = function (data) {
var that = this;
if (isEmpty(data)) {
return;
......@@ -910,7 +601,7 @@ if (typeof globalThis == "undefined" || globalThis.isLite) {
file.readArrayBuffer({
uri: data.name,
success: function(data) {
success: function (data) {
var fileInfo = {
fileContent: data.buffer,
fileName: fileName
......@@ -918,7 +609,7 @@ if (typeof globalThis == "undefined" || globalThis.isLite) {
that.messageInfo = fileInfo;
console.info("read text from file success.");
},
fail: function(data, code) {
fail: function (data, code) {
var errorInfo = {
errorCode: code,
data
......@@ -929,7 +620,7 @@ if (typeof globalThis == "undefined" || globalThis.isLite) {
});
};
Builder.prototype.setReceivedInfo = function(type, data) {
Builder.prototype.setReceivedInfo = function (type, data) {
this.messageType = type;
this.messageInfo = data;
};
......@@ -944,8 +635,8 @@ if (typeof globalThis == "undefined" || globalThis.isLite) {
}
return Builder;
})();
}
})();
/**
* 文件格式类型
......@@ -953,48 +644,50 @@ if (typeof globalThis == "undefined" || globalThis.isLite) {
* mode: 'text' or 'binary'
* mode2: 'R', 'W', 'RW'
*/
var File = (function() {
var File = (function () {
var name;
var mode;
var mode2;
function File() {}
function File() {
}
return File;
})();
var Message = (function() {
var Message = (function () {
var builder = new Builder();
function Message() {}
function Message() {
}
Message.prototype.describeContents = function() {
Message.prototype.describeContents = function () {
return this.builder.messageInfo;
};
/**
* 获取传送时的信息
*/
Message.prototype.getData = function() {
Message.prototype.getData = function () {
return this.builder.messageInfo;
};
Message.prototype.getDescription = function() {
Message.prototype.getDescription = function () {
return this.builder.messageInfo;
};
/**
* 获取文件信息
*/
Message.prototype.getFile = function() {
Message.prototype.getFile = function () {
if (this.builder.messageType == MessageType.MESSAGE_TYPE_FILE) {
return globalThis.isLite ? JSON.parse(this.builder.messageInfo) : this.builder.messageInfo;
return this.builder.messageInfo;
}
return null;
};
/**
* 获取传输数据类型 0 string,1 File
*/
Message.prototype.getType = function() {
Message.prototype.getType = function () {
return this.builder.messageType;
};
return Message;
})();
export { P2pClient, Message, Builder };
export {P2pClient, Message, Builder};
{
"string": [
{
"name": "app_name",
"value": "Fit健身"
}
]
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment