Reputation: 85
I am developing an android step counter that starts counting when i press a button and stops when i press the button again. The problem is that the counter always misses the first few steps i am using the step counter sensor introduced in android kitkat 4.4. I also tried the step detector sensor but had the same results. I tried changing the delay when registering the sensor to fastest, game, normal, and I even tried to set the delay to 0 but nothing worked
Is there a way i can fix this?
this is a simple app that shows my problem https://github.com/omaressameldin/simplestepcounter
MainActivity.java
package com.example.oessa_000.simplestepcounter;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.ToggleButton;
public class MainActivity extends AppCompatActivity implements SensorEventListener {
private SensorManager mSensorManager;
private Sensor mStepCounterSensor;
ToggleButton countToggle;
TextView stepView;
private int stepCount = 0;
boolean toggle = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
mStepCounterSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER);
stepView = (TextView) findViewById(R.id.stepView);
countToggle = (ToggleButton) findViewById(R.id.countToggle);
;
countToggle.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
toggle = !toggle ;
if(toggle){
startListening();
}
else{
stopListening();
}
}
});
}
protected void startListening() {
mSensorManager.registerListener(this, mStepCounterSensor, SensorManager.SENSOR_DELAY_NORMAL);
}
protected void stopListening() {
mSensorManager.unregisterListener(this, mStepCounterSensor);
}
public void onResume() {
super.onResume();
}
@Override
public void onSensorChanged(SensorEvent event) {
Sensor sensor = event.sensor;
switch(sensor.getType()) {
case Sensor.TYPE_STEP_COUNTER:
if (toggle){
stepCount++;
stepView.setText("Step Count: " + stepCount);
}
break;
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.oessa_000.simplestepcounter.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Step Count: 0"
android:textSize="15dp"
android:id="@+id/stepView"
/>
<ToggleButton
android:layout_width="200dp"
android:layout_height="75dp"
android:layout_centerHorizontal="true"
android:id="@+id/countToggle"
android:layout_margin="20dp"
android:textOff="Not Walking"
android:textOn="Walking"
android:layout_below="@+id/stepView"
/>
</RelativeLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.oessa_000.simplestepcounter">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<uses-feature android:name="android.hardware.sensor.stepcounter" />
</activity>
</application>
</manifest>
Edited: Added code snippets
Upvotes: 1
Views: 3967
Reputation: 85
I realized that using the predefined stepCounter sensor for small number of steps, like what i wanted, isn't the best option and implementing a counter using accelerometer would be a lot better; however it won't be recommended for building fitness apps or any app that require constant step counting for a long time because the delay in the stepcounting sensor is to neglect the false positives.
so this is my implementation of the stepcounter using accelerometer
MainActivity.java
package com.example.oessa_000.testcompass;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.NumberFormat;
import android.app.Activity;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.View;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.ToggleButton;
public class MainActivity extends Activity implements SensorEventListener{
// Gravity for accelerometer data
private float[] gravity = new float[3];
// smoothed values
private float[] smoothed = new float[3];
// sensor manager
private SensorManager sensorManager;
// sensor gravity
private Sensor sensorGravity;
private double bearing = 0;
private TextView acc;
private TextView stepView;
private TextView thresholdView;
private SeekBar seek;
private ToggleButton countToggle;
private int stepCount;
private boolean toggle;
private double prevY;
private double threshold;
private boolean ignore;
private int countdown;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
acc = (TextView) findViewById((R.id.accelerometer));
thresholdView = (TextView) findViewById((R.id.thresholdView));
stepView = (TextView) findViewById((R.id.stepView));
countToggle = (ToggleButton) findViewById(R.id.countToggle);
seek = (SeekBar) findViewById(R.id.seek);
seek.setProgress(0);
seek.incrementProgressBy(1);
seek.setMax(40);
// keep screen light on (wake lock light)
implementListeners();
}
protected float[] lowPassFilter( float[] input, float[] output ) {
if ( output == null ) return input;
for ( int i=0; i<input.length; i++ ) {
output[i] = output[i] + 1.0f * (input[i] - output[i]);
}
return output;
}
@Override
protected void onStart() {
super.onStart();
sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
sensorGravity = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
// listen to these sensors
sensorManager.registerListener(this, sensorGravity,
SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
protected void onStop() {
super.onStop();
// remove listeners
sensorManager.unregisterListener(this, sensorGravity);
}
@Override
public void onSensorChanged(SensorEvent event) {
// get accelerometer data
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
// we need to use a low pass filter to make data smoothed
smoothed = lowPassFilter(event.values, gravity);
gravity[0] = smoothed[0];
gravity[1] = smoothed[1];
gravity[2] = smoothed[2];
acc.setText("x: "+gravity[0] + " y: " + gravity[1] + " z: " + gravity[2]+ "ignore: "+ ignore + "countdown: "+ countdown);
if(ignore) {
countdown--;
ignore = (countdown < 0)? false : ignore;
}
else
countdown = 22;
if(toggle && (Math.abs(prevY - gravity[1]) > threshold) && !ignore){
stepCount++;
stepView.setText("Step Count: " + stepCount);
ignore = true;
}
prevY = gravity[1];
}
}
public void implementListeners(){
seek.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) {
threshold = ((double)seek.getProgress()) * 0.02;
thresholdView.setText("Threshold: "+ threshold);
}
});
countToggle.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
toggle = !toggle ;
if(toggle){
stepCount = 0;
countdown = 5;
ignore = true;
stepView.setText("Step Count: " + stepCount);
}
}
});
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.oessa_000.testcompass.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:layout_below="@+id/degree"
android:id="@+id/accelerometer"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Step Count: 0"
android:textSize="24dp"
android:layout_centerHorizontal="true"
android:id="@+id/stepView"
android:layout_below="@+id/accelerometer"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:layout_marginBottom="0dp"
android:layout_below="@+id/stepView"
android:id="@+id/thresholdView"
/>
<SeekBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/seek"
android:layout_marginTop="0dp"
android:layout_below="@+id/thresholdView"
/>
<ToggleButton
android:layout_width="200dp"
android:layout_height="75dp"
android:layout_centerHorizontal="true"
android:id="@+id/countToggle"
android:layout_margin="20dp"
android:textOff="Not Walking"
android:textOn="Walking"
android:layout_below="@+id/seek"
/>
</RelativeLayout>
Adjusting the threshold sets the sensitivity of the sensor :)
Reference: http://nebomusic.net/androidlessons/Pedometer_Project.pdf
Upvotes: 3