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 – turning the torch light on and off using the proximity sensor


In this section, we will be learning how to use the proximity sensor to turn the camera flash light on and off. As discussed earlier, most proximity sensors return the absolute distance in cm, but some return only the near and far values. The near value is 0 and the far value is the maximum range of the proximity sensor. There are a lot of common use cases for proximity sensors, such as to turn off the screen while the user is on a call, or to detect if the phone is inside the pocket or outside. For our example, we will be turning the camera flashlight on whenever any object comes near the phone screen and turning it off when the object goes far from the phone screen. The proximity sensor has on-change reporting mode, the details of reporting modes are explained in Chapter 1, Sensor Fundamentals. It is fired as soon as the proximity of the object near the phone changes.

The following code shows how to use the...