O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Android Sensor Programming By Example

Book Description

Take your Android applications to the next level of interactivity by exploring the wide variety of Android sensors

About This Book

  • Get a thorough understanding of the fundamentals and framework of Android sensors.
  • Acquire knowledge of advance sensor programming, and learn how to connect and use sensors in external devices such as the Android Watch, Polar heart rate monitors, Adidas speed cells, and so on.
  • Learn from real-world sensor-based applications such as the Pedometer app to detect daily steps, the Driving app to detect driving events, and the Professional Fitness tracker app to track heart rate, weight, daily steps, calories burned, and so on.

Who This Book Is For

This book is targeted at Android developers who want to get a good understanding of sensors and write sensor-based applications, or who want to enhance their existing applications with additional sensor functionality. A basic knowledge of Android development is required

What You Will Learn

  • Learn about sensor fundamentals, different types of sensors, and the sensor co-ordinate system
  • Understand the various classes, callbacks, and APIs of the Android Sensor framework
  • Check all the available sensors on an Android device and know their individual capabilities—for example, their range of values, power consumption, and so on.
  • Implement sensor fusion using two or more sensors together and learn to compensate for the weakness of one sensor by using the strength of another
  • Build a variety of sensor based, real-world applications such as Weather, Pedometer, Compass, Driving Events Detection, Fitness Tracker, and so on.
  • Get to know about wake up and non-wake up sensors, wake locks, and how to use sensor batch processing along with the sensor hardware FIFO queue
  • Develop efficient battery and processor algorithms using raw sensor data to solve real-world problems
  • Connect to a variety of remote sensors such as body weight measurement and body fat percentage measurement using the Google Fit platform from your Android app

In Detail

Android phones available in today’s market have a wide variety of powerful and highly precise sensors. Interesting applications can be built with them such as a local weather app using weather sensors, analyzing risky driving behavior using motion sensors, a fitness tracker using step-counter sensors, and so on. Sensors in external devices such as Android Watch, Body Analyzer & Weight Machine, Running Speed Cell, and so on can also be connected and used from your Android app running on your phone.

Moving further, this book will provide the skills required to use sensors in your Android applications. It will walk you through all the fundamentals of sensors and will provide a thorough understanding of the Android Sensor Framework. You will also get to learn how to write code for the supportive infrastructure such as background services, scheduled and long running background threads, and databases for saving sensor data. Additionally, you will learn how to connect and use sensors in external devices from your Android app using the Google Fit platform.

By the end of the book, you will be well versed in the use of Android sensors and programming to build interactive applications.

Style and approach

A step-by-step and easy-to-follow guide that focuses on utilizing sensors to perform certain tasks. After covering the fundamentals in the first chapter, the book develops the concepts by building a real-world, sensor-based application in subsequent chapters.

Downloading the example code for this book. You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the code file.

