目录

accessoryAccessManager (Accessory Integration Management)

This module provides capabilities such as association wakeup, system service linkage, on-demand scheduling, and secure authorization management for Huawei ecosystem partners' accessories and apps.

Since: 26.0.0

Modules to Import

import { accessoryAccessManager } from '@kit.AccessoryKit';

DiscoveryType

Enumerates the communication types used for an accessory to be discovered and identified by a host device.

System capability: SystemCapability.Collaboration.Accessory.AccessManager

Model restriction: This API can be used only in the stage model.

Since: 26.0.0

Name Value Description
PARTNER_BLE_CONNECT 0 The BLE MAC address you provided is used to establish a BLE GATT connection.

ServiceName

Enumerates the names of services associated with an accessory.

System capability: SystemCapability.Collaboration.Accessory.AccessManager

Model restriction: This API can be used only in the stage model.

Since: 26.0.0

Name Value Description
PARTNER_APP_ACCESSORY_COLLABORATION 'P_AppAccessoryCollaboration' The app and the accessory interact with each other, including mutual wakeup and data transfer.
PARTNER_SHARE_SERVICE 'P_ShareService' The app receives files shared from the accessory.
PARTNER_DISTRIBUTED_CAMERA_SERVICE 'P_DCameraService' The app uses the accessory as a distributed camera.

WakeupType

Enumerates the methods of waking up an app.

System capability: SystemCapability.Collaboration.Accessory.AccessManager

Model restriction: This API can be used only in the stage model.

Since: 26.0.0

Name Value Description
START_ABILITY_BY_CALL 0 The app is woken up through startAbilityByCall.

StringResourceInfo

Defines the brief description of the woken-up app.

System capability: SystemCapability.Collaboration.Accessory.AccessManager

Model restriction: This API can be used only in the stage model.

Since: 26.0.0

Name Type Read-Only Optional Description
bundleName string No Yes Bundle name.
moduleName string No No Module name.
stringResourceId number No No Resource ID.

WakeupInfo

Defines the wakeup type and information about the woken-up app.

System capability: SystemCapability.Collaboration.Accessory.AccessManager

Model restriction: This API can be used only in the stage model.

Since: 26.0.0

Name Type Read-Only Optional Description
wakeupType WakeupType No No Wakeup type.
bundleName string No No Bundle name.
abilityName string No No Capability name.
briefDesc StringResourceInfo No No Brief description of the resource, which contains bundleName, moduleName, and stringResourceId. It is used to obtain the brief description of an app.

ServiceInfo

Defines the type of the associated service and corresponding service parameters.

System capability: SystemCapability.Collaboration.Accessory.AccessManager

Model restriction: This API can be used only in the stage model.

Since: 26.0.0

Name Type Read-Only Optional Description
serviceName ServiceName No No Name of the service associated with the accessory.
parameters Record<string, Object> No Yes Parameters related to the associated service.

PickerItemInfo

Defines accessory information and the service associated with the accessory.

System capability: SystemCapability.Collaboration.Accessory.AccessManager

Model restriction: This API can be used only in the stage model.

Since: 26.0.0

Name Type Read-Only Optional Description
discoveryType DiscoveryType No No Communication type used for an accessory to be discovered.
hasScreen boolean No Yes Indicates whether the accessory has a screen for displaying content such as a PIN. The value true indicates yes, and the value false indicates no. The default value is false.
bleAddress string No Yes BLE address.
bleMtuSize number No Yes Maximum transmission unit (MTU).
productId string No Yes Product ID.
subProductId string No Yes Sub-product ID.
displayName string No No Device name displayed in the dialog.
displayImage image.PixelMap No No Device image displayed in the dialog.
requestAttachServiceInfo Array<ServiceInfo> No No Associated service information after the accessory is authenticated.

AccessoryInfo

Defines the accessory information.

System capability: SystemCapability.Collaboration.Accessory.AccessManager

Model restriction: This API can be used only in the stage model.

