Sorry about the red box, but we really need you to update your browser. Read this excellent article if you're wondering why we are no longer supporting this browser version. Go to Browse Happy for browser suggestions and how to update.

This plugin provides access to the device's accelerometer. The accelerometer is
a motion sensor that detects the change (delta) in movement relative to the
current device orientation, in three dimensions along the x, y, and z
axis.

Installation

cordova plugin add org.apache.cordova.device-motion

Supported Platforms

Amazon Fire OS

Android

BlackBerry 10

Browser

Firefox OS

iOS

Tizen

Windows Phone 7 and 8

Windows 8

Methods

navigator.accelerometer.getCurrentAcceleration

navigator.accelerometer.watchAcceleration

navigator.accelerometer.clearWatch

Objects

Acceleration

navigator.accelerometer.getCurrentAcceleration

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

These acceleration values are returned to the accelerometerSuccess
callback function.

Browser Quirks

Values for X, Y, Z motion are all randomly generated in order to simulate the accelerometer.

iOS Quirks

iOS doesn't recognize the concept of getting the current acceleration at any given point.

You must watch the acceleration and capture the data at given time intervals.

Thus, the getCurrentAcceleration function yields the last value reported from a watchAccelerometer call.

navigator.accelerometer.watchAcceleration

Retrieves the device's current Acceleration at a regular interval, executing
the accelerometerSuccess callback function each time. Specify the interval in
milliseconds via the acceleratorOptions object's frequency parameter.

The returned watch ID references the accelerometer's watch interval,
and can be used with navigator.accelerometer.clearWatch to stop watching the
accelerometer.

iOS Quirks

The API calls the success callback function at the interval requested,
but restricts the range of requests to the device between 40ms and
1000ms. For example, if you request an interval of 3 seconds,
(3000ms), the API requests data from the device every 1 second, but
only executes the success callback every 3 seconds.

navigator.accelerometer.clearWatch

Stop watching the Acceleration referenced by the watchID parameter.

navigator.accelerometer.clearWatch(watchID);

watchID: The ID returned by navigator.accelerometer.watchAcceleration.

Example

Acceleration

Contains Accelerometer data captured at a specific point in time.
Acceleration values include the effect of gravity (9.81 m/s^2), so that when a
device lies flat and facing up, x, y, and z values returned should be
0, 0, and 9.81.

Properties

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

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

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

timestamp: Creation timestamp in milliseconds. (DOMTimeStamp)

Last modified: 2014-10-09

Got questions about leaving a comment? Get answers from our Disqus FAQ.