Git Product home page Git Product logo

cordova-plugin-device-motion's Introduction

title description
Device Motion
Access accelerometer data.

cordova-plugin-device-motion

Android Testsuite Chrome Testsuite iOS Testsuite Lint Test

Usage Notice

With the W3C Device Orientation API, Android, iOS, and Windows devices may not need this plugin anymore.

However, on iOS 13+, potential issues with permissions and secure contexts can arise. Therefore it is recommended to use this plugin as it uses a native implementation.


Description

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.

Access is via a global navigator.accelerometer object.

Although the object is attached to the global scoped navigator, it is not available until after the deviceready event.

document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
    console.log(navigator.accelerometer);
}

Report issues with this plugin on the Apache Cordova issue tracker

Installation

cordova plugin add cordova-plugin-device-motion

Supported Platforms

  • Android
  • Browser
  • iOS
  • Windows

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.

navigator.accelerometer.getCurrentAcceleration(accelerometerSuccess, accelerometerError);

Example

function onSuccess(acceleration) {
    alert('Acceleration X: ' + acceleration.x + '\n' +
          'Acceleration Y: ' + acceleration.y + '\n' +
          'Acceleration Z: ' + acceleration.z + '\n' +
          'Timestamp: '      + acceleration.timestamp + '\n');
}

function onError() {
    alert('onError!');
}

navigator.accelerometer.getCurrentAcceleration(onSuccess, onError);

Browser Quirks

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

Android Quirks

The accelerometer is called with the SENSOR_DELAY_UI flag, which limits the maximum readout frequency to something between 20 and 60 Hz, depending on the device. Values for period corresponding to higher frequencies will result in duplicate samples. More details can be found in the Android API Guide.

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.

var watchID = navigator.accelerometer.watchAcceleration(accelerometerSuccess,
                                                       accelerometerError,
                                                       accelerometerOptions);
  • accelerometerOptions: An object with the following optional keys:
    • frequency: requested frequency of calls to accelerometerSuccess with acceleration data in Milliseconds. (Number) (Default: 10000)

Example

function onSuccess(acceleration) {
    alert('Acceleration X: ' + acceleration.x + '\n' +
          'Acceleration Y: ' + acceleration.y + '\n' +
          'Acceleration Z: ' + acceleration.z + '\n' +
          'Timestamp: '      + acceleration.timestamp + '\n');
}

function onError() {
    alert('onError!');
}

var options = { frequency: 3000 };  // Update every 3 seconds

var watchID = navigator.accelerometer.watchAcceleration(onSuccess, onError, options);

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

var watchID = navigator.accelerometer.watchAcceleration(onSuccess, onError, options);

// ... later on ...

navigator.accelerometer.clearWatch(watchID);

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)

cordova-plugin-device-motion's People

Contributors

agrieve avatar alsorokin avatar bennmapes avatar cfjedimaster avatar clelland avatar cmarcelk avatar dblotsky avatar erisu avatar filmaj avatar hardeep avatar hermwong avatar jamesjong avatar jcesarmobile avatar jlongster avatar ldeluca avatar macdonst avatar mariabukharina avatar marti1125 avatar maverickmishra avatar mmocny avatar purplecabbage avatar sgrebnov avatar shazron avatar stacic avatar stevengill avatar surajpindoria avatar t1st3 avatar timbru31 avatar vladimir-kotikov avatar zalun avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

cordova-plugin-device-motion's Issues

Apple has crippled device motion behind permission check - can this plugin be revived?

So as of iOS 13, Apple now requires a website or webview to ask permission before using the device motion API's for security reasons. The permission request launches a popup that the user has to agree to, which has to be done every time the app runs. For my tilt-based game, this is unacceptable. What do you guys think about reviving this plugin? Or, is there some workaround? I've been unable to find a way to permanently enable motion for a webview.

Safari release notes:
https://developer.apple.com/documentation/safari_release_notes/safari_13_release_notes#3314664

Medium article:
https://medium.com/flawless-app-stories/how-to-request-device-motion-and-orientation-permission-in-ios-13-74fc9d6cd140

Similar issue in different repo:
processing/p5.js#4037

deprecated compatibility.

I know this plugin is now deprecated, but the last version of it was 2.0.1 - though it looks like someone was still doing a dev version 2.0.2.

My question is when it was deprecated, what were the version levels of cordova-ios and cordova-android (or cordova-cli) that this plugin was no longer needed?

My app is running significantly behind all the latest and greatest revs - so I need to determine that as of (example) [email protected] and [email protected] this plugin is not needed.