Since: 26.0.0

Name Type Read-Only Optional Description
accessoryId string No Yes Accessory ID.
bleAddress string No Yes BLE address.
displayName string No No Displayed accessory name.
productId string No Yes Product ID.

AccessEvent

Enumerates the events related to accessory integration. For details about the triggering, please refer to Event Callback Data Structure Description for the showAccessPicker API.

System capability: SystemCapability.Collaboration.Accessory.AccessManager

Model restriction: This API can be used only in the stage model.

Since: 26.0.0

Name Value Description
PICKER_PRESENT 0 The picker appears.
PICKER_DISMISS 1 The picker disappears.
PICKER_END_SUCC 2 The picker completes and the accessory integration succeeds.
PICKER_END_FAIL 3 The picker completes and the accessory integration fails.
SERVICE_ATTACHING 300 A service is being attached.
SERVICE_ATTACH_SUCC 301 A service is attached successfully.
SERVICE_ATTACH_FAIL 302 The service attachment fails.

AttachServiceInfo

Defines the attached accessory and service information.

System capability: SystemCapability.Collaboration.Accessory.AccessManager

Model restriction: This API can be used only in the stage model.

Since: 26.0.0

Name Type Read-Only Optional Description
attachId number No No ID of the attached service.
accessoryInfo AccessoryInfo No No Accessory information.
serviceInfo ServiceInfo No No Service information.

AccessEventInfo

Defines the accessory integration event information.

System capability: SystemCapability.Collaboration.Accessory.AccessManager

Model restriction: This API can be used only in the stage model.

Since: 26.0.0

Name Type Read-Only Optional Description
accessEvent AccessEvent No No Event related to accessory integration. For details about the event triggering process, please refer to Event Callback Data Structure Description for the showAccessPicker API.
attachServiceInfo AttachServiceInfo No Yes Information about the attached service.
errorCode number No Yes

Integration event result code.

0: The integration is successful.

10000: The user cancels the operation by clicking the X sign in the normal integration process.

10001: The operation times out.

10002: The authentication fails. Check whether the product ID is correct.

10003: The dialog is abnormal.

10004: An exception occurs during trust establishment.

10005: The trust establishment fails.

10006: The service attachment fails.

errorDesc string No Yes Integration event result description.

DetachServiceEvent

Enumerates the states of accessory service removal.

System capability: SystemCapability.Collaboration.Accessory.AccessManager

Model restriction: This API can be used only in the stage model.

Since: 26.0.0

Name Value Description
SERVICE_DETACH_SUCC 0 The service is removed successfully.
SERVICE_DETACH_FAIL 1 The service removal fails.

AccessManager

Represents the accessory integration manager.

System capability: SystemCapability.Collaboration.Accessory.AccessManager

Model restriction: This API can be used only in the stage model.

Since: 26.0.0

[h2]showAccessPicker

showAccessPicker(items: Array<PickerItemInfo>, callback: Callback<AccessEventInfo>): number

Defines the information about the integrated accessory and its associated service. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Collaboration.Accessory.AccessManager

Model restriction: This API can be used only in the stage model.

Required permissions: ohos.permission.ALLOW_ACCESSORY_ACCESS. This capability is available only with this permission. For details about how to apply for the required permission, please refer to Requesting Restricted Permissions.

Since: 26.0.0

Parameters

Parameter Type Mandatory Description
items Array<PickerItemInfo> Yes Array of accessory integration options. Currently, only one option is supported. If multiple options are passed, the first one is used by default.
callback Callback<AccessEventInfo> Yes Callback used to return the accessory integration event information object. For details about the callback result, please refer to Event Callback Data Structure Description for the showAccessPicker API.

Returns

Type Description
number Operation result. The value 0 indicates success, and other values indicate failure.

Error codes:

For details about the error codes, please refer to Universal Error Codes.

ID Error Message
201 The permissions check failed.

Example:

