目录

Accelerometer

has.startAccelerometer

has.startAccelerometer(Object object)

Starts listening for acceleration data.

Since: 1.0.4

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

Parameters

The input parameter is an object containing the following fields.

Name

Type

Default Value

Mandatory

Description

interval

string

normal

No

Execution frequency for the callback of device orientation 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 to be invoked when the API call is successful.

fail

function

-

No

Callback to be invoked when the API call fails.

complete

function

-

No

Callback to be invoked when the API call is complete, regardless of success or failure.

Example
has.startAccelerometer({
  success() {
    console.log('startAccelerometer success: ');
  },
  fail(res) {
    console.log('startAccelerometer fail: ', JSON.stringify(res));
  },
  complete() {
    console.log('startAccelerometer complete: ');
  }
});

has.stopAccelerometer

has.stopAccelerometer(Object object)

Stops listening for acceleration data.

Since: 1.0.4

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

Parameters

The input parameter is an object containing the following fields.

Name

Type

Mandatory

Description

success

function

No

Callback to be invoked when the API call is successful.

fail

function

No

Callback to be invoked when the API call fails.

complete

function

No

Callback to be invoked when the API call is complete, regardless of success or failure.

Example
has.stopAccelerometer({
  success() {
    console.log('stopAccelerometer success: ');
  },
  fail(res) {
    console.log('stopAccelerometer fail: ', JSON.stringify(res));
  },
  complete() {
    console.log('stopAccelerometer complete: ');
  }
});

has.onAccelerometerChange

has.onAccelerometerChange(function callback)

Enables listening for acceleration change events.

Since: 1.0.4

Parameters

Name

Type

Mandatory

Description

callback

function

No

Callback for the acceleration change event.

Return value of callback

Name

Type

Description

x

number

Acceleration along the x-axis of the device, in m/s².

y

number

Acceleration along the y-axis of the device, in m/s².

z

number

Acceleration along the z-axis of the device, in m/s².

Example
has.onAccelerometerChange(function (res) {
  console.log('onAccelerometerChange');
});

has.offAccelerometerChange

has.offAccelerometerChange(function callback)

Cancels listening for acceleration change events. If the callback parameter is left empty, listening will be disabled for all events.

Since: 1.0.4

Parameters

Name

Type

Mandatory

Description

callback

function

No

Callback for the operation result.

Example
const fun = function (res) {
  console.log('onAccelerometerChange');
};

has.onAccelerometerChange(fun);
has.offAccelerometerChange(fun);