简体   繁体   中英

Android SensorManager vs Google Fit for step count

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, ie 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!

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, ie 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM