Once upon a time with Google Fit APIs

I am here to tell you a story of an invincible Google API made to make your life awesome. So, let me start off with an introduction of the main lead of our story, i.e. Google Fit. Fit is a simple yet quiescent App in our Android World which keeps you updated about all the activities performed keeping a log of your activities 24*7. It is like a best friend which keeps you healthy and fit and stays with you around the clock.

But, the magic is yet to begin. Wonder you want to build an app which tracks your activity like walk, trek or maybe even sleeping. How big a task is that?
Let me give you an example if you want to build a Step Counter App which counts steps based on your sensors. It is easy to do it via TYPE_STEP_COUNTER but to manage it on everyday basis requires a lot of custom logic and still it might not turn out to be correct(as a jerk on your device is also considered as step) and that will take lot of long painful nights to be accurate ,hundreds of lines of code and mark my word it won’t run same on all devices across Devices .

Google Fit

Here, enters our Superhero, Google Fit APIs. Fit APIs are like Superman, normal when used in Fit App but can do wonders for a developer when Fit APIs are used(The Superhero mode). Google Fit API gives you Historical Data, Session data, and even the Sensor Data.To include it in your app add,

dependencies {
compile 'com.google.android.gms:play-services-fitness:11.6.0'
compile 'com.google.android.gms:play-services-auth:11.6.0'
}

in your grade and connect itto the Google Fit API by creating a FitnessOptions instance, declaring the Fit API data types and access required by your app and connecting it by granting the necessary data access by initiating the authorization flow using,

if(!GoogleSignIn.hasPermissions(GoogleSignIn.getLastSignedInAccount(this), fitnessOptions)) {
GoogleSignIn.requestPermissions(
this, // your activity instance
GOOGLE_FIT_PERMISSIONS_REQUEST_CODE,
GoogleSignIn.getLastSignedInAccount(this),
fitnessOptions);
} else {
accessGoogleFit();
}

and that is it . Your superhero is at his work !!!

Now, you can have got anything and everything about the authorized user activities like Calories burnt, Calories Consumed,Steps walked, Distance travelled, Activity Performed, Maximum Elevation Reached, User Active time during the day, Locations, etc etc etc… Pheewwww, that’s quite a bit to expect a single API to do. By playing around with Calendar API you can achieve almost anything from the Superhero APIs(Google Fit).

I am sharing a small snippet on how to calculate current day’s step :

private void displayStepDataForToday() {
DailyTotalResult result = Fitness.HistoryApi.readDailyTotal(mGoogleApiClient, DataType.TYPE_STEP_COUNT_DELTA ).await(1, TimeUnit.MINUTES);
showDataSet(result.getTotal());
}

and showDataSetgoes like,

private void showDataSet(DataSet dataSet) {
DateFormat dateFormat = DateFormat.getDateInstance();
DateFormat timeFormat = DateFormat.getTimeInstance();
for (DataPoint dp : dataSet.getDataPoints()) {
Log.e("History", "Data point:");
Log.e("History", "tType: " + dp.getDataType().getName());
Log.e("History", "tStart: " + dateFormat.format(dp.getStartTime(TimeUnit.MILLISECONDS)) + " " + timeFormat.format(dp.getStartTime(TimeUnit.MILLISECONDS)));
Log.e("History", "tEnd: " + dateFormat.format(dp.getEndTime(TimeUnit.MILLISECONDS)) + " " + timeFormat.format(dp.getStartTime(TimeUnit.MILLISECONDS)));
for(Field field : dp.getDataType().getFields()) {
Log.e("History", "tField: " + field.getName() +
" Value: " + dp.getValue(field));
}
}
}

and that is it. You will get the Current Day step with Start time of your activity and end time as well.

Not Just this, Superhero APIs will even help you with handling the reset of Step walked values to ZERO at every stroke of Midnight which is (im)possible when you just collect data from Raw Sensors using TYPE_STEP_COUNTER and to a certain extent you have to write too much code to just achieve it. our Superhero does it all.!!!

Long Live Google Fit APIs

This was just a small example of what Superhero API does for you. You can also get user’s sleep cycle, sports data and many more. Adidas, Lifesum, Starva, RunKeeper, Runtastic, Xiaomi are few companies who use Google Fit APIs to make life simple and amazing.

With Background Restrictions coming for Android Oreo, Google Fit APIs works like a charm as it works on Google Play Services. For me personally, it came as a lifesaver and helped me build my app a lot quicker and faster.

Google Fit APIs are like Pocket Size Power House.


Once upon a time with Google Fit APIs was originally published in Mindorks on Medium, where people are continuing the conversation by highlighting and responding to this story.

Gurupriyan is a Software Engineer and a technology enthusiast, he’s been working on the field for the last 6 years. Currently focusing on mobile app development and IoT.

Please follow and like us:
0