7

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.

Community
  • 1
  • 1
Vispercept
  • 105
  • 1
  • 9
  • I don't know how precise the `ondevicemotion` event is, but would it be acceptable to listen for the event, and if no such event is received within, say, 5 seconds, then the device is either resting on a table, held by someone with an iron grip, or doesn't have an accelerometer? – Niet the Dark Absol May 15 '14 at 16:25
  • You're right. This is one possible way, I also thought of. This solution has the problem, that I have to wait some time (only a little, because `devicemotion` fires very often) for the answer of this check. In my case I really need to know from the beginning if the device has an accelerometer, because the user is forwarded to another page if the device has no accelerometer... – Vispercept May 15 '14 at 16:34

1 Answers1

3

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.

Prusprus
  • 7,987
  • 9
  • 42
  • 57