Google Fit API Integration
While working on a project that involved wearables one of the features we were tasked with implementing was step tracking. After some research we discovered that the best way for us to implement this particular feature was to Google Fit API.
Using the Google Fit API helped us save a large amount of time on having to work on our own back end implementation for steps. Using the Fit API also provides the users access to all the useful features provided by the Google Fit APP.
This article will cover the logic for integrating the Google Fit API with a BLE device to handle steps data sync and use the Google Fit API as a cloud storage for the steps data.
The simple idea behind the architecture shown in the diagram above was to have the following three main components:
BleManager: the BLE manager is responsible for all the BLE communication with the wearable device and to provide a simple RxJava backed API to send and receive data from the device.
FitManager: the fit manager is responsible for handling all the communication with the Google Fit API, it also provides a simple RxJava backed APIs to read and write steps data to the Fit API.
StepsSyncManager: the steps sync manager is responsible for connecting the
FitManagertogether by retrieving all the steps data from the
BleManagerand passing them to the
FitManagerto be stored using the Google Fit History API.
When implementing the Google Fit API we wanted to use RxJava to simplify some of the complicated APIs that would be commonly used. This led to the creation of
Connecting to the Google Fit API
To start using the Google Fit API we first have to create a new Google API client, this can be done by using the Google Fit API client builder as shown below.
As shown above, to build the API client we have to specify a couple of values.
- The first value is the API we want to use, in our case we wanted to use the Google Fit history API which can be specified using the
- We also have to specify the access scope we want to that particular API, in our case we need both read and write access to the history API to be able to save new steps data and read them again later. This is specified using the constant value
- Finally we need to provide a connection callbacks listener and connection failed listener. In the example above we are taking the results from these two listeners and then passing them to an RxJava
PublishSubject.This gives us the ability to subscribe to these updates anywhere in the app where information about the connection state is required.
Retrieving total steps recorded between two timestamps
After connecting to the Google Fit API client we can start retrieving the steps data previously recorded. The method below is used to retrieve the total steps recorded between two timestamps and returning them as an RxJava observable.
Retrieving total steps recorded for the day
Sometimes we also want to retrieve the total steps recorded for the current data, thankfully the Fit API provides a simple way to retrieve the daily total as shown below.
Inserting new steps
Finally we need the ability to insert new steps data, the methods below allow us to insert steps data for either a single data point or multiple data points. Each data point consist of the following:
startTime: the start timestamp for when these steps were recorded.
endTime: the end timestamp for when these steps were recorded.
steps: the total steps recorded steps between the specified timestamps.
BleManager is responsible for providing the ability to easily connect to the BLE device, send commands, and retrieve data from the device. To Simplify our
BleManager implementation we looked for a good library that wraps around the core Android BLE APIs and provides RxJava support. After some research we decided to go with RxAndroidBLE, a really powerful library that removes a lot of the headache that comes with using the core Android BLE APIs.
To send commands to the BLE device we first need to retrieve the BLE characteristic we want to send the commands to. After retrieving the characteristic we have to specify the following two values:
- Write Type: the write type of the command being sent, it can be one of:
- Value: the value for the command we want to send.
Finally after specifying the write type and command value we can send back the characteristic to the BLE device as shown below.
Receiving data from BLE notifications
Data are sent from the BLE device using GATT notifications, so when we want to receive data from the BLE device we would setup a notification to a specific BLE characteristic and listen to its value changes. Due to the BLE limitation around the size of the data allowed to be transferred at a time, some responses have to be split into multiple parts with a header and trailer used to indicate the start and end of the transfer.
The example below shows the code for setting up notifications that will listen to steps data responses for a specific date. These responses come in multiple parts so they are being combined into a single object and then published using an RxJava
StepsSyncManager is responsible for connecting the
FitManager together, by retrieving steps data from our
BleManager and pushing them to the
StepsSyncManager is started through a sticky service and kept running in the background all the time. This insures steps data are kept up to data with the Google Fit API, and if the users were to open the Google Fit App at any moment they would be represented with an accurate count of their steps for the day.
Steps syncing is divided into two parts: the first is retrieving the steps data from our BLE device and the second pushing that data to the Google Fit API.
Retrieving steps data from BLE
To regularly receive step data updates from our BLE device, we had to setup a new BLE notification on the characteristic responsible for publishing steps data updates.
Once we were subscribed to the steps data notifications we had to slow down the amount of updates we were receiving, as our BLE device pushes multiple updates a second and we wanted to avoid hammering the Google Fit API with hundreds of requests every couple of minutes. To achieve that we used the RxJava
Sample operator, which allowed us to only receive the last published update every 30 minutes.
Pushing steps data to Google Fit API
After we’ve received new steps data update , we need to push these updates to the Google Fit API. As our steps data updates contain the total steps count for the day and we only need to push the number of the steps since our last update, we first need to retrieve the total steps count since our last update from the Google Fit API which then we can use to work out the steps count delta. Finally after we have worked out the amount of steps walked since our last update we can insert a new steps record using our
FitManager as shown below.
The Google Fit API provides great value at a very low cost for both the developer and user. It removes all the complications that come with implementing your own backend to store all the steps data, while providing the user with a tremendous set of tools to interact and better analyze the collected steps data using the Google Fit App.