15

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

Harry .Naeem
  • 1,245
  • 3
  • 20
  • 33

3 Answers3

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 noice 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 yor 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.

TukeV
  • 641
  • 2
  • 6
  • 13
  • 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. – Harry .Naeem Oct 19 '17 at 08:00
  • 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: https://stackoverflow.com/questions/6375381/are-there-any-well-known-algorithms-to-count-steps-based-on-the-accelerometer – TukeV Oct 19 '17 at 09:30
  • 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. – Yosidroid Jun 16 '20 at 00:59
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++;
    }
}
ShutterSoul
  • 2,551
  • 3
  • 23
  • 28
  • Greate its working but how i will get walking speed using this ? – Vanraj Ghed Jun 20 '18 at 05:14
  • For walking speed, I'd use a different sensor, such as GPS, GLONASS, Galileo, BeiDou, IRNSS, or QZSS, or the general location services API. – Steve Mar 15 '19 at 18:02
  • 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. – TukeV May 28 '19 at 16:21
  • Most devices < android Marshmallow does not have these sensor. – Yosidroid May 12 '20 at 22:58
  • 3
    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. – Yosidroid Jun 16 '20 at 01:00
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.

utengr
  • 3,225
  • 3
  • 29
  • 68