18

Is there any way to access accelerometer data using Javascript on Android's browser? I know it supports "onorientationchange", but I'd like to get everything.

Clarification: I'm asking how to do this in a website, not a native app.

Jeff Lamb
  • 5,755
  • 4
  • 37
  • 54

6 Answers6

17

As of ICS, Android 4.0, you can use the 'devicemotion' event via a JavaScript event listener to access the accelerometer data. See the W3C documentation on how to access it - http://dev.w3.org/geo/api/spec-source-orientation.html.

Note - The W3C documentation title is named with 'device orientation', but the spec does indeed include 'devicemotion' event documentation.

Bamerza
  • 1,335
  • 1
  • 18
  • 34
15

Making an update to this thread.

HTML5 lets someone do this. Detecting whether or not an accelerometer is present is easy.

    if (window.DeviceMotionEvent == undefined) {
        //No accelerometer is present. Use buttons. 
        alert("no accelerometer");
    }
    else {
        alert("accelerometer found");
        window.addEventListener("devicemotion", accelerometerUpdate, true);
    }

In the function that you define to receive the accelerometer events, you can look at the accelerationIncludingGravity member.

function accelerometerUpdate(e) {
   var aX = event.accelerationIncludingGravity.x*1;
   var aY = event.accelerationIncludingGravity.y*1;
   var aZ = event.accelerationIncludingGravity.z*1;
   //The following two lines are just to calculate a
   // tilt. Not really needed. 
   xPosition = Math.atan2(aY, aZ);
   yPosition = Math.atan2(aX, aZ);
}

More information can be found here: http://dev.w3.org/geo/api/spec-source-orientation.html

Maximillian Laumeister
  • 19,884
  • 8
  • 59
  • 78
Joel
  • 2,230
  • 1
  • 20
  • 28
  • 1
    I just tried this in Chrome running on a desktop computer on Windows 10, and Chrome seems to report `window.DeviceMotionEvent`, i.e. that an accelerometer is present. I am pretty sure it is not though, at least nothing happens when I pick up my pc tower and knock it around a bit. – Maximillian Laumeister Apr 20 '17 at 18:13
  • I'm not sure what the Chrome implementation for desktops does. Desktops can have accelerometers (I had one connected through USB for testing some time ago). Additionally portable PCs often run the same version of Chrome and Windows. – Joel May 31 '17 at 16:27
  • @MaximillianLaumeister - This is a criminally underrated comment. Good job. – Barrie Reader Jun 21 '20 at 16:00
5

You could try with PhoneGap that provides API to access the accelerometer from javascript.

Here the documentation.

systempuntoout
  • 71,966
  • 47
  • 171
  • 241
4

If you are trying to access the accelerometer from a webpage hosted on a server (verus one integrated into a native application through WebView), than the accelerometer data does not appear to be available as of now for Android. You can find a more detailed assessment here: http://www.mobilexweb.com/blog/android-froyo-html5-accelerometer-flash-player .

You might also want to check out this SO post: Detect rotation of Android phone in the browser with JavaScript

Community
  • 1
  • 1
Eric Levine
  • 13,536
  • 5
  • 49
  • 49
1

If I'm reading the docs correctly, you could set up a class (within Java/Android) that provides the accelerometer functionality you need in public functions.

Then setup a javascript interface for the webview using the addJavascriptInterface call, which makes the public functions in that class available to be called from within javascript.

ylebre
  • 3,100
  • 1
  • 18
  • 14
  • So that would require a custom rebuild of Android? – Jeff Lamb Dec 17 '10 at 21:51
  • You do not need a "custom rebuild of Android". You would simply be using a feature of WebView: http://developer.android.com/reference/android/webkit/WebView.html – Eric Levine Dec 18 '10 at 00:30
  • Yes. Are you trying to do this through a website and not a WebView in a native app? If so, you might want to clarify that in your original question. – Eric Levine Dec 19 '10 at 18:30
0

Looking at this post flash.sensors.Accelerometer on Android within web browser it seems accelerometer data is available to flash. So a possible workaround (at least for devices which have flash) would be a small flash applet which grabbed the data for you.

Sounds like a hack, but still sounds better than making the whole thing in flash

Community
  • 1
  • 1
funkyhat
  • 13
  • 5