Integrating GoogleFit into
Android Apps
Tokyo Android Meetup 21st December 2016
About The Presenter
Giles Payne
Senior Android Developer - TenTen Corp
Developing for Android since Cupcake
BA Honours Mathematics and Computation, Oxford University
Country of Origin: Scotland
About the Green+ App
https://play.google.com/store/apps/details?id=com.mytenten.sunpo&hl=jp
Implementing the Walking Point feature
iOS - HealthKit
Android - GoogleFit
Integrating GoogleFit into an Android app
Get an API Key/Register your app's package on the Google
API Console
Add dependency to build.gradle
Create the Google API Client
Call enableAutoManage
Subscribe to a data source
Query the data source!!
Get an API key
Get the key from: https://console.developers.google.com/flows/enableapi?apiid=fitness&pli=1
Follow the instructions at: https://developers.google.com/fit/android/get-api-key
You don't actually need to do anything with the key.
Just being registered is enough
Add dependency to build.gradle
dependencies {
...
compile 'com.google.android.gms:play-services-fitness:10.0.0'
...
}
Create the Google API Client
GoogleApiClient googleApiClient = new GoogleApiClient.Builder(TenTenApp.getAppContext())
.addApi(Fitness.RECORDING_API)
.addApi(Fitness.HISTORY_API)
.addScope(new Scope(Scopes.FITNESS_ACTIVITY_READ))
…
…
.build();
Depending on what you want to do you may need different APIs (for example
SENSORS_API) or different scopes (for example FITNESS_ACTIVITY_READ_WRITE)
https://developers.google.com/android/reference/com/google/android/gms/common/api/GoogleApiClient
Call enableAutoManage
GoogleApiClient googleApiClient = new GoogleApiClient.Builder(TenTenApp.getAppContext())
…
.enableAutoManage(fragmentActivity, 0, new GoogleApiClient.OnConnectionFailedListener() {
@Override
public void onConnectionFailed(ConnectionResult result) {
if (result.getErrorCode() == ConnectionResult.CANCELED) {
cancelled = true;
}
}
})
.build();
enableAutoManage handles a lot of things - including
● Requesting user permission to use the service
● Selection of a Google account with which to associate the fitness data
● Installation of any required software that is missing (Google Play Developer Services)
https://developers.google.com/android/reference/com/google/android/gms/common/api/GoogleApiClient.Builder.html
#enableAutoManage(android.support.v4.app.FragmentActivity,com.google.android.gms.common.api.GoogleApiClient.OnConnectionFailedListener)
Subscribe to a data source
Fitness.RecordingApi.subscribe(googleApiClient, DataType.TYPE_STEP_COUNT_DELTA)
.setResultCallback(new ResultCallback<Status>() {
@Override
public void onResult(Status status) {
if (status.isSuccess()) {
// subscribed successfully
} else {
// error subscribing
}
}
});
TYPE_STEP_COUNT_DELTA will report walking steps. For the full range of available
data types refer to:
https://developers.google.com/android/reference/com/google/android/gms/fitness/data/DataTyp
e
Query the data source!!
DataReadRequest dataReadRequest = new DataReadRequest.Builder()
.aggregate(DataType.TYPE_STEP_COUNT_DELTA, DataType.AGGREGATE_STEP_COUNT_DELTA)
.bucketByTime(1, TimeUnit.DAYS)
.setTimeRange(start, end, TimeUnit.MILLISECONDS)
.build();
AGGREGATE data types will generally be more useful however raw data types are also
available. Queries can be synchronous or asynchronous.
https://developers.google.com/android/reference/com/google/android/gms/fitness/data/DataTyp
e
Process the query results
for (Bucket bucket : dataReadResult.getBuckets()) {
List<DataSet> dataSets = bucket.getDataSets();
for (DataSet dataSet : dataSets) {
for (DataPoint dataPoint : dataSet.getDataPoints()) {
if (dataPoint.getDataType().getName().equals(STEP_COUNT_DATA_TYPE)) {
for(Field field : dataPoint.getDataType().getFields()) {
if (field.getName().equals(STEP_COUNT_FIELD_NAME)) {
long dayStart = dataPoint.getStartTime(TimeUnit.MILLISECONDS);
int stepCount = dataPoint.getValue(field).asInt();
}
}
}
}
}
}
GoogleFit Gotchas
Where to put the API key
Nowhere!!
Reconnecting
In some cases you explicitly need to reconnect or handle a connection failed event.
If you don’t do this then data queries will start to fail after the app is open for longer 48 hour

