This module allows third-party apps on wearable devices to subscribe to the connection status between the watch and phone.
Since: 6.1.1(24)
Base class for subscribing to and unsubscribing from the connection status between the watch and phone.
static onConnectionStateChange(callback: MonitorEventCallback): void
Listens for device status changes. This API uses an asynchronous callback to return the result.
Model restriction: This API can be used only in the FA model.
System capability: SystemCapability.Health.WearEngine.Lite
Device behavior differences: This API is supported on wearables and lite wearables, but does not respond on other device types.
Since: 6.1.1(24)
Parameters:
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| callback | MonitorEventCallback | Yes | Callback used to return the device status change information. |
Example:
let eventCallback = {
// Event change callback (triggered when the device connection status changes).
eventChange: (data) => {
console.info(`Succeeded in subscribing connection status. event: ${data.event}, data: ${data.data.data}`);
},
success: (code, data) => {
console.info(`Succeeded in subscribing connection status. Code: ${code.code}, data: ${data.data}`);
},
fail: (error, errorMessage) => {
console.error(`Failed to subscribe connection status. Code: ${error.code}, data: ${errorMessage.data}`);
}
};
WearEngineLite.onConnectionStateChange(eventCallback); static offConnectionStateChange(callback?: MonitorEventCallback): void
Cancels listening for device status changes. This API uses an asynchronous callback to return the result.
Model restriction: This API can be used only in the FA model.
System capability: SystemCapability.Health.WearEngine.Lite
Device behavior differences: This API is supported on wearables and lite wearables, but does not respond on other device types.
Since: 6.1.1(24)
Parameters:
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| callback | MonitorEventCallback | No | Callback used to return information of the MonitorEventCallback class. If this parameter is not specified, the listening event is successfully canceled. |
Example:
let eventCallback = {
success: (code, data) => {
console.info(`Succeeded in unsubscribing connection status. code: ${code.code}, data: ${data.data}`);
},
fail: (error, errorMessage) => {
console.error(`Failed to unsubscribe connection status. code:${error.code}, data:${errorMessage.data}`);
}
};
WearEngineLite.offConnectionStateChange(eventCallback); static onFileReceive(remoteAppInfo: AppInfo, callback: FileReceiverCallback): void
Subscribes to the events that the peer device sends a file to the local device and checks the file receiving progress. This API uses an asynchronous callback to return the result.
Model restriction: This API can be used only in the FA model.
System capability: SystemCapability.Health.WearEngine.Lite
Since: 26.0.0
Parameters:
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| remoteAppInfo | AppInfo | Yes | App information of the peer device. |
| callback | FileReceiverCallback | Yes | Callback used to return information of the FileReceiverCallback class. |
Example:
// Set the device-side app information, including the bundle name and fingerprint.
let remoteAppInfo = {
bundleName: '',
fingerprint: ''
};
// Set the file storage path for receiving files.
globalThis.__fileDir__ = 'xxxx/xxxx';
// Set information about the file to be received and the transfer progress.
let FileReceiverCallback =
{
onReceive: (fileName, filePath, progress)=>{
console.info(`Succeeded in onFileReceiving, fileName: ${fileName}, filePath: ${filePath}, progress: ${progress}`);
},
success: (code, data) => {
console.info(`Succeeded in onFileReceiving, Code: ${code}, data: ${data}`);
},
fail: (code, data) => {
console.error(`Failed to onFileReceive, Code: ${code}, data: ${data}`);
}
};
try {
WearEngineLite.onFileReceive(remoteAppInfo, FileReceiverCallback);
} catch (error) {
console.error(`Failed to onFileReceive. Code: ${error.code}, message: ${error.data}.`);
}; static offFileReceive(remoteAppInfo: AppInfo, callback?: FileReceiverCallback): void
Unsubscribes from the events that the peer device sends a file to the local device and checks the file receiving progress. This API uses an asynchronous callback to return the result.
Model restriction: This API can be used only in the FA model.
System capability: SystemCapability.Health.WearEngine.Lite
Since: 26.0.0
Parameters:
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| remoteAppInfo | AppInfo | Yes | App information of the peer device. |
| callback | FileReceiverCallback | No | Callback used to return information of the FileReceiverCallback class. If this parameter is not specified, the event subscription is successfully canceled. |
Example:
// Set the device-side app information, including the bundle name and fingerprint.
let remoteAppInfo = {
bundleName: '',
fingerprint: ''
};
// Set the callback used to return information about the file to be received.
let fileReceiverCallback =
{
onReceive: (fileName, filePath, progress)=>{
console.info(`Succeeded in offFileReceiving, fileName: ${fileName}, filePath: ${filePath}, progress: ${progress}`);
},
success: (code, data) => {
console.info(`Succeeded in offFileReceiving, Code: ${code}, data: ${data}`);
},
fail: (code, data) => {
console.error(`Failed to offFileReceive, Code: ${code}, data: ${data}`);
}
};
try {
WearEngineLite.offFileReceive(remoteAppInfo, fileReceiverCallback);
} catch (error) {
console.error(`Failed to offFileReceive. Code: ${error.code}, message: ${error.data}.`);
}; As an input parameter of the onConnectionStateChange API, this API transfers the changed device status information to the callback function when the subscribed event is triggered. As an input parameter of the offConnectionStateChange API, this API is used to cancel listening for device connection status changes.
eventChange(data: MonitorEventData): void
Listens for device status change events.
Model restriction: This API can be used only in the FA model.
System capability: SystemCapability.Health.WearEngine.Lite
Device behavior differences: This API is supported on wearables and lite wearables, but does not respond on other device types.
Since: 6.1.1(24)
Parameters:
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| data | MonitorEventData | Yes | Device status information after the change. |
Example:
let eventCallback = {
// Event change callback (triggered when the device connection status changes).
eventChange: (data) => {
console.info(`Succeeded in subscribing connection status, event: ${data.event}, data: ${data.data.data}`);
}
};
WearEngineLite.onConnectionStateChange(eventCallback); success(code: number, data?: string): void
Indicates that the subscription or unsubscription is successful.
Model restriction: This API can be used only in the FA model.
System capability: SystemCapability.Health.WearEngine.Lite
Device behavior differences: This API is supported on wearables and lite wearables, but does not respond on other device types.
Since: 6.1.1(24)
Parameters:
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| code | number | Yes | 0 returned. |
| data | string | No | The default value is undefined. |
Example:
let eventCallback = {
success: (code, data) => {
console.info(`Succeeded in subscribing connection status, Code: ${code.code}, data: ${data.data}`);
}
};
WearEngineLite.onConnectionStateChange(eventCallback); fail(code: number, data?: string): void
Indicates that the subscription or unsubscription fails.
Model restriction: This API can be used only in the FA model.
System capability: SystemCapability.Health.WearEngine.Lite
Device behavior differences: This API is supported on wearables and lite wearables, but does not respond on other device types.
Since: 6.1.1(24)
Parameters:
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| code | number | Yes | If -1 is returned for wearables and 401 is returned for lite wearables, the subscription or unsubscription fails. The possible cause is that a mandatory parameter is empty (for example, eventChange is empty). |
| data | string | No | The default value is undefined. |
Example:
let eventCallback = {
fail: (error, errorMessage) => {
console.error(`Failed to subscribe connection status. Code: ${error.code}, data: ${errorMessage.data}`);
}
};
WearEngineLite.onConnectionStateChange(eventCallback); Device status information.
Model restriction: This API can be used only in the FA model.
System capability: SystemCapability.Health.WearEngine.Lite
Since: 6.1.1(24)
| Name | Type | Read-Only | Optional | Description |
|---|---|---|---|---|
| code | number | No | No | 0 returned. |
| data | string | No | Yes | Device connection status. The value 2 indicates that the connection is successful, and the value 3 indicates that the connection is interrupted. |
As a parameter of eventChange, this API transfers the device status change information to the callback function when the subscribed event is triggered.
Model restriction: This API can be used only in the FA model.
System capability: SystemCapability.Health.WearEngine.Lite
Since: 6.1.1(24)
| Name | Type | Read-Only | Optional | Description |
|---|---|---|---|---|
| event | string | No | No | Subscribed event reported by the callback function. |
| data | MonitorData | No | No | Device status information after the change. |
As an input parameter of the onFileReceive API, this API transfers the changed file transfer progress to the callback function when the file transfer progress changes. As an input parameter of the offFileReceive API, this API is used to cancel the subscription to the event that the peer app sends files to the local app.
onReceive(fileName: string, filePath: string, progress: number): void
Receives files sent by the peer device and views the receiving progress.
Model restriction: This API can be used only in the FA model.
System capability: SystemCapability.Health.WearEngine.Lite
Since: 26.0.0
Parameters:
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| fileName | string | Yes | Name of the file to be received. |
| filePath | string | Yes | Storage path of the file to be received. |
| progress | number | Yes | Progress of receiving a file. The value ranges from 0 to 100. |
Example:
// Set the device-side app information, including the bundle name and fingerprint.
let remoteAppInfo = {
bundleName: '',
fingerprint: ''
};
// Set the callback used to return information about the file to be received.
let fileReceiverCallback =
{
onReceive: (fileName, filePath, progress)=>{
console.info(`Succeeded in onFileReceiving, fileName: ${fileName}, filePath: ${filePath}, progress: ${progress}`);
},
success: (code, data) => {
console.info(`Succeeded in onFileReceiving, Code: ${code}, data: ${data}`);
},
fail: (code, data) => {
console.error(`Failed to onFileReceive, Code: ${code}, data: ${data}`);
}
};
try {
WearEngineLite.onFileReceive(remoteAppInfo, fileReceiverCallback);
} catch (error) {
console.error(`Failed to onFileReceive. Code: ${error.code}, message: ${error.data}.`);
}; success(code: number, data?: string): void
Indicates that the subscription to or unsubscription from the file receiving event is successful.
Model restriction: This API can be used only in the FA model.
System capability: SystemCapability.Health.WearEngine.Lite
Since: 26.0.0
Parameters:
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| code | number | Yes | The value 0 indicates that the subscription or unsubscription is successful. |
| data | string | No | The default value is undefined. |
Example:
// Set the device-side app information, including the bundle name and fingerprint.
let remoteAppInfo = {
bundleName: '',
fingerprint: ''
};
// Set the callback used to return information about the file to be received.
let fileReceiverCallback =
{
onReceive: (fileName, filePath, progress)=>{
console.info(`Succeeded in onFileReceiving, fileName: ${fileName}, filePath: ${filePath}, progress: ${progress}`);
},
success: (code, data) => {
console.info(`Succeeded in onFileReceiving, Code: ${code}, data: ${data}`);
},
fail: (code, data) => {
console.error(`Failed to onFileReceive, Code: ${code}, data: ${data}`);
}
};
try {
WearEngineLite.onFileReceive(remoteAppInfo, fileReceiverCallback);
} catch (error) {
console.error(`Failed to onFileReceive. Code: ${error.code}, message: ${error.data}.`);
}; fail(code: number, data?: string): void
Indicates that the subscription to or unsubscription from the file receiving event fails.
Model restriction: This API can be used only in the FA model.
System capability: SystemCapability.Health.WearEngine.Lite
Since: 26.0.0
Parameters:
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| code | number | Yes | If -1 is returned for wearables and 401 is returned for lite wearables, the subscription or unsubscription fails. The possible cause is that a mandatory parameter is empty (for example, onReceive is empty) or the parameter value is out of range (for example, the bundle name length does not meet the requirements). |
| data | string | No | The default value is undefined. |
Example:
// Set the device-side app information, including the bundle name and fingerprint.
let remoteAppInfo = {
bundleName: '',
fingerprint: ''
};
// Set the callback used to return information about the file to be received.
let fileReceiverCallback =
{
onReceive: (fileName, filePath, progress)=>{
console.info(`Succeeded in onFileReceiving, fileName: ${fileName}, filePath: ${filePath}, progress: ${progress}`);
},
success: (code, data) => {
console.info(`Succeeded in onFileReceiving, Code: ${code}, data: ${data}`);
},
fail: (code, data) => {
console.error(`Failed to onFileReceive, Code: ${code}, data: ${data}`);
}
};
try {
WearEngineLite.onFileReceive(remoteAppInfo, fileReceiverCallback);
} catch (error) {
console.error(`Failed to onFileReceive. Code: ${error.code}, message: ${error.data}.`);
}; Device-side app information class.
Model restriction: This API can be used only in the FA model.
System capability: SystemCapability.Health.WearEngine.Lite
Since: 26.0.0
| Name | Type | Read-Only | Optional | Description |
|---|---|---|---|---|
| bundleName | string | No | No | App name. |
| fingerprint | string | No | No | App fingerprint, a unique ID for identifying an app. For details about how to obtain the app fingerprint, please refer to How Do I Obtain the App Fingerprint?. |