Device Motion

Improve this doc

$ ionic plugin add cordova-plugin-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.

Usage

import { DeviceMotion } from 'ionic-native';


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

// Watch device acceleration
var subscription = DeviceMotion.watchAcceleration().subscribe((acceleration: AccelerationData) => {
  console.log(acceleration);
});

// Stop watch
subscription.unsubscribe();

Static 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
(optional)
number

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

API

Native

General