import { accessoryAccessManager } from '@kit.AccessoryKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { image } from '@kit.ImageKit';

const TAG = 'Demo';

let briefDesc: accessoryAccessManager.StringResourceInfo = {
  'bundleName': 'com.huawei.accessoryDemo',
  'moduleName': 'entry',
  'stringResourceId': $r('app.string.EntryAbility_desc').id,
}
let wakeupInfo: accessoryAccessManager.WakeupInfo = {
  'wakeupType': accessoryAccessManager.WakeupType.START_ABILITY_BY_CALL,
  'bundleName': 'com.huawei.accessoryDemo',
  'abilityName': 'EntryAbility',
  'briefDesc': briefDesc
}

let shareBriefDesc: accessoryAccessManager.StringResourceInfo = {
  'bundleName': 'com.huawei.accessoryDemo',
  'moduleName': 'entry',
  'stringResourceId': $r('app.string.EntryAbility_desc').id,
}

let shareWakeupInfo: accessoryAccessManager.WakeupInfo = {
  'wakeupType': accessoryAccessManager.WakeupType.START_ABILITY_BY_CALL,
  'bundleName': 'com.huawei.accessoryDemo',
  'abilityName': 'EntryAbility',
  'briefDesc': shareBriefDesc
}

let serviceInfo: Array<accessoryAccessManager.ServiceInfo> = [
  {
    serviceName: accessoryAccessManager.ServiceName.PARTNER_APP_ACCESSORY_COLLABORATION,
    parameters: {
      'serviceName': wakeupInfo
    }
  },
  {
    serviceName: accessoryAccessManager.ServiceName.PARTNER_SHARE_SERVICE,
    parameters: {
      'bundleName': 'com.huawei.accessoryDemo',
    }
  }
];

// An empty image is created here. You can create the required image during development.
const color: ArrayBuffer = new ArrayBuffer(96);
let bufferArr: Uint8Array = new Uint8Array(color);
for (let i = 0; i < bufferArr.length; i++) {
  bufferArr[i] = 0x80;
}
let opts: image.InitializationOptions = {
  editable: true,
  pixelFormat: image.PixelMapFormat.BGRA_8888,
  size: { height: 4, width: 6 },
  alphaType: image.AlphaType.UNPREMUL
}
let pixelMap: image.PixelMap | undefined = undefined;
image.createPixelMap(color, opts).then((srcPixelMap: image.PixelMap) => {
  pixelMap = srcPixelMap;
})

let items: Array<accessoryAccessManager.PickerItemInfo> = [
  {
    discoveryType: accessoryAccessManager.DiscoveryType.PARTNER_BLE_CONNECT,
    hasScreen: true,
    bleAddress: 'XX:XX:XX:XX:XX:XX',
    bleMtuSize: 1,
    productId: 'XXXXX',
    subProductId: '',
    displayName: 'xxxx',
    displayImage: pixelMap,
    requestAttachServiceInfo: serviceInfo
  },
  {
    discoveryType: accessoryAccessManager.DiscoveryType.PARTNER_BLE_CONNECT,
    hasScreen: true,
    bleAddress: 'XX:XX:XX:XX:XX:XX',
    bleMtuSize: 1,
    productId: 'XXXXX',
    subProductId: '',
    displayName: 'xxxx',
    displayImage: pixelMap,
    requestAttachServiceInfo: serviceInfo
  }
];
let accessoryManager: accessoryAccessManager.AccessManager = new accessoryAccessManager.AccessManager();
let result: number = accessoryManager.showAccessPicker(items, (event: accessoryAccessManager.AccessEventInfo) => {
  if (!event) {
    hilog.error(0x0000, TAG, `showAccessPicker failed`);
    return;
  }
  hilog.info(0x0000, TAG, `showAccessPicker callback result: ${event.accessEvent}`);
});
hilog.info(0x0000, TAG, `showAccessPicker result: ${result}`);

[h2]modifyDisplayName

modifyDisplayName(accessoryId: string, displayName: string): number

