Device Motion

Improve this doc

$ ionic plugin add cordova-plugin-device-motion
$ npm install --save @ionic-native/device-motion

Repo: https://github.com/apache/cordova-plugin-device-motion

Requires Cordova plugin: cordova-plugin-device-motion. For more info, please see the Device Motion docs.

Supported platforms

Usage

import { DeviceMotion, DeviceMotionAccelerationData } from '@ionic-native/device-motion';

constructor(private deviceMotion: DeviceMotion) { }

...

// Get the device current acceleration
this.deviceMotion.getCurrentAcceleration().then(
  (acceleration: DeviceMotionAccelerationData) => console.log(acceleration),
  (error: any) => console.log(error)
);

// Watch device acceleration
var subscription = this.deviceMotion.watchAcceleration().subscribe((acceleration: DeviceMotionAccelerationData) => {
  console.log(acceleration);
});

// Stop watch
subscription.unsubscribe();

Instance Members

getCurrentAcceleration()

Get the current acceleration along the x, y, and z axes.

Returns: Promise<DeviceMotionAccelerationData> Returns object with x, y, z, and timestamp properties

watchAcceleration(options)

Watch the device acceleration. Clear the watch by unsubscribing from the observable.

Param Type Details
options AccelerometerOptions

list of options for the accelerometer.

Returns: Observable<DeviceMotionAccelerationData> Observable returns an observable that you can subscribe to

DeviceMotionAccelerationData

Param Type Details
x number

Amount of acceleration on the x-axis. (in m/s^2)

y number

Amount of acceleration on the y-axis. (in m/s^2)

z number

Amount of acceleration on the z-axis. (in m/s^2)

timestamp any

Creation timestamp in milliseconds.

DeviceMotionAccelerometerOptions

Param Type Details
frequency number

Requested period of calls to accelerometerSuccess with acceleration data in Milliseconds. Default: 10000

(optional)

API

Native

General