Reader small image

You're reading from  Android Sensor Programming By Example

Product typeBook
Published inApr 2016
Reading LevelBeginner
PublisherPackt
ISBN-139781785285509
Edition1st Edition
Languages
Tools
Right arrow
Author (1)
Varun Nagpal
Varun Nagpal
author image
Varun Nagpal

Varun Nagpal has been developing mobile apps since 2005 and has developed and contributed to more than 100 professional apps and games on various platforms, such as Android, iOS, Blackberry, and J2ME. Android app development has been his main area of expertise, and he has developed apps for a wide variety of Android devices, such as Android phones, tablets, watches, smart TVs, Android Auto, and Google Glass. He moved to Chicago in late 2013, and since then, he has become a seasoned mobile architect. He has worked in different roles (mobile architect, technical lead, senior developer, and technical consultant) for a variety of various global clients (Allstate, Verizon, AT&T, Sydbank Denmark, SiS Taiwan, Chams PLC Nigeria, and Nandos South Africa) in order to implement their mobile solutions. He has SCJP (Core Java) and SCWD (JSP and Servlets) certifications from Sun Microsystems and MCP (C#) and MCTS (ASP.NET) certifications from Microsoft. You can find his blogs on mobile technology and white papers written by him on his website at http://www.varunnagpal.com/. When he's not working, Varun can be found meditating or playing the flute. He also loves to develop meditation apps and fun games in his free time. He has developed about 40 meditation apps and games available on Google Play (https://play.google.com/store/apps/developer?id=Creative.Software.Studio) and the Apple App Store (https://itunes.apple.com/us/artist/creative-software-studio/id574745824) under the name of Creative Software Studio, his part-time start-up company (http://creativesoftwarestudio.com/).
Read more about Varun Nagpal

Right arrow

Chapter 7. The Google Fit Platform and APIs – The Fitness Tracker App

This chapter will introduce you to new ways of working with sensors. We will learn about the new Google Fit platform and how it can be used to manage fitness sensor data efficiently. We will explore the different APIs provided by the Google Fit platform. In this chapter, we will learn new concepts, such as how we can request the automated collection and storage of sensor data in a battery-efficient manner, without your app being in the background all the time. We will also learn how to get data from a remotely connected device, such as Android Wear. In this chapter, we will mostly deal with fitness sensors and as a learning exercise, we will develop a fitness tracker application that will collect and process your fitness data.

The topics you will learn about in this chapter are:

  • What is the Google Fit platform?
  • How the Android fitness APIs, Rest APIs and Google Fitness Store fit together into the Google Fit platform
  • The details...

The Google Fit platform


Google Fit is a platform that allows developers to manage user fitness data effectively. Developers, on behalf of users, can upload, download, and persist their fitness data to a central repository in the cloud. This fitness data can range from simple height and weight numbers to individual step information. The fitness data can come from various data sources, such as fitness apps, Android sensors, Android wear sensors or any other device that can connect and upload data to the Google Fit platform. The data sources can be present either locally on the phone or can be in remote devices in the form of any app or hardware sensors. This fitness data management is done using three key components, as shown in the following diagram: the first is the Google Fitness Store, which resides in the cloud; the second are web-based REST APIs; and the third are Android Fitness APIs which are on the Android devices. Now let's discuss each one of them separately in detail.

Google Fitness...

Platform basics


The following section explains the platform basics and fitness data formats and terminologies used by the Google Fit platform.

Data sources

Data sources represent unique sources of sensor data. They can expose raw data coming from hardware sensors on local or companion devices, which is categorized as TYPE_RAW. Data sources also expose derived data, created by transforming or merging other data sources, which are categorized as TYPE_DERIVED. They hold the metadata regarding the source, such as which hardware device (device name) or application (package name) generated the data. Multiple data sources can exist for the same data type, for example for the heart rate data type, we can have a heart rate sensor in a watch and a heart rate sensor in a chest wrap that show as two different data sources for one data type.

Data types

A data type defines the representation and format of any fitness data. It consists of a name and an ordered list of fields, where each field represents a...

Authorization and permission scopes


User authorization is required before your application can read or write any fitness sensor data. User authorization is a two-step process. Step one is the registration of your application with the Google developer console, which is done outside your application. Step two is getting authorization from the user by using relevant scopes inside your application.

Registration with the Google developer console

Every application that needs to access fitness sensor data needs to register with the Google developer console. The followings steps explain the registration process:

  1. Open the Google developer console in any browser.
  2. Create a project from the console and enter your project name, which could be the same or different from your application name.
  3. Find the Fitness API from the APIs and Auth console menu and turn it on. Now Fitness API should appear at the top of your API list.
  4. Go to the Credential console menu and click on Create a new Client ID. This will open...

Fitness tracker app using fitness APIs


We have covered enough theory, now we will look at the implementation of these fitness APIs. As a learning exercise for this chapter, we will be developing a fitness tracker application that will make use of the fitness APIs we discussed so far in this chapter. This application will capture live fitness data and will also help users to track their fitness history. Let's explore the features and architecture of the application in detail.

Fitness tracker application requirements and architecture


The following list shows the high-level requirements of the fitness tracker application:

  1. When the application starts for the first time, it should get the following authorizations from the user:
    1. To read their live fitness data with all the read scopes, using the Sensor API.
    2. To record their fitness data with all the read scopes, using Recording API.
    3. To read their fitness history data with all the read scopes, using History API.
  2. The application should list all the available data sources for live data capture using the Sensors API.
  3. The application should capture live data from available data sources. It should also allow adding and removing listeners using the Sensors API.
  4. The application should list all the active subscriptions with their data types using the Recording API.
  5. The application should allow adding and removing of subscriptions for a particular data type using the Recording API.
  6. The application should show the history of available...

Time for action – working with live fitness data using the Sensors API


The Sensors API is provided to work with a live stream of fitness sensor data. It can provide data from sensors on local or connected devices. The Sensors API is a part of Google play services and can be connected using the GoogleApiClient class. In SensorActivity, we first add the required scopes and Sensors API and then connect to Google play services using an object of the GoogleApiClient class. The steps for connecting to Google play services via the GoogleApiClient class are explained in the first section of the driving event detection application of the bonus chapter, Sensor Fusion and Sensors-Based APIs – The Driving Events Detection App, during the discussion on the activity recognition API. The only difference in steps is that, instead of adding activity recognition API; we have to add Sensors API. Now let's look at the individual tasks performed by SensorActivity:

  1. The first task performed by SensorActivity is...

Time for action – recording fitness data in background using Recording API


The Recording API allows your app to request automated storage of sensor data in a battery-efficient manner by creating subscriptions. Once you add a subscription for a data type, then it's Google Play services' responsibility to start recording the data for the requested data type in the background. This recorded data is stored in the Google fitness store and can be queried by History API. The Recording API only decides which data type to record; everything else is managed by the Google fitness platform. The Recording API is part of Google play services. The steps for connecting to Google play services via the GoogleApiClient class are exactly the same as for the Sensors API, discussed in the previous section. In our example, inside SubscriptionActivity we will perform four important tasks with subscriptions. First, we will get authorization to read history data from the fitness store using the History API. Second...

Time for action – getting history fitness data using the History API


We have seen in the previous section how to request the automated collection of fitness sensor data, now in this section we will learn how to retrieve all the collected data from the Google fitness store. The History API not only allows your application to retrieve fitness sensor data, but it also allows your app to write and delete fitness data. Your application can only delete the fitness data created by your own app. The History API provides an efficient way of querying fitness data and also supports the batch importing of data. The History API is part of Google Play services and follows the same process to connect as Sensors API or Recording API. In our example, we will focus on how to query the fitness data between two dates and get fitness data that is aggregated by day. We will let the user select the data type from the spinner drop-down, and select the start and end dates and the time picker. Users can also request...

Summary


In this chapter, we learned new ways of working with fitness sensors using the Google Fit platform. The Google Fit platform simplifies the whole process of sensor data collection, storage, and retrieval by taking on most of the responsibility itself. The application has to do the minimal work of requesting and querying the fitness data. The Google Fit platform has been developed to deal only with fitness sensor data. It doesn't deal with other sensor data. The Google Fit platform also does a great job of maintaining and syncing all your fitness data on multiple devices, so if you use Android Wear and an Android phone, then both of them track your step counts differently, but at the end of the day you will find that the step count data is the same on both of them.

In the next bonus chapter, we will explore sensors-based APIs and their use in real-world applications. We will also discuss new examples of combining two or more sensors' data together, which is commonly referred to as sensor...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Android Sensor Programming By Example
Published in: Apr 2016Publisher: PacktISBN-13: 9781785285509
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at ₹800/month. Cancel anytime

Author (1)

author image
Varun Nagpal

Varun Nagpal has been developing mobile apps since 2005 and has developed and contributed to more than 100 professional apps and games on various platforms, such as Android, iOS, Blackberry, and J2ME. Android app development has been his main area of expertise, and he has developed apps for a wide variety of Android devices, such as Android phones, tablets, watches, smart TVs, Android Auto, and Google Glass. He moved to Chicago in late 2013, and since then, he has become a seasoned mobile architect. He has worked in different roles (mobile architect, technical lead, senior developer, and technical consultant) for a variety of various global clients (Allstate, Verizon, AT&T, Sydbank Denmark, SiS Taiwan, Chams PLC Nigeria, and Nandos South Africa) in order to implement their mobile solutions. He has SCJP (Core Java) and SCWD (JSP and Servlets) certifications from Sun Microsystems and MCP (C#) and MCTS (ASP.NET) certifications from Microsoft. You can find his blogs on mobile technology and white papers written by him on his website at http://www.varunnagpal.com/. When he's not working, Varun can be found meditating or playing the flute. He also loves to develop meditation apps and fun games in his free time. He has developed about 40 meditation apps and games available on Google Play (https://play.google.com/store/apps/developer?id=Creative.Software.Studio) and the Apple App Store (https://itunes.apple.com/us/artist/creative-software-studio/id574745824) under the name of Creative Software Studio, his part-time start-up company (http://creativesoftwarestudio.com/).
Read more about Varun Nagpal