How to count steps with TYPE_STEP_DETECTOR?


#1

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.


#2

@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;

}
}


#3

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?


#4

@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