Search code examples
androidaccelerometersensors

Step detector using android accelerometer


i am building an app that counts the steps using accelerometer for android devices.(android version< .4.4)

to do this i calculate the gravity (G) using x,y and z. if the value of G is larger than STEP_THRESHOLD then an step has been occured:

    @Override
public void onSensorChanged(SensorEvent event) {
    synchronized (this) {

        if (event.sensor.getType() != Sensor.TYPE_ACCELEROMETER) {
            return;
        }

        final float x = event.values[0];
        final float y = event.values[1];
        final float z = event.values[2];
        final float g = Math.abs((x * x + y * y + z * z) / (SensorManager.GRAVITY_EARTH * SensorManager.GRAVITY_EARTH));

        if (g > StepUtil.GRAVITY_THRESHOLD) {
            /*
            * check step times and other checkings
            */
             .
             .
             .
            stepCounter++;
        }

    }
}

the problem is: since accelerometer hardwares is varies in each device, there is not a certain value for STEP_THRESHOLD. it seems that the STEP_THRESHOLD value should be dynamic for each device! is there any criterion to change STEP_THRESHOLD value base on accelerometer accuracy?

any help will be appreciated.


Solution

  • since accelerometer hardwares is varies in each deviceو there is not a certain value for STEP_THRESHOLD

    Having that said, what your app is apparently missing is simple calibration, which would allow your users to set the threshold level on their device. Tell them to do couple of steps, take sensor values, take mean/median/average (whatever will work for you better) and you got your STEP_THRESHOLD. You can even send this value back to your server so you can build kind of database and come with some more/less universtal starting value of STEP_AVERAGE.