As well, if the plugin is no longer needed, where is the documentation on how to call the new integrated functions (previously used by this plugin) for device motion functionality?

Dependencies not specified

Bug Report

Problem

What is expected to happen?

Seamless install

What does actually happen?

Stop with error saying module missing

Information

This error occurs
Installing "cordova-plugin-device-motion" for android
Failed to install 'cordova-plugin-device-motion': Error: Unable to load Platform API from /media/mani-pc/your disk1/developer folder/android project/ionic_testing/platforms/android/cordova/Api.js:
Cannot find module 'strip-final-newline'
Require stack:

  • /media/mani-pc/your disk1/developer folder/android project/ionic_testing/platforms/android/cordova/node_modules/execa/index.js
  • /media/mani-pc/your disk1/developer folder/android project/ionic_testing/platforms/android/cordova/lib/check_reqs.js
  • /media/mani-pc/your disk1/developer folder/android project/ionic_testing/platforms/android/cordova/lib/prepare.js
  • /media/mani-pc/your disk1/developer folder/android project/ionic_testing/platforms/android/cordova/Api.js
  • /usr/local/lib/node_modules/cordova/node_modules/cordova-lib/src/cordova/util.js
  • /usr/local/lib/node_modules/cordova/node_modules/cordova-lib/src/platforms/index.js
  • /usr/local/lib/node_modules/cordova/node_modules/cordova-lib/src/platforms/platforms.js
  • /usr/local/lib/node_modules/cordova/node_modules/cordova-lib/src/plugman/install.js
  • /usr/local/lib/node_modules/cordova/node_modules/cordova-lib/src/plugman/plugman.js
  • /usr/local/lib/node_modules/cordova/node_modules/cordova-lib/cordova-lib.js
  • /usr/local/lib/node_modules/cordova/src/help.js
  • /usr/local/lib/node_modules/cordova/src/cli.js
  • /usr/local/lib/node_modules/cordova/bin/cordova
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:793:17)
    at Function.Module._load (internal/modules/cjs/loader.js:686:27)
    at Module.require (internal/modules/cjs/loader.js:848:19)
    at require (internal/modules/cjs/helpers.js:74:18)
    at Object. (/media/mani-pc/your disk1/developer folder/android project/ionic_testing/platforms/android/cordova/node_modules/execa/index.js:5:27)
    at Module._compile (internal/modules/cjs/loader.js:955:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:991:10)
    at Module.load (internal/modules/cjs/loader.js:811:32)
    at Function.Module._load (internal/modules/cjs/loader.js:723:14)
    at Module.require (internal/modules/cjs/loader.js:848:19)
    Unable to load Platform API from /media/mani-pc/your disk1/developer folder/android project/ionic_testing/platforms/android/cordova/Api.js:
    Cannot find module 'strip-final-newline'
    Require stack:
  • /media/mani-pc/your disk1/developer folder/android project/ionic_testing/platforms/android/cordova/node_modules/execa/index.js
  • /media/mani-pc/your disk1/developer folder/android project/ionic_testing/platforms/android/cordova/lib/check_reqs.js
  • /media/mani-pc/your disk1/developer folder/android project/ionic_testing/platforms/android/cordova/lib/prepare.js
  • /media/mani-pc/your disk1/developer folder/android project/ionic_testing/platforms/android/cordova/Api.js
  • /usr/local/lib/node_modules/cordova/node_modules/cordova-lib/src/cordova/util.js
  • /usr/local/lib/node_modules/cordova/node_modules/cordova-lib/src/platforms/index.js
  • /usr/local/lib/node_modules/cordova/node_modules/cordova-lib/src/platforms/platforms.js
  • /usr/local/lib/node_modules/cordova/node_modules/cordova-lib/src/plugman/install.js
  • /usr/local/lib/node_modules/cordova/node_modules/cordova-lib/src/plugman/plugman.js
  • /usr/local/lib/node_modules/cordova/node_modules/cordova-lib/cordova-lib.js
  • /usr/local/lib/node_modules/cordova/src/help.js
  • /usr/local/lib/node_modules/cordova/src/cli.js
  • /usr/local/lib/node_modules/cordova/bin/cordova

Command or Code

ionic cordova plugin add cordova-plugin-device-motion

Environment, Platform, Device

android

Version information

Ionic 6.14

Checklist

  • I searched for existing GitHub issues
  • I updated all Cordova tooling to most recent version
  • I included all the necessary information above

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.