openharmony 鸿蒙 js-apis-mechanicManager

2026-08-25 浏览 (1)

@ohos.distributedHardware.mechanicManager (Mechanic Manager)

The mechanicManager module provides the mechanic device interaction capabilities, such as listening for the device connection status, tracking control, and listening for the tracking status.

NOTE

The initial APIs of this module are supported since API version 20. Newly added APIs will be marked with a superscript to indicate their earliest API version.

Modules to Import

import { mechanicManager } from '@kit.MechanicKit';

mechanicManager.on('attachStateChange')

on(type: 'attachStateChange', callback: Callback<AttachStateChangeInfo>): void

Registers a callback listener for connection state changes. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Mechanic.Core

Parameters

NameTypeMandatoryDescription
type'attachStateChange'YesEvent type. The value attachStateChange indicates a connection state change.
callbackCallback<AttachStateChangeInfo>YesCallback used to return the connection state change of the mechanic device.

Error codes

For details about the error codes, see Mechanic Manager Error Codes.

IDError Message
33300001Service exception.

Example

let callback = (result: mechanicManager.AttachStateChangeInfo) => {
  console.info(`'callback result:' ${result}`);
};

console.info('Register');
mechanicManager.on("attachStateChange", callback);
console.info('Succeeded in registering callback.');

mechanicManager.off('attachStateChange')

off(type: 'attachStateChange', callback?: Callback<AttachStateChangeInfo>): void

Unregisters the callback listener for connection state changes. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Mechanic.Core

Parameters

NameTypeMandatoryDescription
type'attachStateChange'YesEvent type. The value attachStateChange indicates a connection state change.
callbackCallback<AttachStateChangeInfo>NoCallback used to return the connection state change of the mechanic device.

Error codes

For details about the error codes, see Mechanic Manager Error Codes.

IDError Message
33300001Service exception.

Example

let callback = (result: mechanicManager.AttachStateChangeInfo) => {
  console.info(`'callback result:' ${result}`);
};

console.info('Unregister');
mechanicManager.off("attachStateChange", callback);
console.info('Succeeded in unregistering callback.');

mechanicManager.getAttachedMechDevices

getAttachedMechDevices(): MechInfo[]

Obtain the list of connected mechanic devices.

System capability: SystemCapability.Mechanic.Core

Return value

TypeDescription
MechInfo[]List of connected mechanic devices.

Error codes

For details about the error codes, see Mechanic Manager Error Codes.

IDError Message
33300001Service exception.

Example

console.info('Query device list');
let mechanicInfos = mechanicManager.getAttachedMechDevices();
console.info(`'device list:' ${mechanicInfos}`);

mechanicManager.setCameraTrackingEnabled

setCameraTrackingEnabled(isEnabled: boolean): void

Enables or disables camera tracking for the current mechanic device.

System capability: SystemCapability.Mechanic.Core

Parameters

NameTypeMandatoryDescription
isEnabledbooleanYesWhether to enable camera tracking. The value true means to enable camera tracking, and the value false means the opposite.

Error codes

For details about the error codes, see Mechanic Manager Error Codes.

IDError Message
33300001Service exception.
33300002Device not connected.
33300003Feature not supported.

Example

console.info('Enable tracing');
mechanicManager.setCameraTrackingEnabled(true);
console.info('Succeeded in enabling tracking.');

mechanicManager.getCameraTrackingEnabled

getCameraTrackingEnabled(): boolean

Checks whether camera tracking is enabled for the current mechanic device.

System capability: SystemCapability.Mechanic.Core

Return value

TypeDescription
booleanWhether camera tracking is enabled. The value true indicates that camera tracking is enabled, and the value false indicates the opposite.

Error codes

For details about the error codes, see Mechanic Manager Error Codes.

IDError Message
33300001Service exception.
33300002Device not connected.

Example

console.info('Get tracking status');
let enabled = mechanicManager.getCameraTrackingEnabled();
console.info(`'current tracking status:' ${enabled}`);

mechanicManager.on('trackingStateChange')

on(type: 'trackingStateChange', callback: Callback<TrackingEventInfo>): void

Registers a callback listener for tracking state changes. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Mechanic.Core

Parameters

