Steps Data receive from History api is not matched with google fit

*爱你&永不变心* 提交于 2020-01-03 03:20:08

问题


I want google fit steps count in my application, for that I am using History api, provided by google. I found that steps receive from history api is not matched with google fit even if i used same code provided by google. Below is my code.

  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();

        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();

        DataReadRequest readRequest = new DataReadRequest.Builder()
                .aggregate(ESTIMATED_STEP_DELTAS, DataType.AGGREGATE_STEP_COUNT_DELTA)
                .aggregate(DataType.TYPE_DISTANCE_DELTA, DataType.AGGREGATE_DISTANCE_DELTA)
                .aggregate(DataType.TYPE_ACTIVITY_SEGMENT, DataType.AGGREGATE_ACTIVITY_SUMMARY)
                .bucketByTime(1, TimeUnit.DAYS)
                .setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS)
                .build();

  Task<DataReadResponse> result = Fitness.getHistoryClient(getApplicationContext(),
                GoogleSignIn.getLastSignedInAccount(getApplicationContext())).readData(readRequest);

Any Help will be highly appreciated, I am stuck with this issue from long time and even not finding any proper tutorial from google.


回答1:


see the FAQ, which explain the behavior quite literally, which you're descibing.

this is possibly the closest one can get; assuming the latest Play Services installed on device:

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();

DataReadRequest readRequest = new DataReadRequest.Builder()
   .aggregate(ESTIMATED_STEP_DELTAS, DataType.AGGREGATE_STEP_COUNT_DELTA)
   .aggregate(DataType.TYPE_DISTANCE_DELTA, DataType.AGGREGATE_DISTANCE_DELTA)
   .aggregate(DataType.TYPE_CALORIES_EXPENDED, DataType.AGGREGATE_CALORIES_EXPENDED)
   .aggregate(DataType.TYPE_ACTIVITY_SEGMENT, DataType.AGGREGATE_ACTIVITY_SUMMARY)
   .bucketByTime(1, TimeUnit.DAYS)
   .setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS)
   .build();

it's quite the same, except TYPE_CALORIES_EXPENDED. alternatively, you could record you own sessions - which are not just estimated_steps, but have a unique session identifier.




回答2:


I am using history api in my app and compared the output with Google FIT app, they are quite same.

val c = GregorianCalendar()
    c.set(Calendar.HOUR_OF_DAY, 0) //anything 0 - 23
    c.set(Calendar.MINUTE, 0)
    c.set(Calendar.SECOND, 0)
    var endTime = /*date.time + dayInMS*/(getCurrentUTCDate()*1000)
    var startTime = (ConfigVariable.appLastUpdateTime*1000) //converting last updated data from second to MS
    val readRequest = DataReadRequest.Builder()
            .aggregate(DataType.TYPE_STEP_COUNT_DELTA, DataType.AGGREGATE_STEP_COUNT_DELTA)
            .bucketByTime(1  , TimeUnit.DAYS)
            .setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS)
            .build()
    val pendingResult = Fitness.HistoryApi.readData(mGoogleApiClient,readRequest)
    pendingResult.setResultCallback(this)


来源:https://stackoverflow.com/questions/53128859/steps-data-receive-from-history-api-is-not-matched-with-google-fit

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