skip to Main Content

I am working about android sensors and I am trying to try the software based step counter sensor, but instead of starting from 0, it continues from 512 and I can understand that it writes the number of daily steps as the reason for this, but how can I set it to count from 0 when I am going to make a pedometer application ?

class MainActivity : AppCompatActivity() , SensorEventListener {

    private lateinit var  sensorManager : SensorManager

    private lateinit var  step_sensor : Sensor

    lateinit var txt : TextView

    override fun onCreate(savedInstanceState : Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        sensorManager = getSystemService(Context.SENSOR_SERVICE) as SensorManager

        step_sensor = sensorManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER)

        txt = findViewById(R.id.text1)

    }

    override fun onResume() {
        super.onResume()

        sensorManager.registerListener( this , step_sensor , SensorManager.SENSOR_DELAY_NORMAL)
    }

    override fun onSensorChanged(event : SensorEvent?) {

        txt.text = event?.values?.get(0).toString()
    }

    override fun onAccuracyChanged(sensor : Sensor?, accuracy : Int) {

        if (sensor == step_sensor ) {

            when (accuracy) {

                SensorManager.SENSOR_STATUS_ACCURACY_HIGH -> {

                        Toast.makeText(this,"Sensör yüksek doğrulukta çalışmaktadır ." , Toast.LENGTH_SHORT).show()
                }

                SensorManager.SENSOR_STATUS_ACCURACY_MEDIUM -> {

                        Toast.makeText(this,"Sensör orta doğrulukta çalışmaktadır ." , Toast.LENGTH_SHORT).show()
                }

                SensorManager.SENSOR_STATUS_ACCURACY_LOW -> {

                        Toast.makeText(this,"Sensör düşük doğrulukta çalışmaktadır ." , Toast.LENGTH_SHORT).show()
                }

                SensorManager.SENSOR_STATUS_UNRELIABLE -> {

                        Toast.makeText(this,"Sensör çok düşük doğrulukta çalışmaktadır ." , Toast.LENGTH_SHORT).show()
                }
            }
        }
    }

    override fun onPause() {
        super.onPause()

        sensorManager.unregisterListener( this )
    }

}

2

Answers


  1. Chosen as BEST ANSWER

    Since there is only one value coming from the step counter sensor, I found a solution as follows. I may have found a temporary solution

    class MainActivity : AppCompatActivity() , SensorEventListener {
    
        private lateinit var  sensorManager : SensorManager
    
        private lateinit var  step_detector : Sensor
    
        var step = -1
    
        lateinit var txt : TextView
    
        override fun onCreate(savedInstanceState : Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main)
    
            sensorManager = getSystemService(Context.SENSOR_SERVICE) as SensorManager
    
            step_detector = sensorManager.getDefaultSensor(Sensor.TYPE_STEP_DETECTOR)
    
            txt = findViewById(R.id.text1)
        }
    
        override fun onResume() {
            super.onResume()
    
            sensorManager.registerListener( this , step_detector , SensorManager.SENSOR_DELAY_NORMAL)
        }
    
        override fun onSensorChanged(event : SensorEvent?) {
    
            if ( event?.sensor == step_detector ) {
    
                step ++
            }
    
            txt.text = step.toString()
        }
    
        override fun onAccuracyChanged(sensor : Sensor?, accuracy : Int) {
    
            if (sensor == step_detector ) {
    
                when (accuracy) {
    
                    SensorManager.SENSOR_STATUS_ACCURACY_HIGH -> {
    
                            Toast.makeText(this,"Sensör yüksek doğrulukta çalışmaktadır ." , Toast.LENGTH_SHORT).show()
                    }
    
                    SensorManager.SENSOR_STATUS_ACCURACY_MEDIUM -> {
    
                            Toast.makeText(this,"Sensör orta doğrulukta çalışmaktadır ." , Toast.LENGTH_SHORT).show()
                    }
    
                    SensorManager.SENSOR_STATUS_ACCURACY_LOW -> {
    
                            Toast.makeText(this,"Sensör düşük doğrulukta çalışmaktadır ." , Toast.LENGTH_SHORT).show()
                    }
    
                    SensorManager.SENSOR_STATUS_UNRELIABLE -> {
    
                            Toast.makeText(this,"Sensör çok düşük doğrulukta çalışmaktadır ." , Toast.LENGTH_SHORT).show()
                    }
                }
            }
        }
    
        override fun onPause() {
            super.onPause()
    
            sensorManager.unregisterListener( this )
        }
    
    }
    

  2. This is because the sensor returns the steps since the last reboot of the device.

    A constant describing a step counter sensor.

    A sensor of this type returns the number of steps taken by the user
    since the last reboot while activated.

    To properly start tracking the amount of steps, you would want to use the first value as the starting value.

    var initialStepCount = -1
    var steps = 0
    
    override fun onSensorChanged(event : SensorEvent?) {
        val sensorStepCount = event?.values?.firstOrNull()
    
        sensorStepCount?.let {
            if (initialStepCount == -1) {
                initialStepCount = sensorStepCount 
            }
    
            if (event?.sensor == step_detector) {
                steps = sensorStepCount - initialStepCount
            }
        
            txt.text = step.toString()
        }
    }
    

    See the documentation for a more detailed description.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search