Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detecting device motion/orientation support?

I'm looking for a way to check if a device supports either the DeviceOrientationEvent or DeviceMotionEvent. To be more precisely I wan't to know if the device really has an accelerometer.

Unfortunately window.DeviceMotionEvent and window.ondevicemotion respectively window.DeviceOrientationEvent and window.ondeviceorientation exists although the device - MacBook Retina - doesn't have an accelerometer.

It's clear to me, that if the Event is never fired, the callback-function of an eventlistener will also never run. But in my case I, my program need's to know if the device has an accelerometer, because the user should receive a notification if there is no accelerometer.

Answers of this post unfortunately didn't work for my problem.

  • Detecting support for a given JavaScript event?
like image 799
Thomas König Avatar asked May 15 '14 16:05

Thomas König


People also ask

How do I know what orientation my device is?

Detecting Orientation Changes in Javascript Should you need to simply detect when a user changes orientation, you can use the following event listener: screen. orientation. addEventListener("change", function(e) { // Do something on change });

What is the difference between the device Motion API and the orientation API?

The device orientation event returns rotation data, which includes how much the device is leaning front-to-back, side-to-side, and, if the phone or laptop has a compass, the direction the device is facing. Use device motion for when the current motion of the device is needed.

What is device motion?

It allows Web applications to access the accelerometer data expressed as acceleration (in m/s2) and gyroscope data expressed as rotation angle change (in °/s) for each of the three dimensions, provided as events.


1 Answers

Given your program needs to have an accelerometer functionality to function, I'm assuming you're blocking its execution if there is none.

So your program could 'block' by default, and listen for a single deviceMotionEvent which will initialize the whole of your program, which will ultimately bind onto window.ondevicemotion. That way you don't need to block the execution for with a setTimeout; a deviceMotionEvent will likely be fired rather quickly after window load.

like image 148
Prusprus Avatar answered Sep 17 '22 13:09

Prusprus