Device motion

This commit is contained in:
Max Lynch
2015-09-14 12:38:04 -05:00
parent af53d274bf
commit fe028627b8
2 changed files with 14 additions and 10 deletions

View File

@ -1,4 +1,4 @@
import {IonicView} from 'ionic/ionic'; import {IonicView, DeviceMotion} from 'ionic/ionic';
@IonicView({ @IonicView({
@ -10,9 +10,15 @@ import {IonicView} from 'ionic/ionic';
<ion-title>Device Motion</ion-title> <ion-title>Device Motion</ion-title>
</ion-navbar> </ion-navbar>
<ion-content class="padding"> <ion-content class="padding">
<div *ng-if="accel">{{accel.x}} {{accel.y}} {{accel.z}}</div>
</ion-content> </ion-content>
` `
}) })
export class DeviceMotionPage { export class DeviceMotionPage {
constructor() {
DeviceMotion.watchAcceleration().source.subscribe((accel) => {
this.accel = accel.acceleration;
});
}
} }

View File

@ -13,21 +13,20 @@ import {NativePlugin} from '../plugin';
export class DeviceMotion { export class DeviceMotion {
static _wrap(result) { static _wrap(result) {
// Mimic the DeviceMotionEvent // Mimic the DeviceMotionEvent
return { return util.extend({
acceleration: result, acceleration: result, // result will be x/y/z accel
accelerationIncludingGravity: result, accelerationIncludingGravity: result, //TODO: I know this isn't correct but not sure how to normalize from native plugin
rotationRate: 0, rotationRate: 0,
interval: 0, interval: 0,
native: true native: true
} }, result);
} }
static getCurrentAcceleration() { static getCurrentAcceleration() {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if(window.DeviceMotionEvent || ('listenForDeviceMovement' in window)) { if(window.DeviceMotionEvent || ('listenForDeviceMovement' in window)) {
var fnCb = function fnCb(eventData) { var fnCb = function fnCb(eventData) {
console.log('Event', eventData); resolve(DeviceMotion._wrap(eventData));
resolve(eventData);
window.removeEventListener('devicemotion', fnCb); window.removeEventListener('devicemotion', fnCb);
} }
window.addEventListener('devicemotion', fnCb); window.addEventListener('devicemotion', fnCb);
@ -52,8 +51,7 @@ export class DeviceMotion {
let source = Rx.Observable.create((observer) => { let source = Rx.Observable.create((observer) => {
var fnCb = function fnCb(eventData) { var fnCb = function fnCb(eventData) {
console.log(eventData); observer.onNext(DeviceMotion._wrap(eventData));
observer.onNext(eventData);
}; };
window.addEventListener('devicemotion', fnCb); window.addEventListener('devicemotion', fnCb);
@ -73,7 +71,7 @@ export class DeviceMotion {
let source = Rx.Observable.create((observer) => { let source = Rx.Observable.create((observer) => {
watchID = navigator.accelerometer.watchAcceleration(function (result) { watchID = navigator.accelerometer.watchAcceleration(function (result) {
observer.onNext(result); observer.onNext(DeviceMotion._wrap(result));
}, function (err) { }, function (err) {
observer.onError(err, observer); observer.onError(err, observer);
}, options); }, options);