Changes the display name of an accessory.

System capability: SystemCapability.Collaboration.Accessory.AccessManager

Model restriction: This API can be used only in the stage model.

Required permissions: ohos.permission.ALLOW_ACCESSORY_ACCESS. This capability is available only with this permission. For details about how to apply for the required permission, please refer to Requesting Restricted Permissions.

Since: 26.0.0

Parameters

Parameter Type Mandatory Description
accessoryId string Yes Accessory ID.
displayName string Yes Display name.

Returns

Type Description
number Renaming result. The value 0 indicates that the renaming succeeds, while other values indicate failure.

Error codes:

For details about the error codes, please refer to Universal Error Codes.

ID Error Message
201 The permissions check failed.

Example:

import { accessoryAccessManager } from '@kit.AccessoryKit';
import { hilog } from '@kit.PerformanceAnalysisKit';

const TAG = 'Demo';

let deviceId = 'deviceIdTest';
let deviceName = 'deviceNameTest';
let accessoryManager: accessoryAccessManager.AccessManager = new accessoryAccessManager.AccessManager();
let result: number = accessoryManager.modifyDisplayName(deviceId, deviceName);
hilog.info(0x0000, TAG, `modifyDisplayName code: ${result}`);

[h2]queryAttachedService

queryAttachedService(): Array<AttachServiceInfo>

Queries the information about associated accessories and services.

System capability: SystemCapability.Collaboration.Accessory.AccessManager

Model restriction: This API can be used only in the stage model.

Required permissions: ohos.permission.ALLOW_ACCESSORY_ACCESS. This capability is available only with this permission. For details about how to apply for the required permission, please refer to Requesting Restricted Permissions.

Since: 26.0.0

Returns

Type Description
Array<AttachServiceInfo> Array of the information about services that have been successfully attached.

Error codes:

For details about the error codes, please refer to Universal Error Codes.

ID Error Message
201 The permissions check failed.

Example:

import { accessoryAccessManager } from '@kit.AccessoryKit';

let accessoryManager: accessoryAccessManager.AccessManager = new accessoryAccessManager.AccessManager();
let resultArr: Array<accessoryAccessManager.AttachServiceInfo> = accessoryManager.queryAttachedService();

[h2]detachService

detachService(attachId: number, callback: Callback<DetachServiceEvent>): number

Removes an associated accessory or its associated service information. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Collaboration.Accessory.AccessManager

Model restriction: This API can be used only in the stage model.

Required permissions: ohos.permission.ALLOW_ACCESSORY_ACCESS. This capability is available only with this permission. For details about how to apply for the required permission, please refer to Requesting Restricted Permissions.

Since: 26.0.0

Parameters

Parameter Type Mandatory Description
attachId number Yes Attachment ID.
callback Callback<DetachServiceEvent> Yes Callback used to return the status of accessory or service removal.

Returns

Type Description
number Operation result. The value 0 indicates success, and other values indicate failure.

Error codes:

For details about the error codes, please refer to Universal Error Codes.

ID Error Message
201 The permissions check failed.

Example:

import { accessoryAccessManager } from '@kit.AccessoryKit';
import { hilog } from '@kit.PerformanceAnalysisKit';

const TAG = 'Demo';

let accessoryManager: accessoryAccessManager.AccessManager = new accessoryAccessManager.AccessManager();
let attachId = 1;
let result: number = accessoryManager.detachService(attachId, (event: accessoryAccessManager.DetachServiceEvent) => {
  if (!event) {
    hilog.error(0x0000, TAG, `detachService failed`);
    return;
  }
  hilog.info(0x0000, TAG, `detachService event: ${event}`);
});
hilog.info(0x0000, TAG, `detachService result: ${result}`);

ChannelType

Enumerates the types of channels used for accessory connection.

System capability: SystemCapability.Collaboration.Accessory.AccessManager

Model restriction: This API can be used only in the stage model.

Since: 26.0.0

