openharmony 鸿蒙 js-apis-mechanicManager-sys

2026-08-25 浏览 (1)

@ohos.distributedHardware.mechanicManager (Mechanic Manager) (System API)

The mechanicManager module provides the mechanic device interaction capabilities, such as connection management, device control, and monitoring.

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.

  • This topic describes only the system APIs provided by the module. For details about its public APIs, see @ohos.distributedHardware.mechanicManager (Mechanic Manager).

Modules to Import

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

mechanicManager.setUserOperation

setUserOperation(operation: Operation, mac: string, params: string): void

Sets the the user operation, such as connection or disconnection.

Required permissions: ohos.permission.CONNECT_MECHANIC_HARDWARE

System capability: SystemCapability.Mechanic.Core

System API: This is a system API.

Parameters

NameTypeMandatoryDescription
operationOperationYesOperation type.
macstringYesMAC address of the mechanic device.
paramsstringYesOperation parameters.

Error codes

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

IDError Message
201Permission denied.
202Not system application.
33300001Service exception.

Example

console.info('User operate');
mechanicManager.setUserOperation(mechanicManager.Operation.CONNECT, "58:51:9e:e7:79:6d", "operatingParams");
console.info('User operation was successful');

mechanicManager.setCameraTrackingLayout

setCameraTrackingLayout(trackingLayout: CameraTrackingLayout): void

Sets the camera tracking layout of the current mechanic device.

System capability: SystemCapability.Mechanic.Core

System API: This is a system API.

Parameters

NameTypeMandatoryDescription
trackingLayoutCameraTrackingLayoutYesCamera tracking layout.

Error codes

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

IDError Message
202Not system application.
33300001Service exception.
33300002Device not connected.
33300003Feature not supported.

Example

console.info('Set layout');
mechanicManager.setCameraTrackingLayout(mechanicManager.CameraTrackingLayout.LEFT);
console.info('Set layout successful');

mechanicManager.rotate

rotate(mechId: number, angles: RotationAngles, duration: number): Promise<Result>

Rotates a mechanic device to the relative angles. This API uses a promise to return the result.

System capability: SystemCapability.Mechanic.Core

System API: This is a system API.

Parameters

NameTypeMandatoryDescription
mechIdnumberYesID of the mechanic device.
anglesRotationAnglesYesRotation angle relative to the current position.
durationnumberYesRotation duration, in milliseconds.

Return value

TypeDescription
Promise<Result>Promise used to return the rotation result.

Error codes

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

IDError Message
202Not system application.
33300001Service exception.
33300002Device not connected.

Example

console.info('Start rotate');
let degree: mechanicManager.RotationAngles = {
  yaw: 0.1 * Math.PI,
  roll: 0.0,
  pitch: 0.0
}
mechanicManager.rotate(0, degree, 500)
  .then((result: mechanicManager.Result) => {
    console.info(`'Rotate result:' ${result}`);
  });
console.info('End rotation');

mechanicManager.rotateToEulerAngles

rotateToEulerAngles(mechId: number, angles: EulerAngles, duration: number): Promise<Result>

Rotates a mechanic device to the absolute angles. This API uses a promise to return the result.

System capability: SystemCapability.Mechanic.Core

System API: This is a system API.

Parameters

NameTypeMandatoryDescription
mechIdnumberYesID of the mechanic device.
anglesEulerAnglesYesAbsolute angle.
durationnumberYesRotation duration, in milliseconds.

Return value

TypeDescription
Promise<Result>Promise used to return the rotation result.

Error codes

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

IDError Message
202Not system application.
33300001Service exception.
33300002Device not connected.

Example

let degree: mechanicManager.EulerAngles = {
  yaw: 0.9 * Math.PI,
  roll: 0.9 * Math.PI,
  pitch: 0.9 * Math.PI
}
mechanicManager.rotateToEulerAngles(0, degree, 500)
  .then((result: mechanicManager.Result) => {
    console.info(`'Rotate result:' ${result}`);
  });
console.info('End rotation');

mechanicManager.getMaxRotationTime

getMaxRotationTime(mechId: number): number

Obtains the maximum duration for continuous rotation of a mechanic device.

System capability: SystemCapability.Mechanic.Core

System API: This is a system API.

Parameters

NameTypeMandatoryDescription
mechIdnumberYesID of the mechanic device.

Return value

TypeDescription
numberMaximum rotation duration, in milliseconds.

Error codes

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

IDError Message
202Not system application.
33300001Service exception.
33300002Device not connected.

Example

console.info('Query maximum rotation time');
let maxTime = mechanicManager.getMaxRotationTime(0);
console.info(`'Query maximum rotation time successful, maximum time:' ${maxTime}`);

mechanicManager.getMaxRotationSpeed

