I want to get data from the Heart Rate sensor every x seconds. I tried it with postDelayed() but that only works for reading the data every x seconds, but the sensor is running in that time and the battery drains very fast. I'm trying to find a way to start the sensor, get the data and stop the sensor every x seconds.
Here's what I did so far:
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
checkHR = true;
handler.postDelayed(this, 10000);
}
}, 1);
public void onSensorChanged(SensorEvent event) {
if(checkHR == true){
if (event.sensor.getType() == Sensor.TYPE_HEART_RATE) {
String msg = "" + (int)event.values[0];
heartRate = (int)event.values[0];
Log.d("Heart rate is:", msg);
checkHR = false;
}
}