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
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
This is because the sensor returns the steps since the last reboot of the device.
To properly start tracking the amount of steps, you would want to use the first value as the starting value.
See the documentation for a more detailed description.