DEV Community

simple lau
simple lau

Posted on

How to communicate with wearable devices in HarmonyOS development

0. Preparation Work

Enter the "Management Center" of the Huawei Developer Alliance, and click on the "Wear Engine" card under the "Application Service" tab.

Note: The permission review may take a long time. Even if expedited, it may take 3 to 5 days. Therefore, if you need to use this permission, you should apply for it as early as possible to avoid delaying important matters.

Image description

1. Project Configuration

Configure the client_id in the module.json5 of the engineering project. If it has been configured previously, you can ignore this step.

"metadata": [ // Configure the following information
    {
        "name": "client_id",
        "value": "xxxxxx"
    }
]
Enter fullscreen mode Exit fullscreen mode

2. Permission Acquisition

// Step 1: Obtain the AuthClient object
let authClient: wearEngine.AuthClient = wearEngine.getAuthClient(getContext(this));

// Step 2: Define the permission request class based on the permissions that require user authorization
let request: wearEngine.AuthorizationRequest = {
  permissions: [wearEngine.Permission.USER_STATUS]
}
Enter fullscreen mode Exit fullscreen mode

Note: The permissions you request here must correspond to the permissions you've applied for in the developer console. If the permissions don't match, you'll encounter an error code 1008500004.
The available permission enumerations are:

  • USER_STATUS: User status data
  • HEALTH_SENSOR: Health sensor data (e.g., heart rate)
  • MOTION_SENSOR: Motion sensor data (e.g., accelerometer)
  • DEVICE_IDENTIFIER: Device identifier information

3. Device Communication

3.1 Obtain Devices

// Obtain the DeviceClient object
const deviceClient = wearEngine.getDeviceClient(ctx)
// Obtain the connected wearable devices
const deviceList = await this.deviceClient.getConnectedDevices()
// Obtain the specified wearable device
const targetDevice = deviceList.find(device => device.category === wearEngine.DeviceCategory.WATCH)
Enter fullscreen mode Exit fullscreen mode

After obtaining the device list, you can further obtain the specified device through the device's category, name, device model, etc. Once you have obtained the device, you can perform the next steps of operation on it.

3.2 Communication Configuration

Some preparatory work needs to be done before the device communicates with other devices:

  1. Prepare the package name and appid of the watch application, which will be required during communication.
  2. Configure the package name and appid of the current application on both the mobile phone side and the watch side. If not configured properly, errors such as 206 and 207 will occur. If there are multiple package names, you can separate them with commas (in module.json5).
metadata: [
    {
      "name": "wearEngineRemoteAppNameList",
      "value": "xxx.xxx.xxx"
    }
]
Enter fullscreen mode Exit fullscreen mode

3.3 Device Communication

Before device communication, you can first check whether the device that needs to communicate is properly installed on the current device, and launch the application on the remote device.

// Obtain the client of the P2p module
const p2pClient = wearEngine.getP2pClient(ctx)

// Check if it is installed currently
p2pClient.isRemoteAppInstalled(this.device.randomId, this.remoteBundleName)
  .then((isInstalled) => {
    if(isInstalled) {
        // Launch the remote app
       await p2pClient.startRemoteApp(targetDevice.randomId, 'Specified package name')
    }
  })
Enter fullscreen mode Exit fullscreen mode
// Send a message
const textEncoder: util.TextEncoder = new util.TextEncoder();
const messageStr = JSON.stringify(messageContent)
const message: wearEngine.P2pMessage = {
  content: textEncoder.encodeInto(messageStr)
}
p2pClient.sendMessage(this.device.randomId, appParam, message)
Enter fullscreen mode Exit fullscreen mode
// Receive a message
p2pClient.registerMessageReceiver(this.device.randomId, appParam, callback)
Enter fullscreen mode Exit fullscreen mode

Notes:

  1. Before device communication, only binary data can be sent, so it is necessary to convert it into the corresponding format through textEncoder.encodeInto.
  2. appParam is the watch package name and appid prepared in 3.2, with the following format:
    {
      remoteApp: {
        // Set the application information of the device-side application: package name and fingerprint
        bundleName: '',
        fingerprint: ''
      }
    }
Enter fullscreen mode Exit fullscreen mode

Summary of the Ideas

  1. During the development process, first apply for the permission for the current application to access the wearable device service connected to the mobile phone through wearEngine.AuthClient.
  2. Then obtain the client device object deviceClient through wearEngine.getDeviceClient.
  3. Further obtain the list of wearable devices connected to the current mobile phone through deviceClient.getConnectedDevices of the client device object.
  4. Obtain the specified device through the type and name of the wearable device list.
  5. Use the built-in methods of wearEngine to obtain the specified client class, which can be used to monitor the status of the current device, launch the service, obtain the sensor data of the wearable device, and perform two-way communication, etc.

The version used in this article and code is HarmonyOS 5.0.1 Release SDK.

Top comments (0)