目录

AgentHostProxy

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

  • 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

import { common } from '@kit.AbilityKit';

AgentHostProxy

[h2]sendData

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

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.

ID Error Message
35600002 Failed to send the IPC message.

Example

import { common, AgentExtensionAbility } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';

const TAG: string = '[AgentExtensionAbility] ';

export default class MyAgentExtensionAbility extends AgentExtensionAbility {
  // Handle data sending.
  onData(proxy: common.AgentHostProxy, data: string) {
    console.info(TAG + `onData ${data}`);
    try {
      // Send data to the client of AgentExtensionAbility.
      proxy.sendData('Hello Client');
    } catch (err) {
      let code = (err as BusinessError).code;
      let msg = (err as BusinessError).message;
      console.error(`${TAG} sendData failed, err code: ${code}, err msg: ${msg}.`);
    }
  }
}

[h2]authorize

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

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.

ID Error Message
35600002 Failed to send the IPC message.

Example

import { common, AgentExtensionAbility } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';

const TAG: string = '[AgentExtensionAbility] ';

export default class MyAgentExtensionAbility extends AgentExtensionAbility {
  // Handle security authentication.
  onAuth(proxy: common.AgentHostProxy, handshakeData: string) {
    console.info(TAG + `onAuth ${handshakeData}`);
    try {
      // Send the authentication data to the client of AgentExtensionAbility.
      proxy.authorize('handshake_token');
    } catch (err) {
      let code = (err as BusinessError).code;
      let msg = (err as BusinessError).message;
      console.error(`${TAG} authorize failed, err code: ${code}, err msg: ${msg}.`);
    }
  }
}