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

Time for action – getting the sensor values and updating the user interface


Now, let's deal with the most important aspect of sensors, that is, playing with the sensor values. We have created a common activity and screen that can fit a vast number of values for all sensor types. Sensors can have varied values such as temperature or pressure; a light and proximity sensor may have only one value, while sensors such as magnetometer, accelerometer, gyroscope, linear acceleration, and gravity have the three values in the x, y, and z axes. There are other sensors that can have more than three values, for example, rotational vector, geomagnetic rotational vector, game rotational vector, and un-calibrated gyroscope. All the sensor values are passed in an array called values[], which is part of the SensorEvent object.

  1. We have created a generic common SensorValuesActivity to display all the values that are coming from the different sensors. We are using the length of values[]array to determine the...