Working with Prototypes and the Google Fit API

During the fifth sprint for the Fitness Explorer app I started more deeply exploring the Google Fit API. The earliest version of the app started to take shape during this phase, and I learned a lot about using Google's APIs. This article will guide you through how I used the Google Fit API.

Connecting to a Google API

Connecting to a Google API is actually quite complicated. It requires multiple steps and an understanding of what an API actually is. By definition an API is a set of routines, protocols, and tools for building software applications. Basically, an API is an instruction set for how to integrate a tool into your application. Before I started integrating Google Fit into the Fitness Explorer app I created a Prototype, called FitTest, so that I could test querying with Google Fit and learn all I could before integration. Step 1: Create a Google account. Most people will already have one if you're planning on doing Android development, but this step still needs to be stated. Step 2: Update to the latest client library for Google Play on your development host This includes updating the Google Play app on your development device as well as adding the necessary gradle dependencies. Refer to this link for the list of Google Play Services available for Android: https://developers.google.com/android/guides/setup Step 3: Get an OAuth 2.0 client ID. For this step you'll need to go to console.developers.google.com and create a new project. This may take a few minutes. Once the project is created it will bring you to a screen which looks like this: googlefitblog Search for the API you want to use in the search bar, or choose it from the list below. Once you find the API you're looking for click the "Enable" button on the top of the screen which will enable the API for this particular project. Once it's enabled click the "Go to Credentials" button to add information for Google as to how you'll be connecting to the API. Here you'll need to add some information which you'll need to get from your project and from a console command. You'll need the SHA-1 signing certificate which is a secure code only you'll know to access the API. Create a folder called "keystore" somewhere on your computer and copy the path. Go back to your IDE of choice and generate a new signed APK. For Android Studio developers it's under Build -> Generate Signed APK. From here you'll need to open a command line and copy this line: keytool -exportcert -keystore [path to your keystore] -list -v From here you'll see something that looks very similar to this: Alias name: androiddebugkey Creation date: Jan 01, 2013 Entry type: PrivateKeyEntry Certificate chain length: 1 Certificate[1]: Owner: CN=Android Debug, O=Android, C=US Issuer: CN=Android Debug, O=Android, C=US Serial number: 4aa9b300 Valid from: Mon Jan 01 08:04:04 UTC 2013 until: Mon Jan 01 18:04:04 PST 2033 Certificate fingerprints: MD5: AE:9F:95:D0:A6:86:89:BC:A8:70:BA:34:FF:6A:AC:F9 SHA1: BB:0D:AC:74:D3:21:E1:43:07:71:9B:62:90:AF:A1:66:6E:44:5D:75 Signature algorithm name: SHA1withRSA Version: 3 Copy the SHA1 code and paste it in the area of the developers console which asks for it. Now you'll need the package name from your AndroidManifest.xml file.

Paste this in the appropriate place and press the "Create client ID" button. Step 4: Add the appropriate API as a dependency in your build.gradle file. This step is pretty straightforward. Just modify your build.gradle file.

Step 5: Connect to your API. Finally, you'll need to connect to the API using a Google API Client object. Make sure to have your class implement GoogleApiClient.ConnectionCallbacks. You'll also need to override two methods.=: the onConnected method and the onConnectionSuspended method.

Once you've created the GoogleApiClient and have overridden the correct methods as above, you'll need to create a method called InsertAndVerifyDataTask which will extend an AsyncTask. Basically this creates a new thread which will run in the background of the app which collects and parses the data retrieved from Google Fit. This is where you'll do the querying from Google Fit.

Querying the Google Fit API

Retrieving information from the Google Fit API is quite simple, really. You'll need to create something called a Data Read Request, which is exactly as it sounds, just a request to the API for specific data.

Once you've created the Data Read Request you'll then need to create a Data Read Result and pass the newly created Data Read Request to it.

From here you'll receive a DataReadResult object which holds a Bucket, which is a grouping of DataPoints with Fields inside, and you can get your data from there. I needed an activity for my app that is associated with calories lost during a given period of time. To implement this I found all of the activities the user completed during a set time period (such as two weeks).

Once that was done and I had all of the activities in an ArrayList, and I iterated over the list while doing a search for calories lost during each activity.

Put together it should look something like this:

Here's a list of all the activity types Google Fit tracks and their corresponding numbers: https://developers.google.com/fit/rest/v1/reference/activity-types

Next Sprint

My next sprint will cover some considerations for front-end design including Material design, raster assets, and vector assets on Android.

GrapeCity

GrapeCity Developer Tools
comments powered by Disqus