Name Value Description
PARTNER_WIFI_CHANNEL 1 Wi-Fi channel.

ChannelEvent

Enumerates the states of a channel used for accessory connection.

System capability: SystemCapability.Collaboration.Accessory.AccessManager

Model restriction: This API can be used only in the stage model.

Since: 26.0.0

Name Value Description
CONNECTING 0 Connecting.
CONNECTED 1 Connected.
CONNECT_FAIL 2 Connection failed.
DISCONNECTED 3 Disconnected.

ChannelEventInfo

Defines the information about a channel used for accessory connection.

System capability: SystemCapability.Collaboration.Accessory.AccessManager

Model restriction: This API can be used only in the stage model.

Since: 26.0.0

Name Type Read-Only Optional Description
channelEvent ChannelEvent No No Channel event.
attachId number No No Attachment ID.
channelType ChannelType No No Channel type.
ip string No Yes IP address.
errorCode number No Yes Connection event result code.
reason string No Yes Error cause.

ConnectRequestInfo

Defines the connection request information.

System capability: SystemCapability.Collaboration.Accessory.AccessManager

Model restriction: This API can be used only in the stage model.

Since: 26.0.0

Name Type Read-Only Optional Description
attachId number No No Attachment ID.
channelType ChannelType No No Channel type.
serviceDesc StringResourceInfo No No Service description.

ConnectManager

Represents the connection manager.

System capability: SystemCapability.Collaboration.Accessory.AccessManager

Model restriction: This API can be used only in the stage model.

Since: 26.0.0

[h2]registerConnectListener

registerConnectListener(attachId: number, stateCallback: Callback<ChannelEventInfo>): number

Registers a connection event listener that triggers a callback when the accessory is connected or disconnected. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Collaboration.Accessory.AccessManager

Model restriction: This API can be used only in the stage model.

Required permissions: ohos.permission.ALLOW_ACCESSORY_ACCESS. This capability is available only with this permission. For details about how to apply for the required permission, please refer to Requesting Restricted Permissions.

Since: 26.0.0

Parameters

Parameter Type Mandatory Description
attachId number Yes Attachment ID.
stateCallback Callback<ChannelEventInfo> Yes Callback used to return the channel information object.

Returns

Type Description
number Listener registration result. The value 0 indicates a successful registration, and other values indicate failure.

Error codes:

For details about the error codes, please refer to Universal Error Codes.

ID Error Message
201 The permissions check failed.

Example:

import { accessoryAccessManager } from '@kit.AccessoryKit';
import { hilog } from '@kit.PerformanceAnalysisKit';

const TAG = 'Demo';

let attachId = 0;
let connectManager: accessoryAccessManager.ConnectManager = new accessoryAccessManager.ConnectManager();
let result : number = connectManager.registerConnectListener(attachId, (event: accessoryAccessManager.ChannelEventInfo) => {
  hilog.info(0x0000, TAG, `registerConnectListener event result: ${JSON.stringify(event)}`);
  if (!event) {
    hilog.error(0x0000, TAG, `registerConnectListener callback event undefined`);
    return;
  }
  if ((event.channelEvent as accessoryAccessManager.ChannelEvent) ==
    accessoryAccessManager.ChannelEvent.CONNECTING) {
    attachId = event.attachId;
  }
  if ((event.channelEvent as accessoryAccessManager.ChannelEvent) ==
    accessoryAccessManager.ChannelEvent.CONNECTED) {
    attachId = event.attachId;
  }
});
hilog.info(0x0000, TAG, `registerConnectListener code: ${result}`);

[h2]unregisterConnectListener

unregisterConnectListener(attachId: number): number

Unregisters a connection event listener, after which the listener will no longer receive notifications about the accessory connection status.

System capability: SystemCapability.Collaboration.Accessory.AccessManager

Model restriction: This API can be used only in the stage model.

Required permissions: ohos.permission.ALLOW_ACCESSORY_ACCESS. This capability is available only with this permission. For details about how to apply for the required permission, please refer to Requesting Restricted Permissions.

