Search code examples
javascripthtmlmobilecross-browseraccelerometer

Standardizing Accelerometer Output


I am currently writing an HTML5 web app that depends heavily on accelerometer data. The idea is to have the app be as cross platform as possible (iPhones, iPads, Androids, Windows Phone, etc.)

I am having trouble because, first of all, there are several ways to get the acceleration of the device through HTML5:

  • DeviceMotionEvent
  • DeviceOrientationEvent
  • navigator.accelerometer

Secondly, different devices give different values. For example, one device might give values between 0-10 and another 0-100. To make this even more frustrating, different browsers on the same device are inconsistent about it too.

Is there a way to somehow take this information and use it in a way that I won't have to code separate cases for every combination of device and browser? For example, turning the accelerometer data from raw, meaningless numbers to the orientation of the device in degrees?


Solution

  • You could always have a divisor set up for each popular device, or somehow fetch the max and min values. If the min number was negative, subtract that from the current value, so that it HAS to be positive, then divide by the max. You should then get a decimal between 0 and 1. No guarantees. I tested this with random numbers, but not actual accelerator input, It seemed to convert the numbers to a decimal.

    if (device=="iphone")
    {
    minval=-10 ; maxval=10
        }
    if (device=="somethingelse")
        {
        minval=0 ; maxval=100
            }
    if (minval<0)
            {
            rawdataa = rawdata - minval
            }
        rawdataaa = rawdataa / maxval
        alert(rawdataaa);