Intelligent Assistant
Chat with our virtual assistant to get answers promptly.
This API is used to establish the communications channel between your phone app and the wearable app, to receive and send messages and files customized by your app.
Before sending a P2P message, you can use the ping method to check whether your wearable app can communicate with the phone app.
- // Step 1: Create a P2P communication object.
- var p2pClient = new P2pClient();
- var peerPkgName = "com.*.*";
-
- // Step 2: Set the package name of your phone app to be communicated with.
- p2pClient.setPeerPkgName(peerPkgName);
-
- // Step 3: Check whether your app has been installed on the phone.
- p2pClient.ping({
- onSuccess: function() {
- console.log('ping success.');
- },
- onFailure: function() {
- console.log('ping failed');
- },
- onPingResult: function(resultCode) {
- console.log(resultCode.data + resultCode.code);
- },
- });
The maximum size of a message is 1 KB. If the size of a message exceeds the limit, the message can be divided into several packets or files (with each no larger than 4 MB) can be sent instead.
If the wearable app is able to send messages and files, the phone app must support receiving these messages and files. For details, see Receiving P2P messages or files sent by your wearable app on the phone.
- // Set metaData of your phone app in the module of the config.json file.
- "metaData": {
- "customizeData": [
- {
- "name": "supportLists",
- // The value consists of the package name and fingerprint information and can contain a maximum of 255 characters. For details about how to obtain the fingerprint information, refer to Step 3 in section "Sending Files."
- "value": "com.huawei.idehelper:69178F8F5B8DF***"
- "extra": ""
- }
- ]
- }
- // Step 1: Create a P2P communication object.
- var p2pClient = new P2pClient();
- var peerPkgName = "com.*.*";
- var peerFinger = "67DFB7FD********";
-
- // Step 2: Set the package name of your phone app to be communicated with.
- p2pClient.setPeerPkgName(peerPkgName);
-
- // Step 3: Set the app fingerprint information on the phone.
- p2pClient.setPeerFingerPrint(peerFinger);
-
- // Step 4: Send a short message to your phone app.
- // Build a Builder object.
- var builderClient = new Builder();
- var messageStr = "Hello, Wear Engine!";
- builderClient.setDescription(messageStr);
- // Build a Message object
- var sendMessage = new Message();
- sendMessage.builder = builderClient;
- // Define the callback function.
- var sendCallback = {
- onSuccess: function() {
- // Related processing logic for your app after the send task is successfully executed.
- },
- onFailure: function() {
- // Related processing logic for your app after the send task fails.
- },
- onSendResult: function(resultCode) {
- // Process the result code and information returned after the send task is complete.
- console.log(resultCode.data + resultCode.code);
- },
- }
- if(sendMessage != null && sendCallback != null){
- p2pClient.send(sendMessage, sendCallback);
- };
Versions earlier than HarmonyOS 2.0 of lite wearable devices do not support the setPeerFingerPrint method. Use config.json to obtain the fingerprint information of your phone app. For details, refer to step 3 in Sending P2P Messages.
- // Step 1: Create a P2P communication object.
- var p2pClient = new P2pClient();
- var peerPkgName = "com.*.*";
- var peerFinger = "67DFB7FD********";
-
- // Step 2: Set the package name of your phone app to be communicated with.
- p2pClient.setPeerPkgName(peerPkgName);
-
- // Step 3: Set the app fingerprint information on the phone.
- p2pClient.setPeerFingerPrint(peerFinger);
-
- // Step 4: Send a file to the phone app.
- // Build a Builder object.
- var testFile = {
- "name" : "internal://app/testPhoto.png", // File path. If the file fails to be sent and the result code is 208, it indicates that the wearable version is too early. In this case, instruct users to update the version.
- "mode": "", // File type, 'text' or 'binary'
- "mode2": "", // File attribute, "R", "W", or "RW"
- };
- var builderClient = new Builder();
- builderClient.setPayload(testFile);
- // Build a Message object.
- var sendMessage = new Message();
- sendMessage.builder = builderClient;
- // Define the callback function.
- var sendCallback = {
- onSuccess: function() {
- // Related processing logic for your app after the send task is successfully executed.
- },
- onFailure: function() {
- // Related processing logic for your app after the send task fails.
- },
- onSendResult: function(resultCode) {
- // Process the result code and information returned after the send task is complete.
- console.log(resultCode.data + resultCode.code);
- },
- onSendProgress: function(count) {
- // Return the progress information after the send task is complete.
- console.log("Progress" + count);
- },
- }
- if(sendMessage != null && sendCallback != null){
- p2pClient.send(sendMessage, sendCallback);
- };
Versions earlier than HarmonyOS 2.0 of lite wearable devices do not support the setPeerFingerPrint method. Use config.json to obtain the fingerprint information of your phone app. For details, refer to step 3 in Sending P2P Messages.
- // Step 1: Create a P2P communication object.
- var p2pClient = new P2pClient();
- var peerPkgName = "com.*.*";
- var peerFinger = "67DFB7FD********";
-
- // Step 2: Set the package name of your phone app to be communicated with.
- p2pClient.setPeerPkgName(peerPkgName);
-
- // Step 3: Set the app fingerprint information on the phone.
- p2pClient.setPeerFingerPrint(peerFinger);
-
- // Step 4: Receive short messages or files from your phone app.
- // Define the receiver
- var flash = this;
- var receiver = {
- onSuccess: function() {
- // Process the callback function returned when messages or files have been received from the phone during registration.
- flash.receiveMessageOK="Succeeded in receiving the message";
- },
- onFailure: function() {
- // Process the callback function returned when messages or files fail to be received from the phone during registration.
- flash.receiveMessageOK="Failed to receive the message";
- },
- onReceiveMessage: function (data) {
- if (data && data.isFileType) {
- // Process the file sent by your phone app.
- flash.receiveMessageOK = "file:"+data.name;
- } else {
- // Process the message sent from your phone app.
- flash.receiveMessageOK = "message:"+data;
- }
- },
- }
- p2pClient.registerReceiver(receiver);
-
- // Step 5: Cancel the reception of messages or files from your phone app.
- p2pClient.unregisterReceiver({
- onSuccess:function() {
- console.log = ("Stopped receiving messages");
- },
- onFailure:function() {
- console.log = ("Failed to stop receiving messages");
- }
- });
If the phone app is launched while the wearable app is not, the phone app can launch the latter by using the ping method.
- public class HiWearMainAbility extends AceAbility {
- @Override
- public void onStart(Intent intent) {
- super.onStart(intent);
- }
-
- @Override
- public void onStop() {
- super.onStop();
- }
- }
- "abilities": [
- ......
- {
- ......
- "visible": true,
- "name": ".HiWearMainAbility",
- "type": "page",
- ......
- },
- ......
- ],