问题
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