Since: 26.0.0

Parameters

Parameter Type Mandatory Description
attachId number Yes Attachment ID.

Returns

Type Description
number Listener unregistration result. The value 0 indicates a successful unregistration, and other values indicate failure.

Error codes:

For details about the error codes, please refer to Universal Error Codes.

ID Error Message
201 The permissions check failed.

Example:

import { accessoryAccessManager } from '@kit.AccessoryKit';
import { hilog } from '@kit.PerformanceAnalysisKit';

const TAG = 'Demo';

let attachId = 1;
let connectManager: accessoryAccessManager.ConnectManager = new accessoryAccessManager.ConnectManager();
let result: number = connectManager.unregisterConnectListener(attachId);
hilog.info(0x0000, TAG, `unregisterConnectListener result: ${JSON.stringify(result)}`);

[h2]connect

connect(connectRequestInfo: ConnectRequestInfo): number

Connects to an accessory. The connection status is notified through the callback registered by calling registerConnectListener.

System capability: SystemCapability.Collaboration.Accessory.AccessManager

Model restriction: This API can be used only in the stage model.

Required permissions: ohos.permission.ALLOW_ACCESSORY_ACCESS. This capability is available only with this permission. For details about how to apply for the required permission, please refer to Requesting Restricted Permissions.

Since: 26.0.0

Parameters

Parameter Type Mandatory Description
connectRequestInfo ConnectRequestInfo Yes Connection request information.

Returns

Type Description
number Result of establishing a connection with the accessory. The value 0 indicates that the connection succeeds, and other values indicate failure.

Error codes:

For details about the error codes, please refer to Universal Error Codes.

ID Error Message
201 The permissions check failed.

Example:

import { accessoryAccessManager } from '@kit.AccessoryKit';
import { hilog } from '@kit.PerformanceAnalysisKit';

const TAG = 'Demo';

let connectAttachId = 1;
let connectManager: accessoryAccessManager.ConnectManager = new accessoryAccessManager.ConnectManager();
let shareDesc: accessoryAccessManager.StringResourceInfo = {
  bundleName: "com.huawei.accessoryDemo",
  moduleName: "entry",
  stringResourceId: $r('app.string.EntryAbility_desc').id
};
let info: accessoryAccessManager.ConnectRequestInfo =
  {
    attachId: connectAttachId,
    channelType: accessoryAccessManager.ChannelType.PARTNER_WIFI_CHANNEL,
    serviceDesc: shareDesc
  };
let result: number = connectManager.connect(info);
hilog.info(0x0000, TAG, `connect result: ${JSON.stringify(result)}`);

[h2]disconnect

disconnect(attachId: number): number

Disconnects from an accessory. The disconnection status is notified through the callback registered by calling registerConnectListener.

System capability: SystemCapability.Collaboration.Accessory.AccessManager

Model restriction: This API can be used only in the stage model.

Required permissions: ohos.permission.ALLOW_ACCESSORY_ACCESS. This capability is available only with this permission. For details about how to apply for the required permission, please refer to Requesting Restricted Permissions.

Since: 26.0.0

Parameters

Parameter Type Mandatory Description
attachId number Yes Attachment ID.

Returns

Type Description
number Result of disconnecting from the accessory. The value 0 indicates that the disconnection succeeds, and other values indicate failure.

Error codes:

For details about the error codes, please refer to Universal Error Codes.

ID Error Message
201 The permissions check failed.

Example:

import { accessoryAccessManager } from '@kit.AccessoryKit';
import { hilog } from '@kit.PerformanceAnalysisKit';

const TAG = 'Demo';

let attachId = 1;
let connectManager: accessoryAccessManager.ConnectManager = new accessoryAccessManager.ConnectManager();
let result: number = connectManager.disconnect(attachId);
hilog.info(0x0000, TAG, `disconnect result: ${JSON.stringify(result)}`);