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


Published on

Simple introduction to integrating GoogleFit support into Android Apps

Published in: Engineering
  • My wife and I bought our son's first car from one of the auctions listed at Gov-Auctions. Thanks for providing such a great service. More info.. ▲▲▲
    Are you sure you want to  Yes  No
    Your message goes here
  • 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
  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: Follow the instructions at: 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 '' ... }
  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)
  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) #enableAutoManage(,
  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: 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. 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