# How to count steps with TYPE\_STEP\_DETECTOR?

**URL:** <https://discourse.processing.org/t/how-to-count-steps-with-type-step-detector/6411>\
**Category:** Processing for Android\
**Created:** [December 8, 2018, 12:49am UTC](https://discourse.processing.org/t/how-to-count-steps-with-type-step-detector/6411 "2018-12-08T00:49:17Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![craftthis](https://avatars.discourse-cdn.com/v4/letter/c/6bbea6/32.png) [@craftthis](https://discourse.processing.org/u/craftthis)\
**Post date:** [December 8, 2018, 12:49am UTC](https://discourse.processing.org/t/how-to-count-steps-with-type-step-detector/6411/1 "2018-12-08T00:49:17Z")

</div>

I am trying to use the sensor TYPE\_STEP\_DETECTOR to detect when the user steps, but i don’t know exactly the sintax for this, if anyone could help it would help.

---

<div class="post-metadata">

**Author:** ![akenaton](https://avatars.discourse-cdn.com/v4/letter/a/a88e4f/32.png) [@akenaton](https://discourse.processing.org/u/akenaton)\
**Post date:** [December 8, 2018, 11:10am UTC](https://discourse.processing.org/t/how-to-count-steps-with-type-step-detector/6411/2 "2018-12-08T11:10:25Z")

</div>

@craftthis===

if(i suppose!!!) you have a phone with this sensor and know that, you have only to add some global var to the SensorEventListener (eg : int numberSteps), use it when the sensor fires (method onSensorChanged()):

numberSteps++;

Anyway add the usesFeatures for this sensor to the Manifest (if you wan to distribute your app) and in order to be sure that the sensor is available add a method listing all sensors and their names::

String sensorPresent = “”;  
String theStepSensorName = //(i dont remember but probably…)// “step detector”;  
List sensorList = sensorManager.getSensorList(Sensor.TYPE\_ALL);  
for (Sensor s : sensorList){  
sensorPresent= sensorPresent + s.getName()+ “\n”;  
println(sensorPresent);  
if(s.getName.equals(theStepSensorName)){  
mStepSensor = sensorManager.getDefaultSensor(Sensor.TYPE\_STEP\_DETECTOR);  
break;

}  
}

---

<div class="post-metadata">

**Author:** ![craftthis](https://avatars.discourse-cdn.com/v4/letter/c/6bbea6/32.png) [@craftthis](https://discourse.processing.org/u/craftthis)\
**Post date:** [December 8, 2018, 9:17pm UTC](https://discourse.processing.org/t/how-to-count-steps-with-type-step-detector/6411/3 "2018-12-08T21:17:00Z")

</div>

thanks for answering,

I already know that my phone has this sensor (by the use of other apps), so by now i was focusing on the step counting algorithm and i tried this:

import android.content.Context;  
import android.hardware.Sensor;  
import android.hardware.SensorManager;  
import android.hardware.SensorEvent;  
import android.hardware.SensorEventListener;

Context context;  
SensorManager manager;  
Sensor sensor;  
PedometerListener listener;  
float numberSteps= 0 ;

void setup(){  
fullScreen();  
context = getActivity();  
manager = (SensorManager)context.getSystemService(Context.SENSOR\_SERVICE);  
sensor = manager.getDefaultSensor(Sensor.TYPE\_STEP\_DETECTOR);  
listener = new PedometerListener();  
manager.registerListener(listener, sensor, SensorManager.SENSOR\_DELAY\_GAME);

textFont(createFont(“SansSerif”, 30 \* displayDensity));  
}

void draw(){  
background(0);  
text(numberSteps, 0, height/2);  
}  
class PedometerListener implements SensorEventListener {  
public void onSensorChanged(SensorEvent event){  
numberSteps++;  
}  
public void onAccuracyChanged(Sensor sensor, int accuracy) {  
}  
}

But it just returns 0. Do you know what i did wrong?

---

<div class="post-metadata">

**Author:** ![akenaton](https://avatars.discourse-cdn.com/v4/letter/a/a88e4f/32.png) [@akenaton](https://discourse.processing.org/u/akenaton)\
**Post date:** [December 9, 2018, 8:58am UTC](https://discourse.processing.org/t/how-to-count-steps-with-type-step-detector/6411/4 "2018-12-09T08:58:05Z")

</div>

@craftthis===  
as any of my phones & tablets has this sensor i cannot test; yet looking to your code, which is the standard one for sensors, i see 2 problems=

- your context is not good: you get only the activity which created the fragment and not the context (to which sensors belong) : you have to write:

context = this.getActivity().getApplicationContext().

- your delay is very short (game): try with SENSOR\_DELAY\_NORMAL
