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 6. The Step Counter and Detector Sensors – The Pedometer App

This chapter will focus on learning the use of the step detector and step counter sensors. We will analyze and process the accelerometer data to develop the algorithm for detecting the types of steps (walking, jogging, and fast running). As a learning exercise, we will develop a pedometer application and will closely look at the infrastructure (service, threads, and database) required to process the sensor data in the background for a longer duration of time. We will also discuss how to combine the step detector sensor with the accelerometer sensor to achieve battery optimization.

You will learn the following topics in this chapter:

  • Understanding the requirements for the pedometer app using the step detector and accelerometer sensors
  • Understanding the step detector and step counter sensors
  • How to use the step counter sensor in activity and show the number of steps taken since the phone's last reboot
  • How to use the step detector...

The pedometer app's requirements


We will be working on three incremental examples as learning assignments for this chapter. In the first example, we will be using the step counter sensor to show the number of steps taken since the phone was powered on in the activity. In our second example, we will go a little deeper and will discuss how to use the step detector sensor to store the steps information in the SQLite database from the service, and finally we will show the steps history along with the dates of using the activity. Our third example will be an evolved pedometer application that will use the sensor fusion between the step detector and accelerometer sensors to derive advanced functionality of the app. This evolved pedometer application will be highly battery optimized and will automatically track the physical activity (walking, jogging, and fast running) happening in the background. The following is a list of the high level requirements of this pedometer application:

  1. Create a pedometer...

Understanding the step counter and step detector sensors


The step counter and step detector sensors are very similar to each other and are used to count the steps. Both the sensors are based on a common hardware sensor that internally uses the accelerometer, but Android still treats them as logically separate sensors. Both of these sensors are highly battery optimized and consume very little power. Now, let's look at each individual sensor in detail.

The step counter sensor

The step counter sensor is used to get the total number of steps taken by the user since the last reboot (power on) of the phone. When the phone is restarted, the value of the step counter sensor is reset to zero. In the onSensorChanged() method, the number of steps is given by event.value[0]; although it's a float value, the fractional part is always zero. The event timestamp represents the time at which the last step was taken. This sensor is especially useful for those applications that don't want to run in the background...

Time for action – using the step counter sensor in activity


In this section, we will learn how to use the step counter sensor with a simple example. The good thing about the step counter is that, unlike other sensors, your app doesn't need to tell the sensor when to start counting the steps and when to stop counting them. It automatically starts counting as soon as the phone is powered on. To use it, we just have to register the listener with the sensor manager and then unregister it after using it. In the following example, we will be showing the total number of steps taken by the user since the last reboot (power on) of the phone in the android activity:

  1. We create a StepsCounterActivity, which implements the SensorEventListener interface so that it can receive the sensor events. We initiate the SensorManager and Sensor objects of the step counter, and we also check the sensor availability in the OnCreate() method of the activity. We register the listener in the onResume() method and unregistered...

Time for action – maintaining step history with the step detector sensor


The step counter sensor works well when we have to deal with the total number of steps taken by the user since the last reboot (power on) of the phone. It doesn't cater to our purpose when we have to maintain the history of each and every step taken by the user. The step counter sensor may combine some steps and process them together and will only update with an aggregated count instead of reporting the individual step detail. For such cases, the step detector sensor is the right choice. In our example, we will be using the step detector sensor to store the details of each step taken by the user, and we will show the total number of steps for each day since the application was installed. Our example will consist of three major components of android, namely, service, the SQLite database, and activity.

The Android service will be used to listen to all the individual step details, using the step counter sensor, when the...

Understanding the walking, jogging, and running signatures using the accelerometer sensor's data


The step counter and step detector sensors are really useful, but they fail to provide advanced information on steps, such as what the type of step was (running, jogging, or walking), and what the duration of each step was. For developing an advanced pedometer app or a health and fitness app, the duration and type of step is valuable information. As a learning exercise, we will develop our own algorithm to detect the type of step and the duration of each step. We will use the accelerometer sensor for our algorithm. The accelerometer sensor is the best sensor that can detect motion and acceleration acting on the phone during the walking process. The first step in developing the algorithm is to understand how a step looks on the accelerometer sensor. In the following section, we will be looking at the different signatures of walking, jogging, and running, as seen on the accelerometer sensor's data...

Time for action – type of step (walking, jogging, and running) detection using the accelerometer sensor


This section is dedicated to the implementation of the type of step detection algorithm discussed in the previous section. Our implementation for the algorithm will consist of four major components of android: first is android service, which will stay in the background, second is a set of two threads using the  ScheduledExecutorService, and third is the activity to show the pedometer application data. The last component is the SQLite database to store the steps' information. The following is the high-level class diagram of the application; we will discuss each class in detail in their own sections. Now, let's explore the first component in detail:

  1. The first component of the algorithm is the StepsTrackerService service, which will remain in the background and provide a container for execution. Inside this service, we create the StepDetectorListener and AccelerometerListener classes and...

Summary


We learned a lot of new concepts in this chapter, such as the step detector and step counter sensors. We learned how to develop the algorithm for detecting the types of steps (walking, jogging, fast running) using the accelerometer sensor data. We also looked at the infrastructure (service, threads, and database) required to process the sensor data in the background for a longer duration of time. This knowledge of the required infrastructure (service, threads, and database) will play an important role in developing efficient sensor-based applications.

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