Table of Contents

  1. Android Sensor Programming By Example
    1. Android Sensor Programming By Example
    2. Credits
    3. About the Author
    4. About the Reviewers
    5. www.PacktPub.com
      1. Why subscribe?
      2. Free access for Packt account holders
    6. Preface
      1. What this book covers
      2. What you need for this book
      3. Who this book is for
      4. Conventions
      5. Reader feedback
      6. Customer support
        1. Downloading the example code
        2. Errata
        3. Piracy
        4. Questions
    7. 1. Sensor Fundamentals
      1. What are sensors?
        1. Types of sensors
        2. Types of sensor values
      2. Motion, position, and environmental sensors
        1. Motion sensors
        2. Position sensors
        3. Environmental sensors
      3. Sensors' coordinate system
        1. Android Sensor Stack
      4. Components of the sensor framework
        1. SensorManager
        2. SensorEventListener
        3. Sensor
        4. SensorEvent
      5. Sensor's sampling period, power, and battery consumption
      6. The reporting modes of sensors
      7. Dealing with specific sensor configuration
        1. Checking the availability of the sensor at runtime
        2. Declaring the sensor as mandatory feature
      8. Sensor availability based on the Android API level
      9. Best practice for accessing sensors
      10. Summary
    8. 2. Playing with Sensors
      1. Understanding the sensor framework callbacks
        1. Seeing the big picture
      2. Time for action - using sensors in the foreground activity
        1. What just happened?
      3. Time for action – listing the available sensors on a device
        1. What just happened?
      4. Time for action – knowing individual sensors' capabilities
        1. What just happened?
      5. Time for action – getting the sensor values and updating the user interface
        1. What just happened?
      6. Time for action – processing the sensor values in the background service
        1. The phone handling algorithm
      7. What just happened?
      8. Summary
    9. 3. The Environmental Sensors – The Weather Utility App
      1. The weather utility app's requirements
      2. Understanding environmental sensors
      3. Time for action – using the temperature sensor
        1. What just happened?
      4. Getting air pressure from the phone's pressure sensor
        1. Time for action – calculating the altitude using the pressure sensor
          1. What just happened?
      5. Getting relative humidity from the phone's humidity sensor
        1. Time for action - calculating the dew point and absolute humidity
          1. What just happened?
        2. Time for action – comparing the temperature, humidity, and pressure values from web services to phone sensors
        3. Third-party web service – open weather map
        4. Using Google Play Services Location API and AsyncTask
          1. What just happened?
      6. Summary
    10. 4. The Light and Proximity Sensors
      1. Understanding the light and proximity sensors
      2. The automatic torch light and screen brightness app requirements
      3. Time for action – turning the torch light on and off using the proximity sensor
        1. What just happened?
      4. Time for action – adjusting the screen brightness using the light sensor
        1. What just happened?
      5. Wake locks, wakeup sensors, and the FIFO queue
        1. Wakeup and non-wakeup sensors
        2. The sensor's hardware FIFO queue
      6. Summary
    11. 5. The Motion, Position, and Fingerprint Sensors
      1. Understanding motion-based sensors
        1. The accelerometer sensor
        2. The gyroscope sensor
        3. The gravity sensor
        4. The linear acceleration sensor
        5. The significant motion sensor
      2. Understanding position-based sensors
        1. The magnetometer sensor
        2. The orientation sensor
      3. The fingerprint sensor
      4. Time for action – shake detection using the accelerometer sensor
      5. Time for action – the compass using orientation sensor and orientation APIs
      6. Time for action – using the fingerprint sensor
      7. What just happened?
      8. Summary
    12. 6. The Step Counter and Detector Sensors – The Pedometer App
      1. The pedometer app's requirements
      2. Understanding the step counter and step detector sensors
        1. The step counter sensor
        2. The step detector sensor
      3. Time for action – using the step counter sensor in activity
      4. Time for action – maintaining step history with the step detector sensor
        1. What just happened?
      5. Understanding the walking, jogging, and running signatures using the accelerometer sensor's data
        1. The walking signature using the accelerometer sensor
        2. The jogging or fast walking signature using the accelerometer sensor
        3. The running signature using the accelerometer sensor
        4. The type of step detection algorithm
        5. Making it battery- and CPU-efficient using sensor fusion
        6. Scope for improvement
      6. Time for action – type of step (walking, jogging, and running) detection using the accelerometer sensor
        1. What just happened?
      7. Summary
    13. 7. The Google Fit Platform and APIs – The Fitness Tracker App
      1. The Google Fit platform
        1. Google Fitness Store
        2. REST APIs
        3. Android Fitness APIs
          1. Sensors API
          2. Recording API
          3. History API
          4. Sessions API
          5. Bluetooth Low Energy API
          6. Config API
      2. Platform basics
        1. Data sources
        2. Data types
        3. Data point
        4. Data set
      3. Authorization and permission scopes
        1. Registration with the Google developer console
        2. Authorization from a user in the application
        3. Fitness scopes
        4. Installing and running time permissions
      4. Fitness tracker app using fitness APIs
      5. Fitness tracker application requirements and architecture
      6. Time for action – working with live fitness data using the Sensors API
        1. What just happened?
      7. Time for action – recording fitness data in background using Recording API
        1. What just happened?
      8. Time for action – getting history fitness data using the History API
        1. What just happened?
        2. Asynchronous versus synchronous results callback
      9. Summary