Book Image

Android Sensor Programming By Example

By : Varun Nagpal
Book Image

Android Sensor Programming By Example

By: Varun Nagpal

Overview of this book

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.
Table of Contents (13 chapters)
Android Sensor Programming By Example
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface

Understanding the sensor framework callbacks


The two most important callbacks of the sensor framework are the onSensorChanged() and onAccuracyChanged() methods. In order to write efficient sensor code, it's important to understand when these methods are called, and what processing we can do in them. These callbacks are methods of the SensorEventListnener interface, which needs to be implemented in the class where the callbacks are to be received:

onSensorChanged() is the first callback and has the following syntax:

@Override 
  public void onSensorChanged(SensorEvent event) { 
   } 

Depending on the type of reporting mode of the sensor, this method will be called, either at regular frequency (Continuous mode) or whenever there is a change in the value of the sensors from the previously reported value (On the change mode). The onSensorChanged() method provides the sensor values inside the float value[] array of the SensorEvent object. These sensor values are different from the...