We use essential cookies for the website to function, as well as analytics cookies for analyzing and creating statistics of the website performance. To agree to the use of analytics cookies, click "Accept All". You can manage your preferences at any time by clicking "Cookie Settings" on the footer. More Information.

Only Essential Cookies
Accept All
GuidesWear EngineWearable App DevelopmentWearable App Development (JS)App-to-App Message Communications

App-to-App Message Communications

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.

Checking Whether Your App Has Been Installed on the Phone

Before sending a P2P message, you can use the ping method to check whether your wearable app can communicate with the phone app.

  1. Create a P2pClient object.
  2. Call the setPeerPkgName method and specify the package name of your phone app to be communicated with using the ping method.
  3. Call the ping method of the P2pClient object to check whether the specified app has been installed on the phone. If the app has not been installed on the phone, result code 204 will be returned. If the app has been installed on the phone, result code 205 will be returned.

    Collapse
    Word wrap
    Dark theme
    Copy code
    1. // Step 1: Create a P2P communication object.
    2. var p2pClient = new P2pClient();
    3. var peerPkgName = "com.*.*";
    4. // Step 2: Set the package name of your phone app to be communicated with.
    5. p2pClient.setPeerPkgName(peerPkgName);
    6. // Step 3: Check whether your app has been installed on the phone.
    7. p2pClient.ping({
    8. onSuccess: function() {
    9. console.log('ping success.');
    10. },
    11. onFailure: function() {
    12. console.log('ping failed');
    13. },
    14. onPingResult: function(resultCode) {
    15. console.log(resultCode.data + resultCode.code);
    16. },
    17. });

Sending P2P Messages or Files to Your Phone App

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.

Sending P2P Messages

  1. Create a P2pClient object.
  2. Call the setPeerPkgName method and specify the package name of your phone app to be communicated with.
  3. Call the setPeerFingerPrint method and specify the fingerprint SHA-256 information of your phone app to be communicated with. (Refer to Generating a Signing Certificate Fingerprint to obtain an SHA-256 certificate fingerprint. The fingerprint information does not contain colons, and the fingerprint is one of the fingerprints registered by your phone app when applying for the HUAWEI ID Service from HUAWEI Developers, that is, one of the SHA-256 certificate fingerprints in Step 4 in Registering a HUAWEI ID and Applying for the HUAWEI ID Service.)

    NOTE
    Versions earlier than HarmonyOS 2.0 of lite wearable devices do not support the calling of the setPeerFingerPrint method. Use config.json to obtain the fingerprint information of your phone app.
    Collapse
    Word wrap
    Dark theme
    Copy code
    1. // Set metaData of your phone app in the module of the config.json file.
    2. "metaData": {
    3. "customizeData": [
    4. {
    5. "name": "supportLists",
    6. // 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."
    7. "value": "com.huawei.idehelper:69178F8F5B8DF***"
    8. "extra": ""
    9. }
    10. ]
    11. }

  4. Call the send method in the P2pClient object to send short messages from your wearable app to the phone app.

    • Create a Builder object to define messages that need to be sent from your app.
    • Create a Message object and provide the Builder object to the send method.
      Collapse
      Word wrap
      Dark theme
      Copy code
      1. // Step 1: Create a P2P communication object.
      2. var p2pClient = new P2pClient();
      3. var peerPkgName = "com.*.*";
      4. var peerFinger = "67DFB7FD********";
      5. // Step 2: Set the package name of your phone app to be communicated with.
      6. p2pClient.setPeerPkgName(peerPkgName);
      7. // Step 3: Set the app fingerprint information on the phone.
      8. p2pClient.setPeerFingerPrint(peerFinger);
      9. // Step 4: Send a short message to your phone app.
      10. // Build a Builder object.
      11. var builderClient = new Builder();
      12. var messageStr = "Hello, Wear Engine!";
      13. builderClient.setDescription(messageStr);
      14. // Build a Message object
      15. var sendMessage = new Message();
      16. sendMessage.builder = builderClient;
      17. // Define the callback function.
      18. var sendCallback = {
      19. onSuccess: function() {
      20. // Related processing logic for your app after the send task is successfully executed.
      21. },
      22. onFailure: function() {
      23. // Related processing logic for your app after the send task fails.
      24. },
      25. onSendResult: function(resultCode) {
      26. // Process the result code and information returned after the send task is complete.
      27. console.log(resultCode.data + resultCode.code);
      28. },
      29. }
      30. if(sendMessage != null && sendCallback != null){
      31. p2pClient.send(sendMessage, sendCallback);
      32. };