getMaxRotationSpeed(mechId: number): RotationSpeed

Obtains the maximum rotation speed of a mechanic device.

System capability: SystemCapability.Mechanic.Core

System API: This is a system API.

Parameters

NameTypeMandatoryDescription
mechIdnumberYesID of the mechanic device.

Return value

TypeDescription
RotationSpeedMaximum rotation speed (absolute value).

Error codes

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

IDError Message
202Not system application.
33300001Service exception.
33300002Device not connected.

Example

console.info('Query rotation speed');
let speedLimit: mechanicManager.RotationSpeed = mechanicManager.getMaxRotationSpeed(0);
console.info(`'Query rotation speed successful, speed limit information:' ${speedLimit}`);

mechanicManager.rotateBySpeed

rotateBySpeed(mechId: number, speed: RotationSpeed, duration: number): Promise<Result>

Rotates the current body at the specified speed. This API uses a promise to return the result.

System capability: SystemCapability.Mechanic.Core

System API: This is a system API.

Parameters

NameTypeMandatoryDescription
mechIdnumberYesID of the mechanic device.
speedRotationSpeedYesSpecified rotation speed. If the specified value is greater than the value returned by getMaxRotationSpeed, the value returned by getMaxRotationSpeed is used by default.
durationnumberYesRotation duration, in milliseconds. If the specified value is greater than the value returned by getMaxRotationTime, the value returned by getMaxRotationTime is used by default.

Return value

TypeDescription
Promise<Result>Promise used to return the rotation result.

Error codes

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

IDError Message
202Not system application.
33300001Service exception.
33300002Device not connected.

Example

console.info('Start rotate');
let degree: mechanicManager.RotationSpeed = {
  yawSpeed: 3 * Math.PI,
  rollSpeed: 3 * Math.PI,
  pitchSpeed: 3 * Math.PI
}
mechanicManager.rotateBySpeed(0, degree, 500)
  .then((result) => {
    console.info(`'Rotate result:' ${result}`);
  });
console.info('Rotate finish');

mechanicManager.stopMoving

stopMoving(mechId: number): Promise<void>

Stops movement of a mechanic device. This API uses a promise to return the result.

System capability: SystemCapability.Mechanic.Core

System API: This is a system API.

Parameters

NameTypeMandatoryDescription
mechIdnumberYesID of the mechanic device.

Return value

TypeDescription
Promise<void>Promise that returns no value.

Error codes

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

IDError Message
202Not system application.
33300001Service exception.
33300002Device not connected.

Example

console.info('Stop moving');
mechanicManager.stopMoving(0)
  .then(() => {
    console.info('Get stop complete');
  });
console.info('Stop succeeded');

mechanicManager.getCurrentAngles

getCurrentAngles(mechId: number): EulerAngles

Obtains the maximum rotation angle of a mechanic device relative to the initial position.

NOTE

The coordinates of the initial position are (0, 0, 0).

System capability: SystemCapability.Mechanic.Core

System API: This is a system API.

Parameters

NameTypeMandatoryDescription
mechIdnumberYesID of the mechanic device.

Return value

TypeDescription
EulerAnglesCurrent rotation angle of the mechanic device.

Error codes

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

IDError Message
202Not system application.
33300001Service exception.
33300002Device not connected.

Example

console.info('Query current location');
let currentAngles: mechanicManager.EulerAngles = mechanicManager.getCurrentAngles(0);
console.info(`'Query current location, location:' ${currentAngles}`);

mechanicManager.getRotationLimits

getRotationLimits(mechId: number): RotationLimits

Obtains the maximum rotation angle of a mechanic device relative to the initial position.

System capability: SystemCapability.Mechanic.Core

System API: This is a system API.

Parameters

NameTypeMandatoryDescription
mechIdnumberYesID of the mechanic device.

Return value

TypeDescription
RotationLimitsMaximum rotation angle.

Error codes

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

IDError Message
202Not system application.
33300001Service exception.
33300002Device not connected.

Example

console.info('Query rotation limit information');
let degreeLimit: mechanicManager.RotationLimits = mechanicManager.getRotationLimits(0);
console.info(`'Query the rotation limit information successfully, limit information:' ${degreeLimit}`);

mechanicManager.on('rotationAxesStatusChange')

on(type: 'rotationAxesStatusChange', callback: Callback<RotationAxesStateChangeInfo>): void

Registers a callback listener for rotation axis state change events. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Mechanic.Core

System API: This is a system API.

Parameters

NameTypeMandatoryDescription
type'rotationAxesStatusChange'YesEvent type. The value rotationAxesStatusChange indicates a rotation axis state change.
callbackCallback<RotationAxesStateChangeInfo>YesCallback used to return the rotation axis state change.

