0

I have a Samsung phone which apparently doesn't have a Pedometer (step counter) because when I use TYPE_STEP_COUNTER it says doesn't have one. So after searching online, I came across an implementation using accelerometer censor. But the problem is the implementation is not very accurate at detecting steps. I have tried to calibrate the numbers but still not very reliable. It will add the squares of x, y, z axis and calculate the root to get the magnitude of the steps. So my question is has anyone done this and how they did it in a way that is a good implementation. Thanks here's the code I'm using

public class StatsFragment extends Fragment implements SensorEventListener {
    private static final String LOG_TAG = "STATS";
    private TextView stepCountTextView;
    private Button clearCounterButton;
    private Sensor stepSensor;
    private Integer stepCount = 0;
    private double MagnitudePrevious = 0;
    private SensorManager sensorManager;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_stats, container, false);

        stepCountTextView = view.findViewById(R.id.step_counter);
        clearCounterButton = view.findViewById(R.id.clear_button);

        sensorManager = (SensorManager) getActivity().getSystemService(getActivity().SENSOR_SERVICE);
        stepSensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);

        clearCounterButton.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {
                stepCount = 0;
                stepCountTextView.setText(stepCount.toString());
            }
        });

        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        if(stepSensor != null) {
            sensorManager.registerListener(this, stepSensor, SensorManager.SENSOR_DELAY_UI);
        }
        else {
            Toast.makeText(getActivity(), "Sensor not available!", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        //unregister sensor
        sensorManager.unregisterListener(this);
    }

    @Override
    public void onSensorChanged(SensorEvent sensorEvent) {
        if (sensorEvent != null) {
            float x_acceleration = sensorEvent.values[0];
            float y_acceleration = sensorEvent.values[1];
            float z_acceleration = sensorEvent.values[2];
            double Magnitude = Math.sqrt(x_acceleration*x_acceleration + y_acceleration*y_acceleration
                    + z_acceleration*z_acceleration);
            double MagnitudeDelta = Magnitude - MagnitudePrevious;
            MagnitudePrevious = Magnitude;

            if (sensorEvent.values[0] > 6){
                stepCount++;
            }
            stepCountTextView.setText(stepCount.toString());
        }
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int i) {

    }
}
miatech
  • 2,150
  • 8
  • 41
  • 78
  • 2
    There's dozens of possible approaches to this, each with pluses and minuses. There's an entire field to this type of problem called Digital Signal Processing. You're going to need to spend some time studying, this isn't a quick answer. Your best bet is to log the patterns of someone walking 100 steps exactly, and look for patterns in the data. – Gabe Sechan Jan 28 '20 at 18:14
  • 1
    Did you find any solutions to this? – Azizjon Kholmatov Apr 02 '21 at 10:57

0 Answers0