Sending Files

  1. Create a P2pClient object.
  2. Call the setPeerPkgName method and specify the package name of your phone app to be communicated with.
  3. Call the setPeerFingerPrint method and specify the fingerprint SHA-256 information of your phone app to be communicated with. (Refer to Generating a Signing Certificate Fingerprint to obtain an SHA-256 certificate fingerprint. The fingerprint information does not contain colons, and the fingerprint is one of the fingerprints registered by your phone app when applying for the HUAWEI ID Service from HUAWEI Developers, that is, one of the SHA-256 certificate fingerprints in Step 4 in Registering a HUAWEI ID and Applying for the HUAWEI ID Service.)

    NOTE

    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.

  4. Call the send method of the P2pClient object to send files from your wearable app to the phone app. (You cannot send different files of the same type at the same time from a lite wearable device to the phone. Do not send another file before the ongoing transfer completes.)

    • Create a Builder object to define files that need to be sent by your app. (The file size should be less than 4 MB).
    • Create a Message object and provide the Builder object to the send method.
    Collapse
    Word wrap
    Dark theme
    Copy code
    1. // Step 1: Create a P2P communication object.
    2. var p2pClient = new P2pClient();
    3. var peerPkgName = "com.*.*";
    4. var peerFinger = "67DFB7FD********";
    5. // Step 2: Set the package name of your phone app to be communicated with.
    6. p2pClient.setPeerPkgName(peerPkgName);
    7. // Step 3: Set the app fingerprint information on the phone.
    8. p2pClient.setPeerFingerPrint(peerFinger);
    9. // Step 4: Send a file to the phone app.
    10. // Build a Builder object.
    11. var testFile = {
    12. "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.
    13. "mode": "", // File type, 'text' or 'binary'
    14. "mode2": "", // File attribute, "R", "W", or "RW"
    15. };
    16. var builderClient = new Builder();
    17. builderClient.setPayload(testFile);
    18. // Build a Message object.
    19. var sendMessage = new Message();
    20. sendMessage.builder = builderClient;
    21. // Define the callback function.
    22. var sendCallback = {
    23. onSuccess: function() {
    24. // Related processing logic for your app after the send task is successfully executed.
    25. },
    26. onFailure: function() {
    27. // Related processing logic for your app after the send task fails.
    28. },
    29. onSendResult: function(resultCode) {
    30. // Process the result code and information returned after the send task is complete.
    31. console.log(resultCode.data + resultCode.code);
    32. },
    33. onSendProgress: function(count) {
    34. // Return the progress information after the send task is complete.
    35. console.log("Progress" + count);
    36. },
    37. }
    38. if(sendMessage != null && sendCallback != null){
    39. p2pClient.send(sendMessage, sendCallback);
    40. };

