PEDOMETER
Hello All ,
As health concerns are growing up here comes a basic idea how to create a pedometer app, This blog will guide to create a pedometer and manipulate its setting to get the accuracy of foot steps, you can also create the logic of calories burnt on behalf of foot step results, So here we go...
- Step 1 : Create a service that implements "SensorEventListener" once you implement this interface you will get two of its methods
-
onSensorChanged
-
onAccuracyChange
All you have to do is to put your login on this sensor Change method.
- Step 2 : OnSensorChanged method gives a parameter Snesor
- this sensor contains all type of available sensors on the device in our case we will only get Accelerometer sensor which provides us the change in x, y, and z co-ordinate of the device.
Sensor sensor = event.sensor;
int j = (sensor.getType() == Sensor.TYPE_ACCELEROMETER) ? 1 : 0;
- Step 3 : And thats it we just have to calculate our values now and find if there is certain change in co ordinate its a step and if not ignore the value.
int j = (sensor.getType() == Sensor.TYPE_ACCELEROMETER) ? 1 : 0;
if (j == 1) {
float vSum = 0;
for (int i=0 ; i<3 ; i++) {
final float v = mYOffset + event.values[i] * mScale[j];
vSum += v;
}
int k = 0;
float v = vSum / 3;
float direction = (v > mLastValues[k] ? 1 : (v < mLastValues[k] ? -1 : 0));
if (direction == - mLastDirections[k]) {
// Direction changed
int extType = (direction > 0 ? 0 : 1); // minumum or maximum?
mLastExtremes[extType][k] = mLastValues[k];
float diff = Math.abs(mLastExtremes[extType][k] - mLastExtremes[1 - extType][k]);
if (diff > mLimit) {
boolean isAlmostAsLargeAsPrevious = diff > (mLastDiff[k]*2/3);
boolean isPreviousLargeEnough = mLastDiff[k] > (diff/3);
boolean isNotContra = (mLastMatch != 1 - extType);
if (isAlmostAsLargeAsPrevious && isPreviousLargeEnough && isNotContra) {
Log.e(TAG, "step");
listener.onStep();
for (StepListener stepListener : mStepListeners) {
stepListener.onStep();
}
mLastMatch = extType;
}
else {
mLastMatch = -1;
}
}
mLastDiff[k] = diff;
}
mLastDirections[k] = direction;
mLastValues[k] = v;
}
A full working code of this blog can be obtained from this link.
Happy coding :)
1 Comment(s)