Error codes

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

IDError Message
202Not system application.
33300001Service exception.

Example

console.info('Register Axis Status listener');
mechanicManager.on("rotationAxesStatusChange", (result: mechanicManager.RotationAxesStateChangeInfo) => {
  console.info(`'result:' ${result}`);
});
console.info('Successful registration');

mechanicManager.off('rotationAxesStatusChange')

off(type: 'rotationAxesStatusChange', callback?: Callback<RotationAxesStateChangeInfo>): void

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

System capability: SystemCapability.Mechanic.Core

System API: This is a system API.

Parameters

NameTypeMandatoryDescription
type'rotationAxesStatusChange'YesEvent type. The value rotationAxesStatusChange indicates a rotation axis state change.
callbackCallback<RotationAxesStateChangeInfo>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 and Universal Error Codes.

IDError Message
202Not system application.
33300001Service exception.

Example

console.info('Unregister Axis Status listener');
mechanicManager.off("rotationAxesStatusChange", (result: mechanicManager.RotationAxesStateChangeInfo) => {
  console.info(`'result:' ${result}`);
});
console.info('Unregister successfully');

mechanicManager.getRotationAxesStatus

getRotationAxesStatus(mechId: number): RotationAxesStatus

Obtains the rotation axis status of the current mechanic device.

System capability: SystemCapability.Mechanic.Core

System API: This is a system API.

Parameters

NameTypeMandatoryDescription
mechIdnumberYesID of the mechanic device.

Return value

TypeDescription
RotationAxesStatusRotation axis status.

Error codes

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

IDError Message
202Not system application.
33300001Service exception.
33300002Device not connected.

Example

console.info('Query the rotation axis status');
let axisStatus: mechanicManager.RotationAxesStatus = mechanicManager.getRotationAxesStatus(0);
console.info(`'Query the rotation axis status successfully, axis state:' ${axisStatus}`);

mechanicManager.searchTarget21+

searchTarget(target: TargetInfo, params: SearchParams): Promise<SearchResult>

Rotates the mechanical body 360 degrees to search for the target. This API uses a promise to return the result.

System capability: SystemCapability.Mechanic.Core

System API: This is a system API.

Parameters

NameTypeMandatoryDescription
targetTargetTypeYesTarget face information.
paramsSearchParamsYesSearch direction.

Return value

TypeDescription
Promise<SearchResult>Promise used to return the search result.

Error codes

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

IDError Message
202Not system application.
33300001Service exception.
33300002Device not connected.
33300003Feature not supported.
33300004Camera not opened.

Example

let targetInfo: mechanicManager.TargetInfo = {
    targetType: mechanicManager.TargetType.HUMAN_FACE
};
let searchParams: mechanicManager.SearchParams = {
    direction: mechanicManager.SearchDirection.DEFAULT
}
mechanicManager.searchTarget(targetInfo,
    searchParams).then((searchResult) => {
    console.info(`'result:' ${searchResult}`);
});

RotationAngles

Defines the rotation angle relative to the current position.

System capability: SystemCapability.Mechanic.Core

System API: This is a system API.

NameTypeRead-OnlyOptionalDescription
yawnumberNoYesYaw angle, in radians. The value range is [-2*Math.PI, 2*Math.PI].
rollnumberNoYesRoll angle, in radians. The value range is [-2*Math.PI, 2*Math.PI].
pitchnumberNoYesPitch angle, in radians. The value range is [-2*Math.PI, 2*Math.PI].

EulerAngles

Defines the absolute Euler angle relative to the initial position.

System capability: SystemCapability.Mechanic.Core

System API: This is a system API.

NameTypeRead-OnlyOptionalDescription
yawnumberNoYesYaw angle, in radians. The value range is [-2*Math.PI, 2*Math.PI].
rollnumberNoYesRoll angle, in radians. The value range is [-2*Math.PI, 2*Math.PI].
pitchnumberNoYesPitch angle, in radians. The value range is [-2*Math.PI, 2*Math.PI].

RotationSpeed

Defines the rotation speed. A negative value indicates clockwise rotation, and a positive value indicates anticlockwise rotation.

System capability: SystemCapability.Mechanic.Core

System API: This is a system API.

NameTypeRead-OnlyOptionalDescription
yawSpeednumberNoYesYaw speed, in radians per second.
rollSpeednumberNoYesRoll speed, in radians per second.
pitchSpeednumberNoYesPitch speed, in radians per second.

RotationLimits

Defines the limit of the rotation angle relative to the initial position.

System capability: SystemCapability.Mechanic.Core

System API: This is a system API.

