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.
| Parameter | Type | Default Value | Mandatory | Description |
|---|---|---|---|---|
| interval | string | normal | No | Execution frequency for the callback of acceleration data. - 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 code information
| Error Code | Error Message |
|---|---|
| 102 | interval is invalid. |
| 12001501 | startAccelerometer:fail. For details, see Sensor Error Codes. |
Example:
has.startAccelerometer({
success: () => {
console.info('startAccelerometer success');
},
fail: (err) => {
console.error('startAccelerometer fail', err);
},
complete: (res) => {
console.info('startAccelerometer complete', res);
}
}); 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.
| 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 Code | Error Message |
|---|---|
| 12001601 | stopAccelerometer:fail. For details, see Sensor Error Codes. |
Example:
has.stopAccelerometer({
success: () => {
console.info('stopAccelerometer success');
},
fail: (err) => {
console.error('stopAccelerometer fail', err);
},
complete: (res) => {
console.info('stopAccelerometer complete', res);
}
}); has.onAccelerometerChange(function callback)
Enables listening for acceleration data change events.
Since: 1.0.4
Parameters
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| callback | function | Yes | Callback for the acceleration change event. |
Return value of callback
| Parameter | 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.info('onAccelerometerChange callback triggered', res);
}); has.offAccelerometerChange(function callback)
Removes the listener function for acceleration data change events.
Since: 1.0.4
Parameters
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| callback | function | No | Listener function passed by has.onAccelerometerChange. If this parameter is not passed, all listener functions will be removed. |
Example:
const onAccelerometerChangeFn = function(res) {
console.info('onAccelerometerChange callback triggered', res);
};
has.onAccelerometerChange(onAccelerometerChangeFn);
has.offAccelerometerChange(onAccelerometerChangeFn);