Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
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 Mat...
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 depende...
Get an API key
Get the key from: https://console.developers.google.com/flows/enableapi?apiid=fitness&pli=1
Follow the inst...
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())
.add...
Call enableAutoManage
GoogleApiClient googleApiClient = new GoogleApiClient.Builder(TenTenApp.getAppContext())
…
.enableAu...
Subscribe to a data source
Fitness.RecordingApi.subscribe(googleApiClient, DataType.TYPE_STEP_COUNT_DELTA)
.setResultCallb...
Query the data source!!
DataReadRequest dataReadRequest = new DataReadRequest.Builder()
.aggregate(DataType.TYPE_STEP_COUN...
Process the query results
for (Bucket bucket : dataReadResult.getBuckets()) {
List<DataSet> dataSets = bucket.getDataSets(...
GoogleFit Gotchas
Where to put the API key
Nowhere!!
Reconnecting
In some cases you explicitly need to reconnect or handle...
Upcoming SlideShare
Loading in …5
×

0

Share

Download to read offline

Integrating GoogleFit into Android Apps

Download to read offline

Simple introduction to integrating GoogleFit support into Android Apps

Related Books

Free with a 30 day trial from Scribd

See all

Related Audiobooks

Free with a 30 day trial from Scribd

See all
  • Be the first to like this

Integrating GoogleFit into Android Apps

  1. 1. Integrating GoogleFit into Android Apps Tokyo Android Meetup 21st December 2016
  2. 2. 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
  3. 3. About the Green+ App https://play.google.com/store/apps/details?id=com.mytenten.sunpo&hl=jp
  4. 4. Implementing the Walking Point feature iOS - HealthKit Android - GoogleFit
  5. 5. 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!!
  6. 6. 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
  7. 7. Add dependency to build.gradle dependencies { ... compile 'com.google.android.gms:play-services-fitness:10.0.0' ... }
  8. 8. 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
  9. 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. 10. 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
  11. 11. 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
  12. 12. 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(); } } } } } }
  13. 13. 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

Simple introduction to integrating GoogleFit support into Android Apps

Views

Total views

256

On Slideshare

0

From embeds

0

Number of embeds

0

Actions

Downloads

2

Shares

0

Comments

0

Likes

0

×