1

I need to get my fit data to reflect that which shows in the android fit application. I have tried using the Sensor API, but this number reflects the amount of steps taken since the boot of the device, not for the day.

How to get step count from Google Fit REST API like Google Fit app?

I have also tried using the History API and querying for stepcount_cumulative and step_delta. Both of these values do not reflect the values of the fit API.

I have come up with the idea of storing the previous day value in the sharedPref and subtracting the difference to get the true value. Other calculations for when the device resets need to be factored in as well.

Is there any easier way of getting the values I need?

Cole Murray
  • 546
  • 6
  • 12
  • Are you trying to read step data that other ppl have logged to google fit, or are you trying to use the step sensor on the device itself? I think the question might be a bit unclear in that regard – Glenn Bech Mar 23 '15 at 22:07
  • I am open to either implementation. I would prefer to pull the data from the Fit api as it will be more accurate in my opinion. – Cole Murray Mar 23 '15 at 23:10

2 Answers2

1

Try this new method HistoryApi.readDailyTotal(). (Google play service v24 required)

PendingResult<DailyTotalResult> result = HistoryApi.readDailyTotal(client, TYPE_STEP_COUNT_DELTA);
   DailyTotalResult totalResult = result.await(30, SECONDS);
   if (totalResult.getStatus().isSuccess()) {
     DataSet totalSet = totalResult.getTotal();
     long total = totalSet.isEmpty()
         ? 0
         : totalSet.getDataPoints().get(0).getValue(FIELD_STEPS).asInt();
   } else {
     // handle failure
   }

https://developer.android.com/reference/com/google/android/gms/fitness/HistoryApi.html

TZHX
  • 5,291
  • 15
  • 47
  • 56
JamesC
  • 61
  • 6
0

This worked for me: https://github.com/rsteckler/Android-Google-Fit-Service-Skeleton

Function you are looking for:

   private void getStepsToday() {
        Calendar cal = Calendar.getInstance();
        Date now = new Date();
        cal.setTime(now);
        long endTime = cal.getTimeInMillis();
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        long startTime = cal.getTimeInMillis();

        final DataReadRequest readRequest = new DataReadRequest.Builder()
                .read(DataType.TYPE_STEP_COUNT_DELTA)
                .setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS)
                .build();

        DataReadResult dataReadResult =
                Fitness.HistoryApi.readData(mGoogleApiFitnessClient, readRequest).await(1, TimeUnit.MINUTES);

        DataSet stepData = dataReadResult.getDataSet(DataType.TYPE_STEP_COUNT_DELTA);

        int totalSteps = 0;

        for (DataPoint dp : stepData.getDataPoints()) {
            for(Field field : dp.getDataType().getFields()) {
                int steps = dp.getValue(field).asInt();

                totalSteps += steps;

            }
        }

        publishTodaysStepData(totalSteps);
    }
JoKr
  • 4,976
  • 8
  • 27
  • 39
  • This is returning the categorized amount accurately. It is not including the misc steps though. Is it possible to get the API to return those also? – Cole Murray Mar 23 '15 at 23:11
  • i was just playing with Fit API for some short time, i don't know :/ – JoKr Mar 23 '15 at 23:15