How to retrieve daily running and walking steps from Google Fit API

*爱你&永不变心* 提交于 2019-12-11 02:43:56

问题


It is probably a newbie question but I have lost a one day to figure out w/o success. I'm using Google Fit API into my Android app and I need to show some of its data like running and walking daily steps. I have managed to show data but in time unit (f.e running in x min). I need to show it in steps unit.

The snipped below code shows how I retrieved data as time unit (in milliseconds):

  DataReadRequest readRequest = new DataReadRequest.Builder()
            .aggregate(DataType.TYPE_ACTIVITY_SEGMENT, DataType.AGGREGATE_ACTIVITY_SUMMARY)
            .bucketByTime(1, TimeUnit.DAYS)
            .setTimeRange(start, end, TimeUnit.MILLISECONDS)
            .build();


  Fitness.HistoryApi.readData(client, readRequest).setResultCallback(new ResultCallback<DataReadResult>() {
        @Override
        public void onResult(DataReadResult dataReadResult) {
            if (dataReadResult.getBuckets().size() > 0) {
                display.show("Bucket DataSet.size(): "
                        + dataReadResult.getBuckets().size());
               retrieveHistoryData(dataReadResult, client.getContext());
             }
        }

                            .....

   private void retrieveHistoryData(DataReadResult dataReadResult, Context context) {

    int walking = 0;
    int running = 0;
    int biking = 0;

    for (Bucket bucket : dataReadResult.getBuckets()) {
        List<DataSet> dataSets = bucket.getDataSets();
        for (DataSet dataSet : dataSets) {
            display.show("dataSet.dataType: " + dataSet.getDataType().getName());
            for (DataPoint dp : dataSet.getDataPoints()) {
                 walking += getBucketData(dp, FitnessActivities.WALKING);
                 running += getBucketData(dp, FitnessActivities.RUNNING);
                 biking += getBucketData(dp, FitnessActivities.BIKING);
            }
        }
    }

    Log.d(TAG, Constants.KEY_WALKING_STEPS + ": " + walking);
    Log.d(TAG, Constants.KEY_RUNNING_STEPS + ": " + running);
    Log.d(TAG, Constants.KEY_BIKING_STEPS + ": " + biking);
}

private int getBucketData(DataPoint dp, String activityName) {
    int count = 0;
    for (Field field : dp.getDataType().getFields()) {
        Log.d(TAG, "Field " + field.getName() +  " " + FitnessActivities.getName(dp.getValue(field).asInt()) + " = " + dp.getValue(field).asInt() + " field.describeContents()" + field.describeContents());
        if ("activity".equals(field.getName()) && activityName.contentEquals(FitnessActivities.getName(dp.getValue(field).asInt()))) {
            count +=  dp.getValue(field).asInt();

        }
    }
    return count;
}

Tnx in advance.

Best regards

MB


回答1:


You can get steps for various activities for each day by using the following code

DataReadRequest readRequest = new DataReadRequest.Builder()
            .aggregate(ESTIMATED_STEP_DELTAS, DataType.AGGREGATE_STEP_COUNT_DELTA)
            .bucketByActivityType(1, TimeUnit.SECONDS)
            .setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS)
            .build();

where ESTIMATED_STEP_DELTAS is defined as

DataSource ESTIMATED_STEP_DELTAS = new DataSource.Builder()
            .setDataType(DataType.TYPE_STEP_COUNT_DELTA)
            .setType(DataSource.TYPE_DERIVED)
            .setStreamName("estimated_steps")
            .setAppPackageName("com.google.android.gms")
            .build();

You can run this code in a loop to get data for as many days as you want. It will return data buckets for all activities that have steps involved and you can fetch activity name from the bucket by this method that returns activity name as String

bucket.getActivity();

Hope it helps! :)



来源:https://stackoverflow.com/questions/30817864/how-to-retrieve-daily-running-and-walking-steps-from-google-fit-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!