2

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:

Using SensorManager:

val sensorManager = getSystemService(Context.SENSOR_SERVICE) as SensorManager
val sensor: Sensor? = sensorManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER)

Using Google Fit API:

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!

James Allen
  • 6,406
  • 8
  • 50
  • 83

1 Answers1

6

On a high level:

SensorManager gives you direct access to the sensors that can be used to record (amongst other things) step count. This gives you raw, unprocessed data in real-time.

Google Fit uses the sensors on your device(s) to collect (amongst other things) step count. They also (might?) do some magic to improve this data. And make sure that if you go out for a run with both your phone and watch it only logs it once. Google Fit gives you access to historical data.

To answer your questions:

  • Do they get step count data from the same source, i.e. will they give the same numbers?

Yes, they get the data from the same source. However, in Google Fit you have access to data from all your connected devices. From the SensorManager you only have access to the data on that specific device. They will not necessarily give the exact same number.

  • Does the Google Fit terms and conditions apply to using SensorManager data?

No, the SensorManager doesn't come with any specific terms and conditions. It's part of the Android platform and can be used just like any other feature therein.

  • Do both retrieve data from Wear OS if the user is wearing one?

As mentioned above the SensorManager only records data on the device where the code is running. Google Fit is able to collect data from a Wear OS device, if it's configured to do so by the user.

  • Why are there two approaches, what's the difference?

I've already explained some of the key differences above. To answer the "why": Different application behaviors requires different solutions. Sometimes the SensorManager is the best alternative, and sometimes Google Fit is.

TofferJ
  • 4,678
  • 1
  • 37
  • 49
  • Great information, thanks @TofferJ :) Just one follow up question. SensorManager gives you access to Step Count, which clearly isn't raw sensor data - it must involve some kind of processing of raw sensor data into a number of steps. My guess would be that they probably share the same core algorithm for calculating that, but that Google Fit maybe adds more data (eg from a watch if present) to get a better result - is that right? – James Allen Apr 02 '20 at 10:16
  • That's a valid point. The SensorManager does give you access to raw data (https://developer.android.com/guide/topics/sensors/sensors_overview) but in the case of Step Count it's obviously doing some calculations to interpret the movement as a step or not. I would assume that Google Fit reads the Step Count and not the raw data (but that is only an assumption). Yes, Google Fit does use all different available devices to get better accuracy. – TofferJ Apr 02 '20 at 11:56