Receiving P2P Messages or Files from Your Phone App

  1. Create a P2pClient object.
  2. Call the setPeerPkgName method and specify the package name of your phone app to be communicated with.
  3. Call the setPeerFingerPrint method and specify the fingerprint SHA-256 information of your phone app to be communicated with. (Refer to Generating a Signing Certificate Fingerprint to obtain an SHA-256 certificate fingerprint. The fingerprint information does not contain colons, and the fingerprint is one of the fingerprints registered by your phone app when applying for the HUAWEI ID Service from HUAWEI Developers, that is, one of the SHA-256 certificate fingerprints in Step 4 in Registering a HUAWEI ID and Applying for the HUAWEI ID Service.)

    NOTE

    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.

  4. By calling the registerReceiver method, your wearable app can receive short messages or files from the phone app (the received files will be stored in the app's private directory internal://app/). The principle behind this is to register a listener for Wear Engine.
  5. Call the unregisterReceiver method to cancel the reception of messages or files sent from your phone app. The principle behind this is to unregister a listener for Wear Engine. You are advised to call the unregisterReceiver method in or before the onDestroy lifecycle of your app to release server resources.

    Collapse
    Word wrap
    Dark theme
    Copy code
    1. // Step 1: Create a P2P communication object.
    2. var p2pClient = new P2pClient();
    3. var peerPkgName = "com.*.*";
    4. var peerFinger = "67DFB7FD********";
    5. // Step 2: Set the package name of your phone app to be communicated with.
    6. p2pClient.setPeerPkgName(peerPkgName);
    7. // Step 3: Set the app fingerprint information on the phone.
    8. p2pClient.setPeerFingerPrint(peerFinger);
    9. // Step 4: Receive short messages or files from your phone app.
    10. // Define the receiver
    11. var flash = this;
    12. var receiver = {
    13. onSuccess: function() {
    14. // Process the callback function returned when messages or files have been received from the phone during registration.
    15. flash.receiveMessageOK="Succeeded in receiving the message";
    16. },
    17. onFailure: function() {
    18. // Process the callback function returned when messages or files fail to be received from the phone during registration.
    19. flash.receiveMessageOK="Failed to receive the message";
    20. },
    21. onReceiveMessage: function (data) {
    22. if (data && data.isFileType) {
    23. // Process the file sent by your phone app.
    24. flash.receiveMessageOK = "file:"+data.name;
    25. } else {
    26. // Process the message sent from your phone app.
    27. flash.receiveMessageOK = "message:"+data;
    28. }
    29. },
    30. }
    31. p2pClient.registerReceiver(receiver);
    32. // Step 5: Cancel the reception of messages or files from your phone app.
    33. p2pClient.unregisterReceiver({
    34. onSuccess:function() {
    35. console.log = ("Stopped receiving messages");
    36. },
    37. onFailure:function() {
    38. console.log = ("Failed to stop receiving messages");
    39. }
    40. });

Launching the App on the Wearable by Calling the Ping Method on the Phone App

If the phone app is launched while the wearable app is not, the phone app can launch the latter by using the ping method.

  • If the deviceType of the wearable JS app contains liteWearable, the app can be installed on a lite smart wearable (sports watch), with no need for other additional configuration. Use the ping method on the phone to start the wearable app.
  • If the deviceType of the wearable app JS contains only wearable, the app can be installed only on smart wearables. In this case, you need to create a specified Page Ability to start the app. Perform the following steps:
  1. Create a JS Page Ability on the wearable device and name it HiWearMainAbility.java. You can right-click the project directory and choose New > Ability from the shortcut menu to create a Page Ability. Alternatively, manually compile code to create a Page Ability. Sample code:

    Collapse
    Word wrap
    Dark theme
    Copy code
    1. public class HiWearMainAbility extends AceAbility {
    2. @Override
    3. public void onStart(Intent intent) {
    4. super.onStart(intent);
    5. }
    6. @Override
    7. public void onStop() {
    8. super.onStop();
    9. }
    10. }
    NOTE
    1. The name of the Ability is HiWearMainAbility.java, which cannot be customized. The HiWearMainAbility package name must correspond to the app package name.
    2. AceAbility, not Ability, needs to be inherited.

  2. Declare the Ability in the config.json file. Set type to page and visible to true.

    Collapse
    Word wrap
    Dark theme
    Copy code
    1. "abilities": [
    2. ......
    3. {
    4. ......
    5. "visible": true,
    6. "name": ".HiWearMainAbility",
    7. "type": "page",
    8. ......
    9. },
    10. ......
    11. ],

  3. After the preceding configuration is complete, use the ping method on the phone to start the wearable app. For details, see Checking Whether the Wearable App is Running.