I've been researching how to read step count in my Android app, and found two completely different ways to do it in the documentation:
val sensorManager = getSystemService(Context.SENSOR_SERVICE) as SensorManager
val sensor: Sensor? = sensorManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER)
private class VerifyDataTask extends AsyncTask<Void, Void, Void> {
protected Void doInBackground(Void... params) {
long total = 0;
PendingResult<DailyTotalResult> result = Fitness.HistoryApi.readDailyTotal(mClient, DataType.TYPE_STEP_COUNT_DELTA);
DailyTotalResult totalResult = result.await(30, TimeUnit.SECONDS);
if (totalResult.getStatus().isSuccess()) {
DataSet totalSet = totalResult.getTotal();
total = totalSet.isEmpty()
? 0
: totalSet.getDataPoints().get(0).getValue(Field.FIELD_STEPS).asInt();
} else {
Log.w(TAG, "There was a problem getting the step count.");
}
Log.i(TAG, "Total steps: " + total);
return null;
}
}
There is no indication as to the difference between these two approaches, does anybody know:
- Do they get step count data from the same source, i.e. will they give the same numbers?
- Does the Google Fit terms and conditions apply to using SensorManager data?
- Do both retrieve data from Wear OS if the user is wearing one?
- Why are there two approaches, what's the difference?
Similar question about heart rate here, but with no answers. Any guidance much appreciated!