NameTypeRead-OnlyOptionalDescription
negativeYawMaxnumberNoNoMaximum yaw rotation angle (negative direction). The value range is [-2*Math.PI, 0], in radians. If the value is less than or equal to -2*Math.PI, there is no limitation on the rotation angle.
positiveYawMaxnumberNoNoMaximum yaw rotation angle (positive direction). The value range is [0, 2*Math.PI], in radians. If the value is greater than or equal to 2*Math.PI, there is no limitation on the rotation angle.
negativeRollMaxnumberNoNoMaximum roll rotation angle (negative direction), in radians. The value range is [–2 x Math.PI, 0]. If the value is less than or equal to -2*Math.PI, there is no limitation on the rotation angle.
positiveRollMaxnumberNoNoMaximum roll rotation angle (positive direction), in radians. The value range is [0, 2 x Math.PI]. If the value is greater than or equal to 2*Math.PI, there is no limitation on the rotation angle.
negativePitchMaxnumberNoNoMaximum pitch rotation angle (negative direction), in radians. The value range is [–2 x Math.PI, 0]. If the value is less than or equal to -2*Math.PI, there is no limitation on the rotation angle.
positivePitchMaxnumberNoNoMaximum pitch rotation angle (positive direction), in radians. The value range is [0, 2 x Math.PI]. If the value is greater than or equal to 2*Math.PI, there is no limitation on the rotation angle.

RotationAxesStatus

Enumerates the rotation axis states.

System capability: SystemCapability.Mechanic.Core

System API: This is a system API.

NameTypeRead-OnlyOptionalDescription
yawEnabledbooleanNoNoWhether the yaw axis is enabled. The value true indicates that the yaw axis is enabled, and the value false indicates the opposite.
rollEnabledbooleanNoNoWhether the roll axis is enabled. The value true indicates that the roll axis is enabled, and the value false indicates the opposite.
pitchEnabledbooleanNoNoWhether the pitch axis is enabled. The value true indicates that the pitch axis is enabled, and the value false indicates the opposite.
yawLimitedRotationAxisLimitedNoYesYaw axis limited.
rollLimitedRotationAxisLimitedNoYesRoll axis limited.
pitchLimitedRotationAxisLimitedNoYesPitch axis limited.

RotationAxisLimited

Enumerates the rotation axis limit states.

System capability: SystemCapability.Mechanic.Core

System API: This is a system API.

NameValueDescription
NOT_LIMITED0Unlimited.
NEGATIVE_LIMITED1Rotation is restricted in the negative direction and allowed in the positive direction.
POSITIVE_LIMITED2Rotation is restricted in the positive direction and allowed in the negative direction.

RotationAxesStateChangeInfo

Defines the rotation axis state change information.

System capability: SystemCapability.Mechanic.Core

System API: This is a system API.

NameTypeRead-OnlyOptionalDescription
mechIdnumberNoNoID of the mechanic device.
statusRotationAxesStatusNoNoRotation axis status.

Operation

Enumerates user operations.

System capability: SystemCapability.Mechanic.Core

System API: This is a system API.

NameValueDescription
CONNECT0Connection.
DISCONNECT1Disconnection.

Result

Enumerates the rotation results.

System capability: SystemCapability.Mechanic.Core

System API: This is a system API.

NameValueDescription
COMPLETED0Completed.
INTERRUPTED1Interrupted.
LIMITED2Restricted by the maximum rotation angle.
TIMEOUT3Operation timeout.
SYSTEM_ERROR100System error.

TargetType21+

Enumerates the target face information.

System capability: SystemCapability.Mechanic.Core

System API: This is a system API.

NameTypeValueDescription
HUMAN_FACEint0Target face information.

SearchDirection21+

Default search direction.

System capability: SystemCapability.Mechanic.Core

System API: This is a system API.

NameTypeValueDescription
DEFAULTint0Default direction.
LEFTWARDint1Leftward, that is, clockwise.
RIGHTWARDint2Rightward, that is, counterclockwise.

TargetInfo21+

Information about the search target.

System capability: SystemCapability.Mechanic.Core

System API: This is a system API.

NameTypeRead-OnlyOptionalDescription
targetTypeTargetTypeNoNoInformation about the search target.

SearchParams21+

Specifies the search direction.

System capability: SystemCapability.Mechanic.Core

System API: This is a system API.

NameTypeRead-OnlyOptionalDescription
directionSearchDirectionNoNoSearch direction.

SearchResult21+

Displays the execution result of the search command.

System capability: SystemCapability.Mechanic.Core

System API: This is a system API.

NameTypeRead-OnlyOptionalDescription
targetCountnumberNoNoNumber of found targets.

你可能感兴趣的鸿蒙文章

openharmony 鸿蒙 Readme-EN

openharmony 鸿蒙 js-apis-mechanicManager

openharmony 鸿蒙 errorcode-mechanic

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