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. ...

Get Android Sensor Programming By Example now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.