Hi, I’m Lisa Wray. And today, I want to talk to
you about the new Google Fit APIs for Android. Google Fit is an open
fitness ecosystem that provides developers
with a single set of APIs to discover, record, store, and
read activity data with user permission so users
have a single place to store all of their data. And users own that
data and can share with any apps of their choice
to make the data work harder for them. Fitness apps can
request that data be stored from any
wearable or sensor. And the user’s fitness
data is persisted when they upgrade
their fitness devices. There’s also a REST API
for use from the web or other platforms. But today, we’ll use the
fit APIs for Android. They’re part of Google Play
services, so starting now, the Android Fit APIs
are already supported on all devices back
to Gingerbread. That’s API level
nine and higher. So for example,
suppose I told you I was going to build a
pedometer app for my phone. It would help remind
users to move more by showing their
steps while they’re out and about in real time. It’ll also save
all of their data and let them view it afterwards
so they can analyze how they’re doing, not just in this app,
but maybe also on the web. Now what if I told
you I was going to do it without using a
database or a backend server. How? Let’s take a look. There are three main parts
of the API– the sensors API, the recording API,
and the history API. I want to get immediate
results, so we’ll start with the sensors API. The sensors API provides access
to raw, sensor data streams. They could be sensors
available on the Android device or from sensors available
on companion devices, such as wearables. For steps, Fit will
use the step counter on the phone or any
connected wearable, whichever it thinks
is most accurate. First thing, you need
a Google account. If you don’t have one,
that’s easy to fix. Next, you need to create a
project in the Google Developer Console and link
it with your app, and there are great step by
step instructions on the Fit website. In your development
environment, make sure you have the newest
Google Play Services client library installed. And if you’re using
Android Studio, you can just add the
Play Services library as a dependency in
your gradle file. Next, we’re going to connect
to Google Play Services using Google API client. Before you use any of
the features Fit offers, you have to connect
to Play Services. We’re going to select the
fitness API and add the read and write fitness scopes. These mean the user
will be prompted to grant our app permission
to access his or her data. Privacy is key here, so
without that permission, our app won’t work. Now, once we get a call back
that the Google API client is connected, we can start
calling the Fit APIs. Here’s where the
sensors API comes in. We use SensorRequest.Builder
to create a request to track the change in
step count every second. Then, we register
the request passing in a listener for the results,
along with the Google API client. And more about that
listener in a moment. Here’s the listener. Now that we’ve
registered, that listener will get a callback every
second with fresh data. And we can update our display. We have a simple fitness app. Of course, when the user
isn’t looking at their phone, we should unregister
the sensor, which we can do almost
exactly the same way. You can dive straight into
our samples and documentation at developers.googl
e.com/fit/android. Or stay with me in
our next DevByte to learn how to
persist and retrieve this data with the
recording and history APIs. Thanks for watching.