android - Getting Google Fit data that matches Google Fit -
i trying step count data on day day basis google fit display in application. seems code i'm using correct, getting data, numbers way off when compare data google fit app. example, in google fit (both on web , in android app) march 19 shows 2269 steps. in app march 19 shows 64 steps. data in app consistently lower power of 10. requesting data incorrectly? relevant code below.
client builder code
googleapiclient.builder builder = new googleapiclient.builder(main.instance) .addapi(fitness.api) .addscope(new scope(scopes.fitness_activity_read));
request builder code
private datareadrequest queryfitnessdata(long date) { // build single day range. calendar cal = calendar.getinstance(); cal.settimeinmillis(date); cal.set(calendar.hour_of_day, 0); cal.set(calendar.minute, 0); cal.set(calendar.second, 0); cal.set(calendar.millisecond, 0); long starttime = cal.gettimeinmillis(); cal.add(calendar.date, 1); cal.add(calendar.millisecond, -1); long endtime = cal.gettimeinmillis(); datareadrequest readrequest = new datareadrequest.builder() .enableserverqueries() .aggregate(datatype.type_step_count_delta, datatype.aggregate_step_count_delta) .bucketbytime(1, timeunit.days) .settimerange(starttime, endtime, timeunit.milliseconds) .build(); return readrequest; }
performing query
datareadresult datareadresult = fitness.historyapi.readdata(googleclient, readrequest).await(1, timeunit.minutes); if(datareadresult.getbuckets().size() > 0) { bucket bucket = datareadresult.getbuckets().get(0); for(dataset dataset : bucket.getdatasets()) { for(datapoint dp : dataset.getdatapoints()) { if(dp.getdatatype().equals(datatype.type_step_count_delta)) { for(field field : dp.getdatatype().getfields()) { if(field.getname().equals(field.field_steps.getname())) { if(listener != null) { listener.onstepcount(dp.getvalue(field).asint()); } } } } } } }
the first thing try rid of enableserverqueries() , query local data, may sync time issue.
outside of difficult tell without seeing of other data comes in datareadresult. i'd tempted put few log statements in see happening.
my code shown below, works me
for (bucket bucket : datareadresult.getbuckets()) { log.d(tag, "bucket start time: " + bucket.getstarttime(timeunit.seconds)); list<dataset> datasets = bucket.getdatasets(); totalstepsvalue = 0; (dataset dataset : datasets) { (datapoint dp : dataset.getdatapoints()) { (field field : dp.getdatatype().getfields()) { if (field.getname().equals("steps")) { totalstepsvalue += dp.getvalue(field).asint(); } } } } log.d(tag, "totalstepsvalue: " + totalstepsvalue); }
also try getting days data using 30 minute buckets , see if different.
Comments
Post a Comment