Motion

The Motion API 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.

Usage
// Get the device current acceleration
Motion.getCurrentAcceleration()
    .then((acc)=>{
        console.log(acc.x);
        console.log(acc.y);
        console.log(acc.z);
        console.log(acc.timestamp);
    })
    .catch((err)=>{
        console.log(err);
    });

...

// Watch device acceleration
// Update every 2 seconds
var options = { frequency: 2000 };  
var subs = Motion.watchAcceleration(options)
.subscribe((acc)=>{
    console.log(acc.x);
    console.log(acc.y);
    console.log(acc.z);
    console.log(acc.timestamp);
});

...

// Stop watch
subs.unsubscribe();
window.Motion.getCurrentAcceleration()
    .then((acc) => {
        console.log(acc.x);
        console.log(acc.y);
        console.log(acc.z);
        console.log(acc.timestamp);
    })
    .catch((err) => {
        console.log(err);
    });

...

var options = { frequency: 2000 };
setSubAceleration(window.Motion.watchAcceleration(options).subscribe(
    (acc) => {
        console.log(acc.x);
        console.log(acc.y);
        console.log(acc.z);
        console.log(acc.timestamp);
    }
));

...

subAceleration.unsubscribe();
window.Motion.getCurrentAcceleration()
    .then((acc) => {
        console.log(acc.x);
        console.log(acc.y);
        console.log(acc.z);
        console.log(acc.timestamp);
    })
    .catch((err) => {
        console.log(err);
    });

...

var options = { frequency: 2000 };
this.subAceleration = window.Motion.watchAcceleration(options).subscribe(
    (acc) => {
        console.log(acc.x);
        console.log(acc.y);
        console.log(acc.z);
        console.log(acc.timestamp);
    }
);

...

this.subAceleration.unsubscribe();
(<any>window).Motion.getCurrentAcceleration()
    .then((acc) => {
        console.log(acc.x);
        console.log(acc.y);
        console.log(acc.z);
        console.log(acc.timestamp);
    })
    .catch((err) => {
        console.log(err);
    });

...

var options = { frequency: 2000 };
this.subAceleration = (<any>window).Motion.watchAcceleration(options).subscribe(
    (acc) => {
        console.log(acc.x);
        console.log(acc.y);
        console.log(acc.z);
        console.log(acc.timestamp);
    }
);

...

this.subAceleration.unsubscribe();
window.Motion.getCurrentAcceleration()
    .then((acc) => {
        console.log(acc.x);
        console.log(acc.y);
        console.log(acc.z);
        console.log(acc.timestamp);
    })
    .catch((err) => {
        console.log(err);
    });

...

var options = { frequency: 2000 };
subAceleration = window.Motion.watchAcceleration(options).subscribe(
    (acc) => {
        console.log(acc.x);
        console.log(acc.y);
        console.log(acc.z);
        console.log(acc.timestamp);
    }
);

...

subAceleration.unsubscribe();

Classes


Motion

getCurrentAcceleration

getCurrentAcceleration(): Promise <Acceleration>

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

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.


RETURN
returns: Promise <Acceleration>

watchAcceleration

watchAcceleration(option? :AcceleratorOptions ): Observable <Acceleration>

Watch device Acceleration at a regular interval. Specify the interval in milliseconds via the AcceleratorOptions object's frequency parameter.


PARAMETERS
[optional]option: AcceleratorOptions

RETURN
returns: Observable <Acceleration>

Interfaces

Acceleration
interface Acceleration {
    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  : DOMTimeStamp; // Creation timestamp in milliseconds
}
AcceleratorOptions
interface AcceleratorOptions {
    frequency: Number;  // default 10000
}