Google Fit API – Java SDK example step count

There are not many examples for Google fit Java SDK out there. In general also for the Google fit API I could not find a good example. I really struggled 2h till I understood how the Google Fit Aggregate API works and I want to share my sample code. I simply wanted to have the step count for a day.
 

Fitness fitness = getFitnessAPI();

// prepare start- and endtime
Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
calendar.set(2015,2,13,0,0,0); // 13. Feb 2015 00:00:00
final long startTime = calendar.getTimeInMillis();
calendar.add(Calendar.DATE, 1); // + 1 Day
final long endTime = calendar.getTimeInMillis();

AggregateRequest aggRequest = new AggregateRequest();

// set start- and endtime
aggRequest.setStartTimeMillis(startTime);
aggRequest.setEndTimeMillis(endTime);

BucketByTime bucketByTime = new BucketByTime();
bucketByTime.setDurationMillis(86400000l); // 24h buckets
aggRequest.setBucketByTime(bucketByTime);

AggregateBy aggregateBy1 = new AggregateBy();
aggregateBy1.setDataTypeName("com.google.step_count.delta");

ArrayList<AggregateBy> list = new ArrayList<AggregateBy>();
list.add(aggregateBy1);
aggRequest.setAggregateBy(list);

AggregateResponse aggResponse = fitness.users().dataset()
                                .aggregate("me", aggRequest).execute();

//System.out.println(aggResponse.toPrettyString());

List<AggregateBucket> bucketList = aggResponse.getBucket();
for (AggregateBucket bucket : bucketList) {
    List<Dataset> datasets = bucket.getDataset();
    for (Dataset d : datasets) {
        List<DataPoint> points = d.getPoint();
        for(DataPoint dp : points){
            System.out.println(dp.getValue());
        }
    }
}

If you want to know how to create the getFitnessAPI() function, please let me know. Also I want to try out PayPal donations. This is the first time for me using this. Lets find out if it’s worth it 😉

 

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert

Diese Website verwendet Akismet, um Spam zu reduzieren. Erfahre mehr darüber, wie deine Kommentardaten verarbeitet werden.