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
ReferencesApplication FrameworkAbility KitArkTS APIsDependent Elements and DefinitionsapplicationAgentHostProxy

AgentHostProxy

Phone24+PC/2in124+Tablet24+TV24+Wearable24+

AgentHostProxy is used to send data or security authentication requests from the AgentExtensionAbility server to the client.

NOTE
  • The initial APIs of this module are supported since API version 24. Newly added APIs will be marked with a superscript to indicate their earliest API version.
  • The APIs of this module can be used only in the stage model.
  • The APIs of this module must be used in the main thread. They are not supported in subthreads such as Worker and TaskPool.

Modules to Import

Collapse
Word wrap
Dark theme
Copy code
  1. import { common } from '@kit.AbilityKit';

AgentHostProxy

Phone24+PC/2in124+Tablet24+TV24+Wearable24+

sendData

Phone24+PC/2in124+Tablet24+TV24+Wearable24+

sendData(data: string): void

Sends data from the AgentExtensionAbility server to the client.

Atomic service API: This API can be used in atomic services since API version 24.

System capability: SystemCapability.Ability.AgentRuntime.Core

Parameters

Expand
Name Type Mandatory Description
data string Yes Data to be sent to the client of AgentExtensionAbility.

Error codes

For details about the error codes, see Universal Error Codes and Ability Error Codes.

Expand
ID Error Message
35600002 Failed to send the IPC message.

Example

Collapse
Word wrap
Dark theme
Copy code
  1. import { common, AgentExtensionAbility } from '@kit.AbilityKit';
  2. import { BusinessError } from '@kit.BasicServicesKit';
  3. const TAG: string = '[AgentExtensionAbility] ';
  4. export default class MyAgentExtensionAbility extends AgentExtensionAbility {
  5. // Handle data sending.
  6. onData(proxy: common.AgentHostProxy, data: string) {
  7. console.info(TAG + `onData ${data}`);
  8. try {
  9. // Send data to the client of AgentExtensionAbility.
  10. proxy.sendData('Hello Client');
  11. } catch (err) {
  12. let code = (err as BusinessError).code;
  13. let msg = (err as BusinessError).message;
  14. console.error(`${TAG} sendData failed, err code: ${code}, err msg: ${msg}.`);
  15. }
  16. }
  17. }

authorize

Phone24+PC/2in124+Tablet24+TV24+Wearable24+

authorize(handshakeData: string): void

Sends a security authentication request from the AgentExtensionAbility server to the client.

Atomic service API: This API can be used in atomic services since API version 24.

System capability: SystemCapability.Ability.AgentRuntime.Core

Parameters

Expand
Name Type Mandatory Description
handshakeData string Yes Security authentication data to be sent to the client.

Error codes

For details about the error codes, see Universal Error Codes and Ability Error Codes.

Expand
ID Error Message
35600002 Failed to send the IPC message.

Example

Collapse
Word wrap
Dark theme
Copy code
  1. import { common, AgentExtensionAbility } from '@kit.AbilityKit';
  2. import { BusinessError } from '@kit.BasicServicesKit';
  3. const TAG: string = '[AgentExtensionAbility] ';
  4. export default class MyAgentExtensionAbility extends AgentExtensionAbility {
  5. // Handle security authentication.
  6. onAuth(proxy: common.AgentHostProxy, handshakeData: string) {
  7. console.info(TAG + `onAuth ${handshakeData}`);
  8. try {
  9. // Send the authentication data to the client of AgentExtensionAbility.
  10. proxy.authorize('handshake_token');
  11. } catch (err) {
  12. let code = (err as BusinessError).code;
  13. let msg = (err as BusinessError).message;
  14. console.error(`${TAG} authorize failed, err code: ${code}, err msg: ${msg}.`);
  15. }
  16. }
  17. }
Search in References
Enter a keyword.