目录

Gyroscope

has.startGyroscope

has.startGyroscope(Object object)

Starts listening for gyroscope data.

Since: 1.0.4

Required permission: ohos.permission.GYROSCOPE, which needs to be declared in module.json5

Parameters

The input parameter is an object containing the following fields.

Parameter Type Default Value Mandatory Description
interval string normal No

Execution frequency for the callback of gyroscope changes.

- game: execution frequency for the callback of game updates, which is about 20 ms per time.

- ui: execution frequency for the callback of UI updates, which is about 60 ms per time.

- normal: execution frequency for the common callback, which is about 200 ms per time.

success function - No Callback triggered when the API call is successful.
fail function - No Callback triggered when the API call fails.
complete function - No Callback triggered when the API call is complete, regardless of success or failure.

Error codes

Error Code Error Message
102 interval is invalid.
12001901 startGyroscope error.

Example:

has.startGyroscope({
  success: () => {
    console.info('startGyroscope success');
  },
  fail: (err) => {
    console.error('startGyroscope fail', err);
  },
  complete: (res) => {
    console.info('startGyroscope complete', res);
  }
});

has.stopGyroscope

has.stopGyroscope(Object object)

Stops listening for gyroscope data.

Since: 1.0.4

Required permission: ohos.permission.GYROSCOPE, which needs to be declared in module.json5

Parameters

The input parameter is an object containing the following fields.

Parameter Type Mandatory Description
success function No Callback triggered when the API call is successful.
fail function No Callback triggered when the API call fails.
complete function No Callback triggered when the API call is complete, regardless of success or failure.

Error codes

Error Code Error Message
12002001 stopGyroscope error.

Example:

has.stopGyroscope({
  success: () => {
    console.info('stopGyroscope success');
  },
  fail: (err) => {
    console.error('stopGyroscope fail', err);
  },
  complete: (res) => {
    console.info('stopGyroscope complete', res);
  }
});

has.onGyroscopeChange

has.onGyroscopeChange(function callback)

Enables listening for gyroscope change events.

Since: 1.0.4

Parameters

Parameter Type Mandatory Description
callback function Yes Callback for the gyroscope change event.

Return value of callback

Parameter Type Description
x number Angular velocity on the x-axis.
y number Angular velocity on the y-axis.
z number Angular velocity on the z-axis.

Example:

has.onGyroscopeChange(function(res) {
  console.info('onGyroscopeChange callback triggered', res);
});

has.offGyroscopeChange

has.offGyroscopeChange(function callback)

Removes the listener function for gyroscope data change events.

Since: 1.0.4

Parameters

Parameter Type Mandatory Description
callback function No Listener function passed by has.onGyroscopeChange. If this parameter is not passed, all listener functions will be removed.

Example:

const onGyroscopeChangeFn = function(res) {
  console.info('onGyroscopeChange callback triggered', res);
};
has.onGyroscopeChange(onGyroscopeChangeFn);
has.offGyroscopeChange(onGyroscopeChangeFn);