Reader small image

You're reading from  Android Sensor Programming By Example

Product typeBook
Published inApr 2016
Reading LevelBeginner
PublisherPackt
ISBN-139781785285509
Edition1st Edition
Languages
Tools
Right arrow
Author (1)
Varun Nagpal
Varun Nagpal
author image
Varun Nagpal

Varun Nagpal has been developing mobile apps since 2005 and has developed and contributed to more than 100 professional apps and games on various platforms, such as Android, iOS, Blackberry, and J2ME. Android app development has been his main area of expertise, and he has developed apps for a wide variety of Android devices, such as Android phones, tablets, watches, smart TVs, Android Auto, and Google Glass. He moved to Chicago in late 2013, and since then, he has become a seasoned mobile architect. He has worked in different roles (mobile architect, technical lead, senior developer, and technical consultant) for a variety of various global clients (Allstate, Verizon, AT&T, Sydbank Denmark, SiS Taiwan, Chams PLC Nigeria, and Nandos South Africa) in order to implement their mobile solutions. He has SCJP (Core Java) and SCWD (JSP and Servlets) certifications from Sun Microsystems and MCP (C#) and MCTS (ASP.NET) certifications from Microsoft. You can find his blogs on mobile technology and white papers written by him on his website at http://www.varunnagpal.com/. When he's not working, Varun can be found meditating or playing the flute. He also loves to develop meditation apps and fun games in his free time. He has developed about 40 meditation apps and games available on Google Play (https://play.google.com/store/apps/developer?id=Creative.Software.Studio) and the Apple App Store (https://itunes.apple.com/us/artist/creative-software-studio/id574745824) under the name of Creative Software Studio, his part-time start-up company (http://creativesoftwarestudio.com/).
Read more about Varun Nagpal

Right arrow

Chapter 4. The Light and Proximity Sensors

In this chapter, we will learn about proximity and light sensors, and we will develop a small application using them. We will also learn about the concepts of wake locks and wakeup and non-wakeup sensors. We will understand the hardware sensor FIFO queue and what happens to sensors when the application processor goes into suspended mode.

You will learn the following things in this chapter:

  • Understanding the light and proximity sensors.
  • Understanding requirements for the automatic torchlight and screen brightness app.
  • How to use the proximity sensor in the phone and turn phone's flashlight off and on.
  • How to use the phone's light sensor and adjust the screen brightness.
  • What are wake locks and how should we use them?
  • What are wakeup and non-wakeup sensors, and what is the hardware FIFO sensor queue?

Understanding the light and proximity sensors


The light sensor is a part of the environmental sensors, and the proximity sensor is a part of the positional sensors for Android. Both light and proximity sensors can be found in almost every Android device today. The light sensor is responsible for measuring the illuminance in lux. Illuminance is the amount of light striking a surface. It is also known as incident light, where the "incident" is the beam of light actually landing on the surface.

The proximity sensor measures the proximity of any object near the screen. There are two types of proximity sensor found on Android devices.

  • The first type of the proximity sensor provides the absolute distance in centimetres between the object and the phone. There are very few phones which support this type of proximity sensor.
  • The second type of sensor gives only two values in form of integers, which represents the proximity of the object from the phone. When the object is near, then the value is 0, while...

The automatic torch light and screen brightness app requirements


As a learning assignment for this chapter, we will be developing a small application that will make use of the light and proximity sensor in the phone to turn on and turn off the flash light and adjust the screen brightness. This app will be running in the foreground Android activity and will start processing the sensor values on onResume() and will stop on onPause(). We will have the separate activity for each proximity sensor and light sensor, and both will work independently. The following are the high-level requirements for the automatic torch light application:

  1. Create an Android activity to process the proximity sensor values.
  2. Whenever any object comes close to the phone (the proximity sensor gives the near value), turn on the flashlight, and whenever that object goes away from the phone (the proximity sensor gives the far value), then turn off the flashlight.
  3. Create an Android activity to process the light sensor values...

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

Time for action – adjusting the screen brightness using the light sensor


One of the most common use cases for the light sensor is to adjust the screen brightness according to the external lighting conditions. The maximum range of the light sensor might be different on different Android devices, but most of them support from 0 lux to several thousand lux. Lux is the standard unit for measuring the luminance of the light falling on a surface. For our example, we will use a range from 0 to 100 lux, as normal indoor lighting falls within this range. But for sunlight and strong lights the range can go up to 1,000 lux or more. In the sample app, we will increase the screen brightness, when the indoor lighting goes low, and similarly we will decrease the screen brightness when it goes high.

  1. We followed the standard steps to get values from the sensor. We select the sensor type to the TYPE_LIGHT in the getDefaultSensor() method of SensorManager. We also called the custom initScreenBrightness() method...

Wake locks, wakeup sensors, and the FIFO queue


All Android applications run on a dedicated Application Processor (AP), which is a part of the main CPU of the phone. This application processor is designed in such a way that it goes into the suspended mode when the user is not interacting with the phone. In this suspended mode, it reduces the power consumption by 10 times or more, but this freezes all the applications in the background. To work around this problem, the Android platform provides a solution using wake locks. If an application has to perform some important operation in the background and doesn't want the application processor to go into suspended mode, then it has to request a wake lock from the system's power service. Once the important operation is completed, it should release the wake lock. Wake lock can be obtained using the PowerManager object, which is provided by the system power service. The newWakeLock() method of PowerManager provides the object of wake lock. This newWakeLock...

Summary


In this chapter, we looked at the two new proximity and light sensors and developed a small app using them. We also learned how to turn on and turn off the flashlight using the proximity sensor and adjust the screen brightness using the light sensor. We understood how to wake up the application processor when it's in suspended mode using wake locks. We looked at the wakeup and non-wake up sensors and their FIFO queues.

In the next chapter, we will learn about motion sensors (accelerometer, gyroscope, linear acceleration, gravity, and significant motion) and position sensors (magnetometer and orientation). We will also explore the newly introduced fingerprint sensor.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Android Sensor Programming By Example
Published in: Apr 2016Publisher: PacktISBN-13: 9781785285509
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at ₹800/month. Cancel anytime

Author (1)

author image
Varun Nagpal

Varun Nagpal has been developing mobile apps since 2005 and has developed and contributed to more than 100 professional apps and games on various platforms, such as Android, iOS, Blackberry, and J2ME. Android app development has been his main area of expertise, and he has developed apps for a wide variety of Android devices, such as Android phones, tablets, watches, smart TVs, Android Auto, and Google Glass. He moved to Chicago in late 2013, and since then, he has become a seasoned mobile architect. He has worked in different roles (mobile architect, technical lead, senior developer, and technical consultant) for a variety of various global clients (Allstate, Verizon, AT&T, Sydbank Denmark, SiS Taiwan, Chams PLC Nigeria, and Nandos South Africa) in order to implement their mobile solutions. He has SCJP (Core Java) and SCWD (JSP and Servlets) certifications from Sun Microsystems and MCP (C#) and MCTS (ASP.NET) certifications from Microsoft. You can find his blogs on mobile technology and white papers written by him on his website at http://www.varunnagpal.com/. When he's not working, Varun can be found meditating or playing the flute. He also loves to develop meditation apps and fun games in his free time. He has developed about 40 meditation apps and games available on Google Play (https://play.google.com/store/apps/developer?id=Creative.Software.Studio) and the Apple App Store (https://itunes.apple.com/us/artist/creative-software-studio/id574745824) under the name of Creative Software Studio, his part-time start-up company (http://creativesoftwarestudio.com/).
Read more about Varun Nagpal