Integrating GoogleFit into Android Apps

  • 1.
    Integrating GoogleFit into AndroidApps Tokyo Android Meetup 21st December 2016
  • 2.
    About The Presenter GilesPayne Senior Android Developer - TenTen Corp Developing for Android since Cupcake BA Honours Mathematics and Computation, Oxford University Country of Origin: Scotland
  • 3.
    About the Green+App https://play.google.com/store/apps/details?id=com.mytenten.sunpo&hl=jp
  • 4.
    Implementing the WalkingPoint feature iOS - HealthKit Android - GoogleFit
  • 5.
    Integrating GoogleFit intoan Android app Get an API Key/Register your app's package on the Google API Console Add dependency to build.gradle Create the Google API Client Call enableAutoManage Subscribe to a data source Query the data source!!
  • 6.
    Get an APIkey Get the key from: https://console.developers.google.com/flows/enableapi?apiid=fitness&pli=1 Follow the instructions at: https://developers.google.com/fit/android/get-api-key You don't actually need to do anything with the key. Just being registered is enough
  • 7.
    Add dependency tobuild.gradle dependencies { ... compile 'com.google.android.gms:play-services-fitness:10.0.0' ... }
  • 8.
    Create the GoogleAPI Client GoogleApiClient googleApiClient = new GoogleApiClient.Builder(TenTenApp.getAppContext()) .addApi(Fitness.RECORDING_API) .addApi(Fitness.HISTORY_API) .addScope(new Scope(Scopes.FITNESS_ACTIVITY_READ)) … … .build(); Depending on what you want to do you may need different APIs (for example SENSORS_API) or different scopes (for example FITNESS_ACTIVITY_READ_WRITE) https://developers.google.com/android/reference/com/google/android/gms/common/api/GoogleApiClient
  • 9.
    Call enableAutoManage GoogleApiClient googleApiClient= new GoogleApiClient.Builder(TenTenApp.getAppContext()) … .enableAutoManage(fragmentActivity, 0, new GoogleApiClient.OnConnectionFailedListener() { @Override public void onConnectionFailed(ConnectionResult result) { if (result.getErrorCode() == ConnectionResult.CANCELED) { cancelled = true; } } }) .build(); enableAutoManage handles a lot of things - including ● Requesting user permission to use the service ● Selection of a Google account with which to associate the fitness data ● Installation of any required software that is missing (Google Play Developer Services) https://developers.google.com/android/reference/com/google/android/gms/common/api/GoogleApiClient.Builder.html #enableAutoManage(android.support.v4.app.FragmentActivity,com.google.android.gms.common.api.GoogleApiClient.OnConnectionFailedListener)
  • 10.
    Subscribe to adata source Fitness.RecordingApi.subscribe(googleApiClient, DataType.TYPE_STEP_COUNT_DELTA) .setResultCallback(new ResultCallback<Status>() { @Override public void onResult(Status status) { if (status.isSuccess()) { // subscribed successfully } else { // error subscribing } } }); TYPE_STEP_COUNT_DELTA will report walking steps. For the full range of available data types refer to: https://developers.google.com/android/reference/com/google/android/gms/fitness/data/DataTyp e
  • 11.
    Query the datasource!! DataReadRequest dataReadRequest = new DataReadRequest.Builder() .aggregate(DataType.TYPE_STEP_COUNT_DELTA, DataType.AGGREGATE_STEP_COUNT_DELTA) .bucketByTime(1, TimeUnit.DAYS) .setTimeRange(start, end, TimeUnit.MILLISECONDS) .build(); AGGREGATE data types will generally be more useful however raw data types are also available. Queries can be synchronous or asynchronous. https://developers.google.com/android/reference/com/google/android/gms/fitness/data/DataTyp e
  • 12.
    Process the queryresults for (Bucket bucket : dataReadResult.getBuckets()) { List<DataSet> dataSets = bucket.getDataSets(); for (DataSet dataSet : dataSets) { for (DataPoint dataPoint : dataSet.getDataPoints()) { if (dataPoint.getDataType().getName().equals(STEP_COUNT_DATA_TYPE)) { for(Field field : dataPoint.getDataType().getFields()) { if (field.getName().equals(STEP_COUNT_FIELD_NAME)) { long dayStart = dataPoint.getStartTime(TimeUnit.MILLISECONDS); int stepCount = dataPoint.getValue(field).asInt(); } } } } } }
  • 13.
    GoogleFit Gotchas Where toput the API key Nowhere!! Reconnecting In some cases you explicitly need to reconnect or handle a connection failed event. If you don’t do this then data queries will start to fail after the app is open for longer 48 hour