+ All Categories
Home > Engineering > Integrating GoogleFit into Android Apps

Integrating GoogleFit into Android Apps

Date post: 07-Feb-2017
Category:
Upload: giles-payne
View: 32 times
Download: 0 times
Share this document with a friend
13
Integrating GoogleFit into Android Apps Tokyo Android Meetup 21st December 2016
Transcript
Page 1: Integrating GoogleFit into Android Apps

Integrating GoogleFit into Android Apps

Tokyo Android Meetup 21st December 2016

Page 2: Integrating GoogleFit into Android Apps

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

Page 3: Integrating GoogleFit into Android Apps

About the Green+ App

https://play.google.com/store/apps/details?id=com.mytenten.sunpo&hl=jp

Page 4: Integrating GoogleFit into Android Apps

Implementing the Walking Point feature

iOS - HealthKit

Android - GoogleFit

Page 5: Integrating GoogleFit into Android Apps

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!!

Process the query results

Page 6: Integrating GoogleFit into Android Apps

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

Page 7: Integrating GoogleFit into Android Apps

Add dependency to build.gradle

dependencies {...

compile 'com.google.android.gms:play-services-fitness:10.0.0'...}

Page 8: Integrating GoogleFit into Android Apps

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

Page 9: Integrating GoogleFit into Android Apps

Call enableAutoManageGoogleApiClient googleApiClient = new GoogleApiClient.Builder(TenTenApp.getAppContext())

….enableAutoManage(fragmentActivity, 0, new GoogleApiClient.OnConnectionFailedListener() {

@Overridepublic 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)

Page 10: Integrating GoogleFit into Android Apps

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/DataType

Page 11: Integrating GoogleFit into Android Apps

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/DataType

Page 12: Integrating GoogleFit into Android Apps

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(); } } } } }}

Page 13: Integrating GoogleFit into Android Apps

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


Recommended