Fetching Google Fit Data into android application

六月ゝ 毕业季﹏ 提交于 2019-12-19 09:21:58

问题


How can we get the data stored in google fit cloud for a specific user? I tried using History API but no data is being displayed. Then i tried entering some data vai History api, now I could see these data only via history api not the complete data that actually resides in fit.

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

Data Request is

DataReadRequest readRequest = new DataReadRequest.Builder()

                .aggregate(DataType.TYPE_STEP_COUNT_DELTA, DataType.AGGREGATE_STEP_COUNT_DELTA)

                .bucketByTime(1, TimeUnit.DAYS)
                .setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS)
                .build();

回答1:


Based on your question, it's hard to know what you're expecting but not receiving. More detail would be helpful.

I'm guessing it's because Fit is returning the default merged stream of steps. Basically, if two apps both report 1,000 steps for the same 60 minute period, Fit will assume that they're duplicative and "merge" them for the response.

Try losing the bucket function and see if you get the raw steps. This code works for me:

    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;

        }
    }



回答2:


Have you subscribe frist? I have same issue but solved after Fitness.RecordingApi.subscribe(mClient, myDataType)

myDataType is DataType.TYPE_STEP_COUNT_DELTA or DataType.TYPE_STEP_COUNT_CUMULATIVE




回答3:


I really don't understand how to insert the step counter value inside the insertFitnessData(). Basic History google program already has a DataSet that holds a step value of 1000. How should I change that part so it can count the steps that I am doing over the last hour?

    DataSet dataSet = DataSet.create(dataSource);
    // For each data point, specify a start time, end time, and the data  
    value -- in this case,
    // the number of new steps.
    DataPoint dataPoint = dataSet.createDataPoint()
            .setTimeInterval(startTime, endTime, TimeUnit.MILLISECONDS);

Should I use the OnDataPointListener to recieve callbacks? I m confused!




回答4:


If you want to read data from the cloud, you must add following call to DataReadRequest.Builder: enableServerQueries()

So your code would then look like this:

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



回答5:


In my case, I can still use bucket, and it worked well until it didn't obey me at all. Anyway, I fixed it. There're some probable causes:

1) Have you subscribed this type of data yet?

2) Your app doesn't connect properly with google service. Have you created OAuth Client ID from Google develop console? This's compulsory in instruction of google to connect to its GG Fit service (Please notice that, if you clone another app, on the same computer or not, you need to re-create another OAuth Client ID and one more thing you need 2 separated account, one to login Google develop console to create OAuth Client ID and one to sign in after starting your app, and it will ask you to sign in to accept its permission,... not sure why it's is, but it would work)

Note: Btw you can make a search about Google setting in your device (Setting --> Google), Here you can find which app connects to google service (including GG Fit service). I recommend you disconnect all and delete OAuth Client ID, your app, then re-create all of them!

Mttdat.



来源:https://stackoverflow.com/questions/28234525/fetching-google-fit-data-into-android-application

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