NameTypeMandatoryDescription
type'trackingStateChange'YesEvent type. The value trackingStateChange indicates a tracking state change.
callbackCallback<TrackingEventInfo>YesCallback used to return the tracking state change of the mechanic device.

Error codes

For details about the error codes, see Mechanic Manager Error Codes.

IDError Message
33300001Service exception.

Example

let callback = (result: mechanicManager.TrackingEventInfo) => {
  console.info(`'callback result:' ${result}`);
};

console.info('Register');
mechanicManager.on("trackingStateChange", callback);
console.info('Succeeded in registering callback.');

mechanicManager.off('trackingStateChange')

off(type: 'trackingStateChange', callback?: Callback<TrackingEventInfo>): void

Unregisters the callback listener for tracking state changes. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Mechanic.Core

Parameters

NameTypeMandatoryDescription
type'trackingStateChange'YesEvent type. The value trackingStateChange indicates a tracking state change.
callbackCallback<TrackingEventInfo>NoCallback to unregister. If this parameter is not specified, all callbacks for the specified type of events are unregistered.

Error codes

For details about the error codes, see Mechanic Manager Error Codes.

IDError Message
33300001Service exception.

Example

let callback = (result: mechanicManager.TrackingEventInfo) => {
  console.info(`'callback result:' ${result}`);
};

console.info('Unregister');
mechanicManager.off("trackingStateChange", callback);
console.info('Succeeded in unregistering callback.');

mechanicManager.getCameraTrackingLayout

getCameraTrackingLayout(): CameraTrackingLayout

Obtains the camera tracking layout of the current mechanic device.

System capability: SystemCapability.Mechanic.Core

Return value

TypeDescription
CameraTrackingLayoutCamera tracking layout of the current mechanic device.

Error codes

For details about the error codes, see Mechanic Manager Error Codes.

IDError Message
33300001Service exception.
33300002Device not connected.

Example

console.info('Query layout');
let layout = mechanicManager.getCameraTrackingLayout();
console.info(`'Succeeded in querying layout, current layout:' ${layout}`);

MechInfo

Defines information about the mechanic device.

System capability: SystemCapability.Mechanic.Core

NameTypeRead-OnlyOptionalDescription
mechIdnumberNoNoID of the mechanic device. The value is an integer greater than or equal to 0.
mechDeviceTypeMechDeviceTypeNoNoType of the mechanic device.
mechNamestringNoNoName of the mechanic device.

TrackingEventInfo

Defines the tracking event information.

System capability: SystemCapability.Mechanic.Core

NameTypeRead-OnlyOptionalDescription
eventTrackingEventNoNoTracking event.

AttachStateChangeInfo

Defines the connection state change information.

System capability: SystemCapability.Mechanic.Core

NameTypeRead-OnlyOptionalDescription
stateAttachStateNoNoDevice connection state.
mechInfoMechInfoNoNoDevice information.

TrackingEvent

Enumerates the camera tracking events.

System capability: SystemCapability.Mechanic.Core

NameValueDescription
CAMERA_TRACKING_USER_ENABLED0Camera tracking is enabled.
CAMERA_TRACKING_USER_DISABLED1Camera tracking is disabled.
CAMERA_TRACKING_LAYOUT_CHANGED2The camera tracking layout is changed.

MechDeviceType

Enumerates the mechanic device types.

System capability: SystemCapability.Mechanic.Core

NameValueDescription
GIMBAL_DEVICE0Portable gimbal device.

AttachState

Enumerates the device connection states.

System capability: SystemCapability.Mechanic.Core

NameValueDescription
ATTACHED0Device connected.
DETACHED1Device disconnected.

CameraTrackingLayout

Enumerates the camera tracking layouts.

System capability: SystemCapability.Mechanic.Core

NameValueDescription
DEFAULT0Default tracking layout.
LEFT1Left layout.
MIDDLE2Center layout.
RIGHT3Right layout.

你可能感兴趣的鸿蒙文章

openharmony 鸿蒙 js-apis-mechanicManager-sys

openharmony 鸿蒙 Readme-EN

openharmony 鸿蒙 errorcode-mechanic

  • 所属分类: 后端技术
  • 本文标签: 鸿蒙 软件
  • 版权声明: 本文链接 https://seaxiang.com/blog/OI5lufYe