Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
H
huaweiWatchClient
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Sky
huaweiWatchClient
Commits
d8691416
Commit
d8691416
authored
May 10, 2021
by
Sky
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
~
parent
7ffe7ac4
Changes
10
Hide whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
915 additions
and
1092 deletions
+915
-1092
build.gradle
build.gradle
+5
-5
build.gradle
entry/build.gradle
+10
-10
config.json
entry/src/main/config.json
+9
-4
wearengine.js
entry/src/main/js/default/liteWearable/wearengine.js
+397
-281
end.js
entry/src/main/js/default/pages/end/end.js
+4
-2
index.css
entry/src/main/js/default/pages/index/index.css
+2
-1
index.js
entry/src/main/js/default/pages/index/index.js
+6
-8
wearengine.js
entry/src/main/js/default/wearable/wearengine.js
+474
-781
string.json
entry/src/main/resources/base/element/string.json
+8
-0
yucong0322.p7b
yucong0322.p7b
+0
-0
No files found.
build.gradle
View file @
d8691416
...
...
@@ -5,11 +5,11 @@ ohos {
signingConfigs
{
release
{
storeFile
file
(
'D:/HuaweiWatchClient/huawei_watch_overseas.p12'
)
storePassword
'0000001B
37FBF75B69C866355CBEFC208DDF92764DE954F1C754CBD53E1B481433E2DBB6E2C3132D049A89
'
storePassword
'0000001B
FDB36C87CF75CE5C9C35B4EB247F0B973A3CEBFCE636F022CE70CBF7EA5D483CB9896BA93C3146
'
keyAlias
'huawei_watch_overseas Release'
keyPassword
'0000001B
E894FABE766383262AD346CB8151121BA00764539557B67C6917C8C4B031ADCF67E1384F5FE120
'
profile
file
(
'D:
/HuaweiWatchClient/
com.company.fit.provisionRelease.p7b'
)
certpath
file
(
'D:
/HuaweiWatchClient/
com.company.fit.overseas_release.cer'
)
keyPassword
'0000001B
32D22EDA409673EBC987E06A1784506F044E7486B4A2590B645408BBD960C712D03072C22FE304
'
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
'
}
}
...
...
entry/build.gradle
View file @
d8691416
...
...
@@ -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
'0000001B
17F8F53E0CF14D52675968C4FD60AA48E6580B4EEEE4AE8348FA556850E01CC92EEA6DE834CA36
'
keyPassword
'0000001B
329C15F1A79D6DE1A35FC33EAC9D748ED2C9769503449638BCD341685E783C445341B6EF7252A0
'
keyAlias
'huawei_watch_overseas Release'
storePassword
'0000001B
16A4C8B18003852BF6E890B38F6D624C8314D7E55CE2FD87CBC3F2C9E2DA9403C977F0C988ABAE
'
storePassword
'0000001B
ED3DA1157AED6BA705ACB0E5A74DF33AB09917EF841F6048AA1D583907E32B547CE23E3D6415F8
'
storeFile
file
(
'D:/HuaweiWatchClient/huawei_watch_overseas.p12'
)
}
release
{
storeFile
file
(
'D:/HuaweiWatchClient/huawei_watch_overseas.p12'
)
storePassword
'0000001B
76038D1DF78CD8E9F7D8F4E133ECE3FABA895EE4FEE9DDC4225267E3D2176904B14F7BF71AF47D
'
storePassword
'0000001B
E711A291624105B6065E58CF3582A64BD480021F7FA44D831AF7F47D3BAA28EE72E1ED5200671F
'
keyAlias
'huawei_watch_overseas Release'
keyPassword
'0000001B
4BECAFAB52D2A2F0F1D06B36F5343980B0FD548D1BB1A84211DB54738785744DD4DF2640AAD1B5
'
keyPassword
'0000001B
9650C5F04495DB8963B0EA3435C222F7566D397075C0BCA00938DC2AC4A57E46138E9C0F70324C
'
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'
}
entry/src/main/config.json
View file @
d8691416
...
...
@@ -2,8 +2,8 @@
"app"
:
{
"bundleName"
:
"com.company.fit"
,
"version"
:
{
"code"
:
2
0
,
"name"
:
"1.0.
2
"
"code"
:
2
5
,
"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"
:
"
harmony
os.permission.READ_HEALTH_DATA"
"name"
:
"
oh
os.permission.READ_HEALTH_DATA"
}
]
}
...
...
entry/src/main/js/default/liteWearable/wearengine.js
View file @
d8691416
/*
* Copyright (c) 202
0
Huawei Device Co., Ltd.
* Copyright (c) 202
1
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,318 +11,434 @@
* 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
peerPkgName
;
var
peerFingerPrint
;
var
version
;
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
// 文件消息
};
function
P2pClient
(
context
)
{
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
:
"2"
,
});
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
=
" "
;
}
catch
(
error
)
{
jsSdk
.
version
=
0
;
console
.
log
(
"service sdk version is too low"
+
error
.
message
);
var
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
.
info
(
"service sdk version is: "
+
data
);
}
else
{
jsSdk
.
version
=
0
;
console
.
info
(
"get service sdk version failed"
);
}
};
wearengine
.
getWearEngineVersion
({
complete
:
getVersionCallBack
,
sdkVersion
:
WEARENGINE_SDK_VERSION
});
}
catch
(
error
)
{
jsSdk
.
version
=
0
;
console
.
info
(
"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
.
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
<
WEARENGINE_SERVICE_VISION_302
)
{
console
.
info
(
"setPeerFingerPrint interface invalid"
);
var
obj
=
{
data
:
"ERROR_CODE_VERSION_TOO_LOW"
,
code
:
ErrorCode
.
MSG_ERROR_CODE_VERSION_TOO_LOW
};
return
obj
;
}
if
(
isEmpty
(
fingerPrint
))
{
fingerPrint
=
DEFAULT_EMPTY_FINGERPRINT
;
}
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
;
}
/**
* 设置手机应用的packageName
* peerPkgName: string
*/
P2pClient
.
prototype
.
setPeerPkgName
=
function
(
peerPkgName
)
{
this
.
peerPkgName
=
peerPkgName
;
var
failCallBack
=
function
(
data
,
code
)
{
if
(
!
data
&&
!
code
)
{
pingCallback
.
onFailure
();
pingCallback
.
onPingResult
(
notInstallCode
);
}
else
{
pingCallback
.
onFailure
();
pingCallback
.
onPingResult
(
failCode
);
}
console
.
error
(
"ping failed."
);
};
/**
* 设置手机侧指纹信息
* 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
:
208
,
};
return
obj
;
FeatureAbility
.
detect
({
bundleName
:
this
.
peerPkgName
,
success
:
successCallBack
,
fail
:
failCallBack
});
};
/**
* 注册消息监听接口
* receiver:object:onSuccess(),onFailure(),onReceiveMessage(message)
*/
P2pClient
.
prototype
.
registerReceiver
=
function
(
receiver
)
{
if
(
!
receiver
)
{
return
;
}
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
.
isRegister
)
{
receiver
.
onSuccess
();
}
else
if
(
data
.
isFileType
){
var
fileObj
=
{
isFileType
:
true
,
name
:
data
.
file
,
mode
:
""
,
mode2
:
""
};
receiver
.
onReceiveMessage
(
fileObj
);
console
.
info
(
"receive file name:"
+
fileObj
.
name
);
}
else
{
receiver
.
onReceiveMessage
(
data
.
message
);
console
.
info
(
"receive message:"
+
data
.
message
);
}
this
.
peerFingerPrint
=
fingerPrint
;
var
successCallBack
=
function
()
{
console
.
log
(
"fingerPrint setup succeeded"
);
};
var
failCallBack
=
function
()
{
console
.
log
(
"fingerPrint setup failed"
);
};
wearengine
.
setFingerprint
({
appName
:
this
.
peerPkgName
,
appCert
:
fingerPrint
,
complete
:
successCallBack
,
fail
:
failCallBack
,
});
}
else
{
receiver
.
onSuccess
();
}
};
/**
* 检测对端设备侧是否已经安装指定应用
* pingCallback: object:onSuccess(),onFailure(),onPingResult(resultCode)
*/
P2pClient
.
prototype
.
ping
=
function
(
pingCallback
)
{
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
;
}
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_P2P_PHONE_APP_EXIT"
,
code
:
205
,
};
var
successCallBack
=
function
()
{
pingCallback
.
onSuccess
();
pingCallback
.
onPingResult
(
successCode
);
};
var
notInstallCode
=
{
data
:
"ERROR_CODE_P2P_PHONE_APP_NOT_EXIT"
,
code
:
204
,
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
:
"ERROR_CODE_P2P_OTHER_ERROR"
,
code
:
203
,
};
var
failCallBack
=
function
(
data
,
code
)
{
if
(
!
data
&&
!
code
)
{
pingCallback
.
onFailure
();
pingCallback
.
onPingResult
(
notInstallCode
);
}
else
{
pingCallback
.
onFailure
();
pingCallback
.
onPingResult
(
failCode
);
}
data
:
errorMessage
,
code
:
ErrorCode
.
MSG_ERROR_SEND_FAIL
};
FeatureAbility
.
detect
({
bundleName
:
this
.
peerPkgName
,
success
:
successCallBack
,
fail
:
failCallBack
,
});
};
/**
* 注册消息监听接口
* receiver:object:onSuccess(),onFailure(),onReceiveMessage(message)
*/
P2pClient
.
prototype
.
registerReceiver
=
function
(
receiver
)
{
if
(
!
receiver
)
{
return
;
}
var
successCallBack
=
function
(
data
)
{
receiver
.
onSuccess
();
if
(
data
)
{
if
(
data
.
isFileType
)
{
var
fileObj
=
{
isFileType
:
true
,
name
:
data
.
file
,
mode
:
""
,
mode2
:
""
,
};
receiver
.
onReceiveMessage
(
fileObj
);
}
else
{
receiver
.
onReceiveMessage
(
data
.
message
);
}
}
else
{
receiver
.
onReceiveMessage
(
data
);
}
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
<
WEARENGINE_SERVICE_VISION_302
)
{
console
.
info
(
"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
;
}
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
;
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
:
ErrorCode
.
MSG_ERROR_SEND_SUCCESS
};
sendCallback
.
onSuccess
();
sendCallback
.
onSendResult
(
successCode
);
};
if
(
message
.
getType
()
==
0
)
{
var
successCallBack
=
function
()
{
var
successCode
=
{
data
:
"ERROR_CODE_COMM_SUCCESS"
,
code
:
207
,
};
sendCallback
.
onSuccess
();
sendCallback
.
onSendResult
(
successCode
);
sendCallback
.
onSendProgress
(
100
+
"%"
);
};
var
failCallBack
=
function
(
errorMessage
,
code
)
{
var
failCode
=
{
data
:
errorMessage
,
code
:
206
,
};
sendCallback
.
onFailure
();
sendCallback
.
onSendResult
(
failCode
);
sendCallback
.
onSendProgress
(
0
+
"%"
);
};
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
:
208
,
};
sendCallback
.
onFailure
();
sendCallback
.
onSendResult
(
obj
);
return
;
}
var
successCallBack
=
function
()
{
var
successCode
=
{
data
:
"ERROR_CODE_COMM_SUCCESS"
,
code
:
207
,
};
sendCallback
.
onSuccess
();
sendCallback
.
onSendResult
(
successCode
);
};
var
failCallBack
=
function
(
errorMessage
,
code
)
{
var
failCode
=
{
data
:
errorMessage
,
code
:
ErrorCode
.
MSG_ERROR_SEND_FAIL
};
sendCallback
.
onFailure
();
sendCallback
.
onSendResult
(
failCode
);
};
var
progressCallBack
=
function
(
data
)
{
sendCallback
.
onSendProgress
(
data
.
progressNum
+
"%"
);
console
.
info
(
"progress of sending file: "
+
data
.
progressNum
+
"%"
);
};
var
failCallBack
=
function
(
errorMessage
,
code
)
{
var
failCode
=
{
data
:
errorMessage
,
code
:
206
,
};
sendCallback
.
onFailure
();
sendCallback
.
onSendResult
(
failCode
);
};
var
progressCallBack
=
function
(
data
)
{
sendCallback
.
onSendProgress
(
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
();
};
return
P2pClient
;
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
();
};
/**
* 判断参数是否为空
* 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
name
;
var
mode
;
var
mode2
;
var
File
=
(
function
()
{
var
name
;
var
mode
;
var
mode2
;
function
File
()
{}
function
File
()
{}
return
File
;
})();
var
Builder
=
(
function
()
{
var
messageInfo
;
function
Builder
(
context
)
{}
var
Builder
=
(
function
()
{
var
messageInfo
;
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
;
function
Builder
()
{}
Builder
.
prototype
.
setDescription
=
function
(
description
)
{
this
.
messageInfo
=
description
;
this
.
messageType
=
MessageType
.
MESSAGE_TYPE_DATA
;
};
/**
* 设置messge信息(两种格式任选其一)
* data: ArrayBuffer
* data: File(暂时不支持)
*/
Builder
.
prototype
.
setPayload
=
function
(
data
)
{
if
(
!
data
)
{
return
;
}
if
(
typeof
data
==
"object"
&&
data
.
name
)
{
this
.
messageType
=
MessageType
.
MESSAGE_TYPE_FILE
;
return
this
.
setFilePlayload
(
data
);
}
else
{
this
.
messageType
=
MessageType
.
MESSAGE_TYPE_DATA
;
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
;
})();
var
Message
=
(
function
()
{
var
builder
=
new
Builder
();
function
Message
(
context
)
{}
Message
.
prototype
.
describeContents
=
function
()
{
return
this
.
builder
.
messageInfo
;
};
/**
* 获取传送时的信息
*/
Message
.
prototype
.
getData
=
function
()
{
return
this
.
builder
.
messageInfo
;
};
Message
.
prototype
.
getDescription
=
function
()
{
return
this
.
builder
.
messageInfo
;
};
/**
* 获取文件信息
*/
Message
.
prototype
.
getFile
=
function
()
{
if
(
this
.
builder
.
messageType
==
1
)
{
return
JSON
.
parse
(
this
.
builder
.
messageInfo
);
}
return
null
;
};
/**
* 获取传输数据类型
* 0 string
* 1 File
*/
Message
.
prototype
.
getType
=
function
()
{
return
this
.
builder
.
messageType
;
};
return
Message
;
var
Message
=
(
function
()
{
var
builder
=
new
Builder
();
function
Message
()
{}
Message
.
prototype
.
describeContents
=
function
()
{
return
this
.
builder
.
messageInfo
;
};
/**
* 获取传送时的信息
*/
Message
.
prototype
.
getData
=
function
()
{
return
this
.
builder
.
messageInfo
;
};
Message
.
prototype
.
getDescription
=
function
()
{
return
this
.
builder
.
messageInfo
;
};
/**
* 获取文件信息
*/
Message
.
prototype
.
getFile
=
function
()
{
if
(
this
.
builder
.
messageType
==
MessageType
.
MESSAGE_TYPE_FILE
)
{
return
JSON
.
parse
(
this
.
builder
.
messageInfo
);
}
return
null
;
};
/**
* 获取传输数据类型
* 0 string
* 1 File
*/
Message
.
prototype
.
getType
=
function
()
{
return
this
.
builder
.
messageType
;
};
return
Message
;
})();
export
{
P2pClient
,
Message
,
Builder
};
export
{
P2pClient
,
Message
,
Builder
};
entry/src/main/js/default/pages/end/end.js
View file @
d8691416
...
...
@@ -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
()
{
},
...
...
entry/src/main/js/default/pages/index/index.css
View file @
d8691416
...
...
@@ -193,11 +193,12 @@
/**已训练**/
.train-new-title
{
font-size
:
38px
;
height
:
38
px
;
height
:
40
px
;
margin-top
:
60px
;
width
:
350px
;
color
:
#FFFFFF
;
text-align
:
center
;
line-hight
:
40px
;
}
.train-time
{
justify-content
:
center
;
...
...
entry/src/main/js/default/pages/index/index.js
View file @
d8691416
...
...
@@ -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
.
init
Data
();
_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
()
{
init
Data
()
{
this
.
isStop
=
false
;
this
.
sendFlg
=
false
;
this
.
stopFlg
=
false
;
...
...
entry/src/main/js/default/wearable/wearengine.js
View file @
d8691416
/*
* 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"
;
var
P2pClient
;
var
Builder
;
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
);
}
import
file
from
"@system.file"
;
const
injectRef
=
Object
.
getPrototypeOf
(
global
)
||
global
;
injectRef
.
regeneratorRuntime
=
require
(
"@babel/runtime/regenerator"
);
var
FeatureAbilityOption
=
{
BUNDLE_NAME
:
"com.huawei.watch.kit.hiwearability"
,
// 设置AA侧服务应用包名称
ABILITY_NAME
:
"com.huawei.watch.kit.hiwearability.HiWearServiceAbility"
,
// 设置AA侧指定service名称
ABILITY_TYPE
:
0
,
// 设置AA侧指定请求服务类型, abilityType: 0-Ability; 1-Internal Ability
SYNCOPTION_TYPE
:
0
// 设置AA侧请求方式(Ace框架,目前Ability方式,只支持Sync同步方式), 0-Sync; 1-Async
BUNDLE_NAME
:
"com.huawei.watch.kit.hiwearability"
,
// 设置AA侧服务应用包名称
ABILITY_NAME
:
"com.huawei.watch.kit.hiwearability.HiWearServiceAbility"
,
// 设置AA侧指定service名称
ABILITY_TYPE
:
0
,
// 设置AA侧指定请求服务类型, abilityType: 0-Ability; 1-Internal Ability
SYNCOPTION_TYPE
:
0
// 设置AA侧请求方式(Ace框架,目前Ability方式,只支持Sync同步方式), 0-Sync; 1-Async
};
var
RequestCode
=
{
INIT_CODEVALUE
:
1000
,
// 初始化请求码
REGISTER_RECEIVER_CODEVALUE
:
1001
,
// 注册接收请求码
UNREGISTER_RECEIVER_CODEVALUE
:
1002
,
// 取消注册接收请求码
PING_CODEVALUE
:
1003
,
// PING探测请求码
SENDMESSAGE_CODEVALUE
:
1004
,
// 发文本消息请求码
SENDFILE_CODEVALUE
:
1005
,
// 发文件消息请求码
SUBSCRIBE_ABILITY_CODEVALUE
:
1006
,
// 通道消息订阅
UNSUBSCRIBE_ABILITY_CODEVALUE
:
1007
// 通道消息取消订阅
INIT_CODEVALUE
:
1000
,
// 初始化请求码
REGISTER_RECEIVER_CODEVALUE
:
1001
,
// 注册接收请求码
UNREGISTER_RECEIVER_CODEVALUE
:
1002
,
// 取消注册接收请求码
PING_CODEVALUE
:
1003
,
// PING探测请求码
SENDMESSAGE_CODEVALUE
:
1004
,
// 发文本消息请求码
SENDFILE_CODEVALUE
:
1005
,
// 发文件消息请求码
SUBSCRIBE_ABILITY_CODEVALUE
:
1006
,
// 通道消息订阅
UNSUBSCRIBE_ABILITY_CODEVALUE
:
1007
// 通道消息取消订阅
};
var
WearEngineConst
=
{
DELAY_INIT_TIME_VALUE
:
500
,
// 初始化延时默认值
DELAY_TIME_VALUE
:
1000
,
// 消息发送延时默认值
DELAY_FILEAPP_VALUE
:
"internal://app/"
,
// 三方应用默认文件存储路径
DEFAULT_EMPTY_VALUE
:
""
,
// 默认空字符串
MAX_TRANSFER_SIZE
:
1024
,
// 分段发送文件时,每段的大小
FILE_TO_BE_CONTINUED
:
1
// 需要继续发送文件的返回码
DELAY_INIT_TIME_VALUE
:
500
,
// 初始化延时默认值
DELAY_TIME_VALUE
:
1000
,
// 消息发送延时默认值
DELAY_FILEAPP_VALUE
:
"internal://app/"
,
// 三方应用默认文件存储路径
DEFAULT_EMPTY_VALUE
:
""
,
// 默认空字符串
MAX_TRANSFER_SIZE
:
1024
*
50
,
// 分段发送文件时,每段的大小
FILE_TO_BE_CONTINUED
:
1
,
// 需要继续发送文件的返回码
END_FLAG
:
1
// 手机给手表发送文件已经结束的标志
};
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版本太低。
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
// 文件消息
MESSAGE_TYPE_DATA
:
0
,
// 文本消息
MESSAGE_TYPE_FILE
:
1
// 文件消息
};
var
isFunction
=
obj
=>
{
return
typeof
obj
===
"function"
&&
typeof
obj
.
nodeType
!==
"number"
;
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,62 +73,61 @@ if (typeof globalThis == "undefined" || globalThis.isLite) {
var
initSuccess
=
false
;
function
P2pClient
()
{
this
.
setPeerFingerPrint
(
WearEngineConst
.
DEFAULT_EMPTY_VALUE
,
true
);
this
.
subscribeAbilityEvent
();
this
.
subscribeAbilityEvent
();
}
/**
* 建立消息通道
*/
P2pClient
.
prototype
.
subscribeAbilityEvent
=
async
function
()
{
var
action
=
this
.
getRequestHeader
(
RequestCode
.
SUBSCRIBE_ABILITY_CODEVALUE
);
var
that
=
this
;
var
action
=
this
.
getRequestHeader
(
RequestCode
.
SUBSCRIBE_ABILITY_CODEVALUE
);
var
that
=
this
;
await
FeatureAbility
.
subscribeAbilityEvent
(
action
,
function
(
callbackData
)
{
var
callbackJson
=
JSON
.
parse
(
callbackData
);
var
receivedMessage
=
JSON
.
parse
(
JSON
.
stringify
(
callbackJson
.
data
));
await
FeatureAbility
.
subscribeAbilityEvent
(
action
,
function
(
callbackData
)
{
var
callbackJson
=
JSON
.
parse
(
callbackData
);
var
receivedMessage
=
JSON
.
parse
(
JSON
.
stringify
(
callbackJson
.
data
));
// 接收消息或文件
if
(
receivedMessage
.
messageType
!=
"progress"
)
{
var
msgOrFileObj
=
handleReceivedMessage
(
receivedMessage
,
that
);
// 接收消息或文件
if
(
receivedMessage
.
messageType
!=
"progress"
)
{
var
msgOrFileObj
=
handleReceivedMessage
(
receivedMessage
,
that
);
if
(
!
msgOrFileObj
)
{
return
;
}
if
(
messageCallbackMap
[
receivedMessage
.
peerPackageName
])
{
console
.
info
(
"receive message or file name: "
+
(
typeof
msgOrFileObj
==
"object"
?
msgOrFileObj
.
name
:
msgOrFileObj
)
);
var
messageCallback
=
messageCallbackMap
[
receivedMessage
.
peerPackageName
];
messageCallback
(
msgOrFileObj
);
}
if
(
messageCallbackMap2
[
receivedMessage
.
peerPackageName
])
{
var
dataOrFileName
=
typeof
msgOrFileObj
==
"object"
?
msgOrFileObj
.
name
:
msgOrFileObj
;
var
messageType
=
typeof
msgOrFileObj
==
"object"
?
MessageType
.
MESSAGE_TYPE_FILE
:
MessageType
.
MESSAGE_TYPE_DATA
;
console
.
info
(
"receive2 message or file name: "
+
dataOrFileName
);
var
receivedMessageObj
=
new
Message
();
var
receivedMessageObjBuilder
=
new
Builder
();
receivedMessageObjBuilder
.
setReceivedInfo
(
messageType
,
dataOrFileName
);
receivedMessageObj
.
builder
=
receivedMessageObjBuilder
;
var
messageCallback
=
messageCallbackMap2
[
receivedMessage
.
peerPackageName
];
messageCallback
(
receivedMessageObj
);
}
}
else
{
// 接受进度消息
console
.
info
(
"progress of sending file: "
+
JSON
.
stringify
(
receivedMessage
.
abilityResult
)
+
"%"
);
if
(
!
msgOrFileObj
)
{
return
;
}
if
(
progressCallbackMap
[
receivedMessage
.
peerPackageName
])
{
var
progressCallback
=
progressCallbackMap
[
receivedMessage
.
peerPackageName
];
progressCallback
(
JSON
.
stringify
(
receivedMessage
.
abilityResult
));
}
}
});
if
(
messageCallbackMap
[
receivedMessage
.
peerPackageName
])
{
console
.
info
(
"receive message or file name: "
+
(
typeof
msgOrFileObj
==
"object"
?
msgOrFileObj
.
name
:
msgOrFileObj
)
);
var
messageCallback
=
messageCallbackMap
[
receivedMessage
.
peerPackageName
];
messageCallback
(
msgOrFileObj
);
}
if
(
messageCallbackMap2
[
receivedMessage
.
peerPackageName
])
{
var
dataOrFileName
=
typeof
msgOrFileObj
==
"object"
?
msgOrFileObj
.
name
:
msgOrFileObj
;
var
messageType
=
typeof
msgOrFileObj
==
"object"
?
MessageType
.
MESSAGE_TYPE_FILE
:
MessageType
.
MESSAGE_TYPE_DATA
;
console
.
info
(
"receive2 message or file name: "
+
dataOrFileName
);
var
receivedMessageObj
=
new
Message
();
var
receivedMessageObjBuilder
=
new
Builder
();
receivedMessageObjBuilder
.
setReceivedInfo
(
messageType
,
dataOrFileName
);
receivedMessageObj
.
builder
=
receivedMessageObjBuilder
;
var
messageCallback
=
messageCallbackMap2
[
receivedMessage
.
peerPackageName
];
messageCallback
(
receivedMessageObj
);
}
}
else
{
// 接受进度消息
console
.
info
(
"progress of sending file: "
+
JSON
.
stringify
(
receivedMessage
.
abilityResult
)
+
"%"
);
if
(
progressCallbackMap
[
receivedMessage
.
peerPackageName
])
{
var
progressCallback
=
progressCallbackMap
[
receivedMessage
.
peerPackageName
];
progressCallback
(
JSON
.
stringify
(
receivedMessage
.
abilityResult
));
}
}
});
};
/**
...
...
@@ -451,10 +136,7 @@ if (typeof globalThis == "undefined" || globalThis.isLite) {
* @param peerPkgName 目标应用包名称
*/
P2pClient
.
prototype
.
setPeerPkgName
=
async
function
(
peerPkgName
)
{
if
(
!
peerPkgName
)
{
return
;
}
this
.
peerPkgName
=
peerPkgName
;
this
.
peerPkgName
=
peerPkgName
;
};
/**
...
...
@@ -462,50 +144,29 @@ 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
.
peerF
ingerPrint
dstPackageName
:
this
.
peerPkgName
,
dstFingerPrint
:
f
ingerPrint
};
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
=>
{
FeatureAbility
.
callAbility
(
action
).
then
(
resultStr
=>
{
var
resultObj
=
JSON
.
parse
(
resultStr
);
if
(
resultObj
.
abilityResult
==
1
)
{
console
.
info
(
"setPeerFingerPrint success."
);
console
.
info
(
"setPeerFingerPrint success."
);
}
else
{
console
.
error
(
"setPeerFingerPrint failed."
);
console
.
error
(
"setPeerFingerPrint failed."
);
}
},
()
=>
{
},
()
=>
{
console
.
error
(
"setPeerFingerPrint failed."
);
}
);
},
WearEngineConst
.
DELAY_INIT_TIME_VALUE
);
}
},
WearEngineConst
.
DELAY_INIT_TIME_VALUE
);
}
);
};
/**
...
...
@@ -514,50 +175,50 @@ if (typeof globalThis == "undefined" || globalThis.isLite) {
* @param pingCallback object:onSuccess(),onFailure(),onPingResult(resultCode)
*/
P2pClient
.
prototype
.
ping
=
async
function
(
pingCallback
)
{
var
successCode
=
{
data
:
"ERROR_CODE_P2P_PHONE_APP_EXIST"
,
code
:
ErrorCode
.
MSG_ERROR_PING_PHONE_APP_NOT_RUNNING
};
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
};
var
successCallBack
=
function
()
{
isFunction
(
pingCallback
.
onSuccess
)
&&
pingCallback
.
onSuccess
();
isFunction
(
pingCallback
.
onPingResult
)
&&
pingCallback
.
onPingResult
(
successCode
);
};
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
);
var
successCode
=
{
data
:
"ERROR_CODE_P2P_PHONE_APP_EXIST"
,
code
:
ErrorCode
.
MSG_ERROR_PING_PHONE_APP_NOT_RUNNING
};
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
};
var
successCallBack
=
function
()
{
isFunction
(
pingCallback
.
onSuccess
)
&&
pingCallback
.
onSuccess
();
isFunction
(
pingCallback
.
onPingResult
)
&&
pingCallback
.
onPingResult
(
successCode
);
};
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
);
}
else
{
isFunction
(
pingCallback
.
onFailure
)
&&
pingCallback
.
onFailure
();
isFunction
(
pingCallback
.
onPingResult
)
&&
pingCallback
.
onPingResult
(
failCode
);
}
};
var
actionData
=
{
dstPackageName
:
this
.
peerPkgName
,
dstFingerPrint
:
this
.
peerFingerPrint
};
var
action
=
this
.
getRequestHeader
(
RequestCode
.
PING_CODEVALUE
,
actionData
);
var
resultStr
=
await
FeatureAbility
.
callAbility
(
action
);
var
resultObj
=
JSON
.
parse
(
resultStr
);
if
(
resultObj
.
abilityResult
==
ErrorCode
.
MSG_ERROR_PING_PHONE_APP_NOT_RUNNING
)
{
successCallBack
();
console
.
info
(
"ping success."
);
}
else
{
isFunction
(
pingCallback
.
onFailure
)
&&
pingCallback
.
onFailure
(
);
isFunction
(
pingCallback
.
onPingResult
)
&&
pingCallback
.
onPingResult
(
failCode
);
failCallBack
(
resultObj
.
abilityResult
);
console
.
error
(
"ping failed."
);
}
};
var
actionData
=
{
dstPackageName
:
this
.
peerPkgName
,
dstFingerPrint
:
this
.
peerFingerPrint
};
var
action
=
this
.
getRequestHeader
(
RequestCode
.
PING_CODEVALUE
,
actionData
);
var
resultStr
=
await
FeatureAbility
.
callAbility
(
action
);
var
resultObj
=
JSON
.
parse
(
resultStr
);
if
(
resultObj
.
abilityResult
==
ErrorCode
.
MSG_ERROR_PING_PHONE_APP_NOT_RUNNING
)
{
successCallBack
();
console
.
info
(
"ping success."
);
}
else
{
failCallBack
(
resultObj
.
abilityResult
);
console
.
error
(
"ping failed."
);
}
};
/**
...
...
@@ -566,36 +227,36 @@ if (typeof globalThis == "undefined" || globalThis.isLite) {
* @param receiver object:onSuccess(),onFailure(),onReceiveMessage(message)
*/
P2pClient
.
prototype
.
registerReceiver
=
async
function
(
receiver
)
{
if
(
!
receiver
)
{
return
;
}
if
(
!
receiver
)
{
return
;
}
var
successCallBack
=
function
()
{
isFunction
(
receiver
.
onSuccess
)
&&
receiver
.
onSuccess
();
};
var
successCallBack
=
function
()
{
isFunction
(
receiver
.
onSuccess
)
&&
receiver
.
onSuccess
();
};
var
failCallBack
=
function
()
{
isFunction
(
receiver
.
onFailure
)
&&
receiver
.
onFailure
();
};
var
failCallBack
=
function
()
{
isFunction
(
receiver
.
onFailure
)
&&
receiver
.
onFailure
();
};
var
messageCallBack
=
function
(
data
)
{
isFunction
(
receiver
.
onReceiveMessage
)
&&
receiver
.
onReceiveMessage
(
data
);
};
var
messageCallBack
=
function
(
data
)
{
isFunction
(
receiver
.
onReceiveMessage
)
&&
receiver
.
onReceiveMessage
(
data
);
};
messageCallbackMap
[
this
.
peerPkgName
]
=
messageCallBack
;
messageCallbackMap
[
this
.
peerPkgName
]
=
messageCallBack
;
var
action
=
this
.
getRequestHeader
(
RequestCode
.
REGISTER_RECEIVER_CODEVALUE
);
var
action
=
this
.
getRequestHeader
(
RequestCode
.
REGISTER_RECEIVER_CODEVALUE
);
var
resultStr
=
await
FeatureAbility
.
callAbility
(
action
);
var
resultObj
=
JSON
.
parse
(
resultStr
);
var
resultStr
=
await
FeatureAbility
.
callAbility
(
action
);
var
resultObj
=
JSON
.
parse
(
resultStr
);
if
(
resultObj
.
abilityResult
==
1
)
{
successCallBack
();
console
.
info
(
"registerReceiver success."
);
}
else
{
failCallBack
();
console
.
error
(
"registerReceiver failed."
);
}
if
(
resultObj
.
abilityResult
==
1
)
{
successCallBack
();
console
.
info
(
"registerReceiver success."
);
}
else
{
failCallBack
();
console
.
error
(
"registerReceiver failed."
);
}
};
/**
...
...
@@ -604,36 +265,36 @@ if (typeof globalThis == "undefined" || globalThis.isLite) {
* @param receiver object:onSuccess(),onFailure(),onReceiveMessage(message)
*/
P2pClient
.
prototype
.
registerReceiver2
=
async
function
(
receiver
)
{
if
(
!
receiver
)
{
return
;
}
if
(
!
receiver
)
{
return
;
}
var
successCallBack
=
function
()
{
isFunction
(
receiver
.
onSuccess
)
&&
receiver
.
onSuccess
();
};
var
successCallBack
=
function
()
{
isFunction
(
receiver
.
onSuccess
)
&&
receiver
.
onSuccess
();
};
var
failCallBack
=
function
()
{
isFunction
(
receiver
.
onFailure
)
&&
receiver
.
onFailure
();
};
var
failCallBack
=
function
()
{
isFunction
(
receiver
.
onFailure
)
&&
receiver
.
onFailure
();
};
var
messageCallBack
=
function
(
data
)
{
isFunction
(
receiver
.
onReceiveMessage
)
&&
receiver
.
onReceiveMessage
(
data
);
};
var
messageCallBack
=
function
(
data
)
{
isFunction
(
receiver
.
onReceiveMessage
)
&&
receiver
.
onReceiveMessage
(
data
);
};
messageCallbackMap2
[
this
.
peerPkgName
]
=
messageCallBack
;
messageCallbackMap2
[
this
.
peerPkgName
]
=
messageCallBack
;
var
action
=
this
.
getRequestHeader
(
RequestCode
.
REGISTER_RECEIVER_CODEVALUE
);
var
action
=
this
.
getRequestHeader
(
RequestCode
.
REGISTER_RECEIVER_CODEVALUE
);
var
resultStr
=
await
FeatureAbility
.
callAbility
(
action
);
var
resultObj
=
JSON
.
parse
(
resultStr
);
var
resultStr
=
await
FeatureAbility
.
callAbility
(
action
);
var
resultObj
=
JSON
.
parse
(
resultStr
);
if
(
resultObj
.
abilityResult
==
1
)
{
successCallBack
();
console
.
info
(
"registerReceiver2 success."
);
}
else
{
failCallBack
();
console
.
error
(
"registerReceiver2 failed."
);
}
if
(
resultObj
.
abilityResult
==
1
)
{
successCallBack
();
console
.
info
(
"registerReceiver2 success."
);
}
else
{
failCallBack
();
console
.
error
(
"registerReceiver2 failed."
);
}
};
/**
...
...
@@ -643,44 +304,44 @@ if (typeof globalThis == "undefined" || globalThis.isLite) {
* @param message 返回结果
*/
function
handleReceivedMessage
(
message
,
that
)
{
if
(
!
message
)
{
return
;
}
if
(
message
.
messageType
==
"text"
)
{
return
message
.
abilityResult
;
}
//返回文件内容,并保存至本地
if
(
message
.
messageType
==
"file"
)
{
var
fileName
=
message
.
fileName
;
that
.
allMessage
=
that
.
allMessage
==
undefined
?
message
.
abilityResult
:
that
.
allMessage
+
message
.
abilityResult
;
if
(
message
.
seqId
&&
message
.
seqId
!=
message
.
cnt
)
{
return
;
if
(
!
message
)
{
return
;
}
file
.
delete
({
uri
:
WearEngineConst
.
DELAY_FILEAPP_VALUE
+
fileName
,
success
:
function
()
{
writeFile
(
fileName
,
that
.
allMessage
);
that
.
allMessage
=
""
;
},
fail
:
function
(
data
,
code
)
{
writeFile
(
fileName
,
that
.
allMessage
);
that
.
allMessage
=
""
;
}
});
if
(
message
.
messageType
==
"text"
)
{
return
message
.
abilityResult
;
}
var
fileObj
=
{
isFileType
:
true
,
name
:
WearEngineConst
.
DELAY_FILEAPP_VALUE
+
fileName
,
mode
:
""
,
mode2
:
""
};
return
fileObj
;
}
//返回文件内容,并保存至本地
if
(
message
.
messageType
==
"file"
)
{
var
fileName
=
message
.
fileName
;
that
.
allMessage
=
that
.
allMessage
==
undefined
?
message
.
abilityResult
:
that
.
allMessage
+
message
.
abilityResult
;
if
(
message
.
end
!=
WearEngineConst
.
END_FLAG
)
{
return
;
}
file
.
delete
({
uri
:
WearEngineConst
.
DELAY_FILEAPP_VALUE
+
fileName
,
success
:
function
()
{
writeFile
(
fileName
,
that
.
allMessage
);
that
.
allMessage
=
""
;
},
fail
:
function
(
data
,
code
)
{
writeFile
(
fileName
,
that
.
allMessage
);
that
.
allMessage
=
""
;
}
});
var
fileObj
=
{
isFileType
:
true
,
name
:
WearEngineConst
.
DELAY_FILEAPP_VALUE
+
fileName
,
mode
:
""
,
mode2
:
""
};
return
fileObj
;
}
}
/**
...
...
@@ -689,16 +350,16 @@ if (typeof globalThis == "undefined" || globalThis.isLite) {
* @param {*} fileContent 文件内容
*/
function
writeFile
(
fileName
,
fileContent
)
{
file
.
writeArrayBuffer
({
uri
:
WearEngineConst
.
DELAY_FILEAPP_VALUE
+
fileName
,
buffer
:
fileContent
,
success
:
function
()
{
console
.
info
(
"write Text to file success."
);
},
fail
:
function
(
data
,
code
)
{
console
.
error
(
"write Text to file failed. code: "
+
code
+
", data: "
+
data
);
}
});
file
.
writeArrayBuffer
({
uri
:
WearEngineConst
.
DELAY_FILEAPP_VALUE
+
fileName
,
buffer
:
fileContent
,
success
:
function
()
{
console
.
info
(
"write Text to file success."
);
},
fail
:
function
(
data
,
code
)
{
console
.
error
(
"write Text to file failed. code: "
+
code
+
", data: "
+
data
);
}
});
}
/**
* 发送消息接口
...
...
@@ -707,117 +368,147 @@ if (typeof globalThis == "undefined" || globalThis.isLite) {
* @param sendCallback object:onSuccess(),onFailure(),onSendResult(resultCode),onSendProgress(count)
*/
P2pClient
.
prototype
.
send
=
async
function
(
message
,
sendCallback
)
{
if
(
!
message
||
!
sendCallback
)
{
return
;
}
var
successCallBack
=
function
()
{
var
successCode
=
{
data
:
"ERROR_CODE_COMM_SUCCESS"
,
code
:
ErrorCode
.
MSG_ERROR_SEND_SUCCESS
};
if
(
!
message
||
!
sendCallback
)
{
return
;
}
isFunction
(
sendCallback
.
onSuccess
)
&&
sendCallback
.
onSuccess
();
isFunction
(
sendCallback
.
onSendResult
)
&&
sendCallback
.
onSendResult
(
successCode
);
isFunction
(
sendCallback
.
onSendProgress
)
&&
sendCallback
.
onSendProgress
(
"100%"
);
};
var
successCallBack
=
function
()
{
var
successCode
=
{
data
:
"ERROR_CODE_COMM_SUCCESS"
,
code
:
ErrorCode
.
MSG_ERROR_SEND_SUCCESS
};
var
failCallBack
=
function
()
{
var
failCode
=
{
data
:
"ERROR_CODE_COMM_FAILED"
,
code
:
ErrorCode
.
MSG_ERROR_SEND_FAIL
isFunction
(
sendCallback
.
onSuccess
)
&&
sendCallback
.
onSuccess
();
isFunction
(
sendCallback
.
onSendResult
)
&&
sendCallback
.
onSendResult
(
successCode
);
isFunction
(
sendCallback
.
onSendProgress
)
&&
sendCallback
.
onSendProgress
(
"100%"
);
};
isFunction
(
sendCallback
.
onFailure
)
&&
sendCallback
.
onFailure
();
isFunction
(
sendCallback
.
onSendResult
)
&&
sendCallback
.
onSendResult
(
failCode
);
isFunction
(
sendCallback
.
onSendProgress
)
&&
sendCallback
.
onSendProgress
(
"0%"
);
};
var
progressCallBack
=
function
(
data
)
{
isFunction
(
sendCallback
.
onSendProgress
)
&&
sendCallback
.
onSendProgress
(
data
+
"%"
);
};
var
failCallBack
=
function
()
{
var
failCode
=
{
data
:
"ERROR_CODE_COMM_FAILED"
,
code
:
ErrorCode
.
MSG_ERROR_SEND_FAIL
};
// 0 代表文本消息类型
if
(
message
.
getType
()
==
MessageType
.
MESSAGE_TYPE_DATA
)
{
var
actionData
=
{
messageContent
:
message
.
getData
()
isFunction
(
sendCallback
.
onFailure
)
&&
sendCallback
.
onFailure
();
isFunction
(
sendCallback
.
onSendResult
)
&&
sendCallback
.
onSendResult
(
failCode
);
isFunction
(
sendCallback
.
onSendProgress
)
&&
sendCallback
.
onSendProgress
(
"0%"
);
};
var
action
=
this
.
getRequestHeader
(
RequestCode
.
SENDMESSAGE_CODEVALUE
,
actionData
);
var
resultStr
=
await
FeatureAbility
.
callAbility
(
action
);
var
resultObj
=
JSON
.
parse
(
resultStr
)
;
var
progressCallBack
=
function
(
data
)
{
isFunction
(
sendCallback
.
onSendProgress
)
&&
sendCallback
.
onSendProgress
(
data
+
"%"
);
}
;
if
(
resultObj
.
abilityResult
==
ErrorCode
.
MSG_ERROR_SEND_SUCCESS
)
{
successCallBack
();
console
.
info
(
"send message success."
);
}
else
{
failCallBack
();
console
.
error
(
"send message failed."
);
}
}
else
{
var
sendFileInterval
=
globalThis
.
setInterval
(()
=>
{
if
(
message
.
getFile
()
&&
message
.
getFile
().
errorCode
)
{
globalThis
.
clearInterval
(
sendFileInterval
);
failCallBack
();
console
.
error
(
"send file failed."
);
return
;
}
// 0 代表文本消息类型
if
(
message
.
getType
()
==
MessageType
.
MESSAGE_TYPE_DATA
)
{
var
actionData
=
{
messageContent
:
message
.
getData
()
};
if
(
!
message
.
getFile
()
||
!
message
.
getFile
().
fileName
)
{
return
;
}
globalThis
.
clearInterval
(
sendFileInterval
);
var
fileContent
=
message
.
getFile
().
fileContent
;
var
cnt
=
Math
.
floor
(
fileContent
.
length
/
WearEngineConst
.
MAX_TRANSFER_SIZE
)
+
1
;
var
action
=
this
.
getRequestHeader
(
RequestCode
.
SENDMESSAGE_CODEVALUE
,
actionData
);
var
resultStr
=
await
FeatureAbility
.
callAbility
(
action
);
var
resultObj
=
JSON
.
parse
(
resultStr
);
sendFilePart
(
0
,
cnt
,
fileContent
,
message
.
getFile
().
fileName
,
successCallBack
,
failCallBack
,
this
);
if
(
resultObj
.
abilityResult
==
ErrorCode
.
MSG_ERROR_SEND_SUCCESS
)
{
successCallBack
();
console
.
info
(
"send message success."
);
}
else
{
failCallBack
();
console
.
error
(
"send message failed."
);
}
}
else
{
var
sendFileInterval
=
globalThis
.
setInterval
(()
=>
{
if
(
message
.
getFile
()
&&
message
.
getFile
().
errorCode
)
{
globalThis
.
clearInterval
(
sendFileInterval
);
failCallBack
();
console
.
error
(
"send file failed."
);
return
;
}
progressCallbackMap
[
this
.
peerPkgName
]
=
progressCallBack
;
},
WearEngineConst
.
DELAY_TIME_VALUE
);
}
if
(
!
message
.
getFile
()
||
!
message
.
getFile
().
fileName
)
{
return
;
}
globalThis
.
clearInterval
(
sendFileInterval
);
var
fileContent
=
message
.
getFile
().
fileContent
;
var
cnt
=
Math
.
floor
(
fileContent
.
length
/
WearEngineConst
.
MAX_TRANSFER_SIZE
)
+
1
;
sendFilePart
(
0
,
fileContent
,
message
.
getFile
().
fileName
,
successCallBack
,
failCallBack
,
0
,
WearEngineConst
.
MAX_TRANSFER_SIZE
,
this
);
progressCallbackMap
[
this
.
peerPkgName
]
=
progressCallBack
;
},
WearEngineConst
.
DELAY_TIME_VALUE
);
}
};
/**
* 递归分段发送文件
* @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
)
{
var
actionData
=
{
messageContent
:
fileContent
.
substring
(
WearEngineConst
.
MAX_TRANSFER_SIZE
*
index
,
Math
.
min
(
WearEngineConst
.
MAX_TRANSFER_SIZE
*
(
index
+
1
),
fileContent
.
length
)
),
seqId
:
index
+
1
,
fileName
,
cnt
};
var
action
=
that
.
getRequestHeader
(
RequestCode
.
SENDFILE_CODEVALUE
,
actionData
);
FeatureAbility
.
callAbility
(
action
).
then
(
resultStr
=>
{
var
resultObj
=
JSON
.
parse
(
resultStr
);
if
(
resultObj
.
abilityResult
==
WearEngineConst
.
FILE_TO_BE_CONTINUED
)
{
sendFilePart
(
index
+
1
,
cnt
,
fileContent
,
fileName
,
successCallBack
,
failCallBack
,
that
);
}
else
if
(
resultObj
.
abilityResult
!=
ErrorCode
.
MSG_ERROR_SEND_SUCCESS
)
{
console
.
error
(
"error:"
+
resultStr
);
failCallBack
();
console
.
error
(
"send file failed."
);
}
else
if
(
index
==
cnt
-
1
)
{
successCallBack
();
console
.
info
(
"send file success."
);
}
},
()
=>
{
failCallBack
();
console
.
error
(
"send file failed."
);
}
);
function
sendFilePart
(
index
,
fileContent
,
fileName
,
successCallBack
,
failCallBack
,
hasSent
,
transferSize
,
that
)
{
var
actionData
=
{
messageContent
:
fileContent
.
substring
(
hasSent
,
Math
.
min
(
hasSent
+
transferSize
,
fileContent
.
length
)),
seqId
:
hasSent
,
fileName
,
cnt
:
fileContent
.
length
};
var
action
=
that
.
getRequestHeader
(
RequestCode
.
SENDFILE_CODEVALUE
,
actionData
);
FeatureAbility
.
callAbility
(
action
).
then
(
resultStr
=>
{
var
resultObj
=
JSON
.
parse
(
resultStr
);
if
(
resultObj
.
abilityResult
==
WearEngineConst
.
FILE_TO_BE_CONTINUED
)
{
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
(
hasSent
==
fileContent
.
length
)
{
successCallBack
();
console
.
info
(
"send file success."
);
}
},
()
=>
{
failCallBack
();
console
.
error
(
"send file failed."
);
}
);
}
/**
...
...
@@ -826,17 +517,17 @@ if (typeof globalThis == "undefined" || globalThis.isLite) {
* @param receiver onSuccess() onFailure()
*/
P2pClient
.
prototype
.
unregisterReceiver
=
async
function
(
receiver
)
{
var
action
=
this
.
getRequestHeader
(
RequestCode
.
UNREGISTER_RECEIVER_CODEVALUE
);
var
resultStr
=
await
FeatureAbility
.
callAbility
(
action
);
var
resultObj
=
JSON
.
parse
(
resultStr
);
if
(
resultObj
.
abilityResult
==
1
)
{
isFunction
(
receiver
.
onSuccess
)
&&
receiver
.
onSuccess
();
console
.
info
(
"unregisterReceiver success."
);
}
else
{
isFunction
(
receiver
.
onFailure
)
&&
receiver
.
onFailure
();
console
.
error
(
"unregisterReceiver failed."
);
}
var
action
=
this
.
getRequestHeader
(
RequestCode
.
UNREGISTER_RECEIVER_CODEVALUE
);
var
resultStr
=
await
FeatureAbility
.
callAbility
(
action
);
var
resultObj
=
JSON
.
parse
(
resultStr
);
if
(
resultObj
.
abilityResult
==
1
)
{
isFunction
(
receiver
.
onSuccess
)
&&
receiver
.
onSuccess
();
console
.
info
(
"unregisterReceiver success."
);
}
else
{
isFunction
(
receiver
.
onFailure
)
&&
receiver
.
onFailure
();
console
.
error
(
"unregisterReceiver failed."
);
}
};
/**
...
...
@@ -846,32 +537,32 @@ if (typeof globalThis == "undefined" || globalThis.isLite) {
* @param actionData 请求参数
* @return action 对象
*/
P2pClient
.
prototype
.
getRequestHeader
=
function
(
code
,
actionData
)
{
var
destInfo
=
{
dstPackageName
:
this
.
peerPkgName
,
dstFingerPrint
:
this
.
peerFingerPrint
};
return
{
bundleName
:
FeatureAbilityOption
.
BUNDLE_NAME
,
abilityName
:
FeatureAbilityOption
.
ABILITY_NAME
,
abilityType
:
FeatureAbilityOption
.
ABILITY_TYPE
,
syncOption
:
FeatureAbilityOption
.
SYNCOPTION_TYPE
,
messageCode
:
code
,
data
:
Object
.
assign
({},
destInfo
,
actionData
)
};
P2pClient
.
prototype
.
getRequestHeader
=
function
(
code
,
actionData
)
{
var
destInfo
=
{
dstPackageName
:
this
.
peerPkgName
,
dstFingerPrint
:
this
.
peerFingerPrint
};
return
{
bundleName
:
FeatureAbilityOption
.
BUNDLE_NAME
,
abilityName
:
FeatureAbilityOption
.
ABILITY_NAME
,
abilityType
:
FeatureAbilityOption
.
ABILITY_TYPE
,
syncOption
:
FeatureAbilityOption
.
SYNCOPTION_TYPE
,
messageCode
:
code
,
data
:
Object
.
assign
({},
destInfo
,
actionData
)
};
};
return
P2pClient
;
})();
Builder
=
(
function
()
{
})();
var
Builder
=
(
function
()
{
var
messageInfo
;
function
Builder
()
{}
function
Builder
()
{
}
Builder
.
prototype
.
setDescription
=
function
(
description
)
{
this
.
messageInfo
=
description
;
this
.
messageType
=
MessageType
.
MESSAGE_TYPE_DATA
;
Builder
.
prototype
.
setDescription
=
function
(
description
)
{
this
.
messageInfo
=
description
;
this
.
messageType
=
MessageType
.
MESSAGE_TYPE_DATA
;
};
/**
...
...
@@ -879,59 +570,59 @@ if (typeof globalThis == "undefined" || globalThis.isLite) {
*
* @param data ArrayBuffer File(暂时不支持)
*/
Builder
.
prototype
.
setPayload
=
function
(
data
)
{
if
(
!
data
)
{
return
;
}
if
(
typeof
data
==
"object"
&&
data
.
name
)
{
this
.
messageType
=
MessageType
.
MESSAGE_TYPE_FILE
;
return
this
.
setFilePlayload
(
data
);
}
else
{
this
.
messageType
=
MessageType
.
MESSAGE_TYPE_DATA
;
return
this
.
setBufferPlayload
(
data
);
}
Builder
.
prototype
.
setPayload
=
function
(
data
)
{
if
(
!
data
)
{
return
;
}
if
(
typeof
data
==
"object"
&&
data
.
name
)
{
this
.
messageType
=
MessageType
.
MESSAGE_TYPE_FILE
;
return
this
.
setFilePlayload
(
data
);
}
else
{
this
.
messageType
=
MessageType
.
MESSAGE_TYPE_DATA
;
return
this
.
setBufferPlayload
(
data
);
}
};
Builder
.
prototype
.
setBufferPlayload
=
function
(
data
)
{
this
.
messageInfo
=
String
.
fromCharCode
.
apply
(
null
,
new
Uint16Array
(
data
));
Builder
.
prototype
.
setBufferPlayload
=
function
(
data
)
{
this
.
messageInfo
=
String
.
fromCharCode
.
apply
(
null
,
new
Uint16Array
(
data
));
};
Builder
.
prototype
.
setFilePlayload
=
function
(
data
)
{
var
that
=
this
;
if
(
isEmpty
(
data
))
{
return
;
}
var
fileInfoArray
=
data
.
name
.
split
(
"/"
);
var
fileName
=
fileInfoArray
[
fileInfoArray
.
length
-
1
];
if
(
isEmpty
(
fileName
))
{
return
;
}
file
.
readArrayBuffer
({
uri
:
data
.
name
,
success
:
function
(
data
)
{
var
fileInfo
=
{
fileContent
:
data
.
buffer
,
fileName
:
fileName
};
that
.
messageInfo
=
fileInfo
;
console
.
info
(
"read text from file success."
);
},
fail
:
function
(
data
,
code
)
{
var
errorInfo
=
{
errorCode
:
code
,
data
};
that
.
messageInfo
=
errorInfo
;
console
.
info
(
"read text from file failed, code: "
+
code
+
", data: "
+
data
);
Builder
.
prototype
.
setFilePlayload
=
function
(
data
)
{
var
that
=
this
;
if
(
isEmpty
(
data
))
{
return
;
}
var
fileInfoArray
=
data
.
name
.
split
(
"/"
);
var
fileName
=
fileInfoArray
[
fileInfoArray
.
length
-
1
];
if
(
isEmpty
(
fileName
))
{
return
;
}
});
file
.
readArrayBuffer
({
uri
:
data
.
name
,
success
:
function
(
data
)
{
var
fileInfo
=
{
fileContent
:
data
.
buffer
,
fileName
:
fileName
};
that
.
messageInfo
=
fileInfo
;
console
.
info
(
"read text from file success."
);
},
fail
:
function
(
data
,
code
)
{
var
errorInfo
=
{
errorCode
:
code
,
data
};
that
.
messageInfo
=
errorInfo
;
console
.
info
(
"read text from file failed, code: "
+
code
+
", data: "
+
data
);
}
});
};
Builder
.
prototype
.
setReceivedInfo
=
function
(
type
,
data
)
{
this
.
messageType
=
type
;
this
.
messageInfo
=
data
;
Builder
.
prototype
.
setReceivedInfo
=
function
(
type
,
data
)
{
this
.
messageType
=
type
;
this
.
messageInfo
=
data
;
};
/**
...
...
@@ -940,12 +631,12 @@ if (typeof globalThis == "undefined" || globalThis.isLite) {
* @param obj obj parameter
*/
function
isEmpty
(
obj
)
{
return
typeof
obj
==
"undefined"
||
obj
==
null
||
obj
==
""
||
obj
==
" "
;
return
typeof
obj
==
"undefined"
||
obj
==
null
||
obj
==
""
||
obj
==
" "
;
}
return
Builder
;
})();
}
})();
/**
* 文件格式类型
...
...
@@ -953,48 +644,50 @@ if (typeof globalThis == "undefined" || globalThis.isLite) {
* mode: 'text' or 'binary'
* mode2: 'R', 'W', 'RW'
*/
var
File
=
(
function
()
{
var
name
;
var
mode
;
var
mode2
;
var
File
=
(
function
()
{
var
name
;
var
mode
;
var
mode2
;
function
File
()
{}
function
File
()
{
}
return
File
;
return
File
;
})();
var
Message
=
(
function
()
{
var
builder
=
new
Builder
();
function
Message
()
{}
Message
.
prototype
.
describeContents
=
function
()
{
return
this
.
builder
.
messageInfo
;
};
/**
* 获取传送时的信息
*/
Message
.
prototype
.
getData
=
function
()
{
return
this
.
builder
.
messageInfo
;
};
Message
.
prototype
.
getDescription
=
function
()
{
return
this
.
builder
.
messageInfo
;
};
/**
* 获取文件信息
*/
Message
.
prototype
.
getFile
=
function
()
{
if
(
this
.
builder
.
messageType
==
MessageType
.
MESSAGE_TYPE_FILE
)
{
return
globalThis
.
isLite
?
JSON
.
parse
(
this
.
builder
.
messageInfo
)
:
this
.
builder
.
messageInfo
;
var
Message
=
(
function
()
{
var
builder
=
new
Builder
();
function
Message
()
{
}
return
null
;
};
/**
* 获取传输数据类型 0 string,1 File
*/
Message
.
prototype
.
getType
=
function
()
{
return
this
.
builder
.
messageType
;
};
return
Message
;
Message
.
prototype
.
describeContents
=
function
()
{
return
this
.
builder
.
messageInfo
;
};
/**
* 获取传送时的信息
*/
Message
.
prototype
.
getData
=
function
()
{
return
this
.
builder
.
messageInfo
;
};
Message
.
prototype
.
getDescription
=
function
()
{
return
this
.
builder
.
messageInfo
;
};
/**
* 获取文件信息
*/
Message
.
prototype
.
getFile
=
function
()
{
if
(
this
.
builder
.
messageType
==
MessageType
.
MESSAGE_TYPE_FILE
)
{
return
this
.
builder
.
messageInfo
;
}
return
null
;
};
/**
* 获取传输数据类型 0 string,1 File
*/
Message
.
prototype
.
getType
=
function
()
{
return
this
.
builder
.
messageType
;
};
return
Message
;
})();
export
{
P2pClient
,
Message
,
Builder
};
export
{
P2pClient
,
Message
,
Builder
};
entry/src/main/resources/base/element/string.json
0 → 100644
View file @
d8691416
{
"string"
:
[
{
"name"
:
"app_name"
,
"value"
:
"Fit健身"
}
]
}
\ No newline at end of file
yucong0322.p7b
deleted
100644 → 0
View file @
7ffe7ac4
File deleted
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment