Device Motion

Contents

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

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

Ionic Device Motion插件的安装(Installation)

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

Ionic Device Motion插件支持的平台(Supported Platforms)

  • Android
  • BlackBerry 10
  • Browser
  • Firefox OS
  • iOS
  • Tizen
  • Ubuntu
  • Windows
  • Windows Phone 8

Ionic Device Motion插件的用法(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();