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. 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" } ] 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] } 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) 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: Prepare the package name and appid of the watch application, which will be required during communication. 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" } ] 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') } }) // 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) // Receive a message p2pClient.registerMessageReceiver(this.device.randomId, appParam, callback) Notes: Before device communication, only binary data can be sent, so it is necessary to convert it into the corresponding format through textEncoder.encodeInto. 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: '' } } Summary of the Ideas 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. Then obtain the client device object deviceClient through wearEngine.getDeviceClient. Further obtain the list of wearable devices connected to the current mobile phone through deviceClient.getConnectedDevices of the client device object. Obtain the specified device through the type and name of the wearable device list. 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.

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.
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"
}
]
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]
}
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)
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:
- Prepare the package name and appid of the watch application, which will be required during communication.
- 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"
}
]
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')
}
})
// 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)
// Receive a message
p2pClient.registerMessageReceiver(this.device.randomId, appParam, callback)
Notes:
- Before device communication, only binary data can be sent, so it is necessary to convert it into the corresponding format through
textEncoder.encodeInto
. -
appParam
is the watch package name and appid prepared in3.2
, with the following format:
{
remoteApp: {
// Set the application information of the device-side application: package name and fingerprint
bundleName: '',
fingerprint: ''
}
}
Summary of the Ideas
- 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
. - Then obtain the client device object
deviceClient
throughwearEngine.getDeviceClient
. - Further obtain the list of wearable devices connected to the current mobile phone through
deviceClient.getConnectedDevices
of the client device object. - Obtain the specified device through the type and name of the wearable device list.
- 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.