Pedometer (Step Counter)
Asked Answered
G

3

14

I am developing a Pedometer Android application to count number of steps taken and using the steps calculate the distance covered and calories burned. I have followed the tutorial Create a Simple Pedometer and Step Counter in Android and done exactly like it. It detects number of steps when the sensor detects motion.

But there are some problems with it:

  1. When I stand at the same place with my device in my hand and just move my hand or give a jerk to device, it detects the change and adds to step count.
  2. If I move very slowly with device in my hand it does not detect the change.
  3. If i jump, then it adds several steps in the counter.

I have checked some other applications from Play Store they do not do this kind of stuff.

I have searched but cannot find an appropriate solution or tutorial for it. Any help or suggestions. Thanks

Goosegog answered 19/10, 2017 at 6:32 Comment(0)
L
13

The problem here is that your implementation is not sophisticated enough: it only checks if there is a spike in the accelerometer data and assumes that the spike is coming from a step. It has no idea where the spike in acceleration is really coming from: it might as well come from you jumping or shaking the device in your hand.

How to make it more accurate then? Well, that is a really difficult question which has been topic for scientific papers for a really long time. Even the most sophisticated fitness trackers (which use machine learning, signal processing and other statistical methods) have difficulties to determine when the step is real and when it is just noise or user playing with the device.

Luckily Android does have it's own builtin step counter and step detector, which are more sophisticated than the class in your example.

So unless you really want to learn signal processing and AI (which I highly recommended, although I don't know much about the data science of step detection), I would suggest to use builtin detector and counter.

Lyontine answered 19/10, 2017 at 7:22 Comment(3)
Yes, there are some devices which do not have sensors in it. For example, Samsung J5. I tested one of the Pedometers (searched on Google, i think not available on Play Store). When I installed that app, I got a message that it does not have sensor in it, so app cannot run. Also the writer of the tutorial I have mentioned in my question has given a comment at the end of his article that "Most of the Android devices doesn’t have an inbuilt Step Counter Sensor." So it makes some sense.Goosegog
In that case the solution is to make signal processing smarter i.e. scraping the solution in the tutorial and apply a different algorithm. And even then, if spikes in the motion look like steps, then the algorithm will mark it as a step. Here's older SO question which might be useful: #6375881Lyontine
Step detector and step counter api does not work in every devices b.c in some devices specially Android 9 (API level 28) and earlier does not support that API.Quinque
H
9

By implementing SensorEventListener listener within a class and overriding the two methods onSensorChanged and onAccuracyChanged you can start tracking steps.

    public class StepActivity extends Activity implements SensorEventListener{
    SensorManager sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
    Sensor sSensor= sensorManager .getDefaultSensor(Sensor.TYPE_STEP_DETECTOR);

    ...

}

Now we have initialised the SensorManager and Sensor and have the Sensor registered as a listener within the activity, we now need to implement the onSensorChanged function that will be triggered by a SensorEvent whenever there is a change to the Sensor we registered, in our case the TYPE_STEP_DETECTOR.

private long steps = 0;

@Override
public void onSensorChanged(SensorEvent event) {
    Sensor sensor = event.sensor;
    float[] values = event.values;
    int value = -1;

    if (values.length > 0) {
        value = (int) values[0];
    }


    if (sensor.getType() == Sensor.TYPE_STEP_DETECTOR) {
        steps++;
    }
}
Highly answered 17/1, 2018 at 10:17 Comment(5)
Greate its working but how i will get walking speed using this ?Pharisaism
For walking speed, I'd use a different sensor, such as GPS, GLONASS, Galileo, BeiDou, IRNSS, or QZSS, or the general location services API.Bramante
If the user is indoors, then satellite solutions just don't work. Indoors you have to use different methods to determine user's speed. One way to do this that user gives his own step length then you just calculate approximate walking speed using the step length and how frequently user takes steps. This however only gives a crude estimate but its better than nothing.Lyontine
Most devices < android Marshmallow does not have these sensor.Quinque
Step detector and step counter api does not work in every devices b.c in some devices specially Android 9 (API level 28) and earlier does not support that API.Quinque
T
2

That's a very naive method to achieve step count. You should use Android's built-in step counter because it also uses other sensors if available such as gyroscope which can improve the step detection. You should especially use this built-in version if you are going to built things on top it. You need a reliable underlying layer. You can also try using linear acceleration sensor which is calculated by removing gravity component from the accelerometer. The gravity makes accelerometer very sensitive, that's why you see step counter increasing when you are just standing.

The details can be found here: https://source.android.com/devices/sensors/sensor-types#step_detector

If you still want to develop your own from scratch, then look at this code: https://github.com/bagilevi/android-pedometer

You can also try Google scholar for the latest papers on step counting algorithms. Especially try to read the latest survey on the topic.

Temblor answered 19/10, 2017 at 20:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.