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 5. The Motion, Position, and Fingerprint Sensors

This chapter will introduce you to the motion, position, and fingerprint sensors. We will learn in detail about all the motion sensors (accelerometer, gyroscope, linear acceleration, gravity, and significant motion) and position sensors (magnetometer and orientation). As a learning exercise for the chapter, we will develop three small applications. The first application will detect a shake using the accelerometer sensor, the second one will tell the earth's magnetic field direction using the orientation sensor, and the third one will use the fingerprint sensor to authenticate the user.

The topics covered in this chapter are as follows:

  • Understanding the motion-based sensors (accelerometer, gyroscope, linear acceleration, gravity, and significant motion sensors)
  • Understanding the position-based sensors (magnetometer and orientation sensors)
  • Understanding the newly introduced fingerprint sensor and its supporting APIs
  • How to use the accelerometer...

Understanding motion-based sensors


Motion sensors are responsible for measuring acceleration forces and rotational forces acting along three axes of the phone. Motion sensors include the accelerometer, the gyroscope, gravity, linear acceleration, signification motion, the step detector, and the step counter. The detailed list of motion sensors can be found in Chapter 1, Sensor Fundamentals. In the next section, we will be discussing these sensors individually in detail. We will cover the step detector and step counter in the next chapter.

The accelerometer sensor

The accelerometer sensor determines the acceleration along the xy, and z axes, which is applied to a phone by measuring the forces acting on the phone. The measured acceleration includes both the physical acceleration (change of velocity) and the static gravity acting on the phone all the time. Accelerometer sensors are made up of the Micro Electro Mechanical System (MEMS), which is an embedded system that integrates electronic...

Understanding position-based sensors


The position sensor lets your app determine the position of a device. There are two major sensors in this category: the geomagnetic field sensor and the orientation sensor. The proximity sensor, which measures the proximity of any object to the phone, also comes under this category. The proximity sensor has already been discussed in the previous chapter in detail.

The magnetometer sensor

The magnetometer sensor measures the changes in the earth's magnetic field. It provides the raw magnetic field strength in units of micro tesla (μT). The magnetometer sensor is based on a miniature Hall effect sensor, which detects the earth's magnetic field along the three perpendicular xy, and z axes. The Hall effect sensor measures the magnetic field by generating a voltage that is proportional to the earth's magnetic field strength and polarity. This voltage is converted to micro tesla (μT) values using electric circuits. The magnetometer sensor provides two types...

The fingerprint sensor


Samsung was the first to introduce the fingerprint sensor in their Android devices, and they also provided support for it in their Pass SDK. The official support for fingerprint sensors was provided in the Android platform from Android 6.0 (API Level 23). The fingerprint sensor is a hardware sensor; generally it is found either at the back of the Android phone, or at the bottom of the screen. Typically, two types of fingerprint sensor are found today: the first one is a capacitive sensor, and the second one is an optical sensor. An optical sensor works by shining a bright light over your fingerprint and taking a digital photograph. This digital image is compared with the original fingerprint digital image to get the authentication results. The capacitive sensor is found in most iPhones and Android phones today. It works by passing a mild electric current through the outer skin of your finger. When your finger is placed on the surface of the sensor, the ridges in your...

Time for action – shake detection using the accelerometer sensor


One of the most common use cases of the accelerometer sensor is to detect the shaking of the phone. Shaking can act as a valuable input for the apps, especially when the phone screen is off. For example, a lot of music player apps allow you to change the songs just by shaking the phone. In our example, we will play a small audio MP3 file when the phone shake is detected using the accelerometer sensor:

  1. As the first step, we create the necessary infrastructure to get the values from the accelerometer sensor. We will create a ShakeDetectionActivity and follow the standard steps for getting values from a sensor. We will select the sensor type for TYPE_ACCELEROMETER in the getDefaultSensor() method of SensorManager and initiate the MediaPlayer object with the audio MP3 file kept in the raw folder inside the onCreate() method of the activity. As a standard practice, we will register the listener in onResume()and un-register it in...

Time for action – the compass using orientation sensor and orientation APIs


In our example, we will use the orientation sensor to develop the compass, and since it is a deprecated sensor, we will also develop it using alternative APIs. From the orientation sensor, we will directly use its azimuth values to feed into the compass, while for alternative APIs, we will use the raw accelerometer and magnetometer sensor values to compute the azimuth values. Let's look at both the implementations in detail:

  1. First, we create the infrastructure to get the values from the orientation, accelerometer, and magnetometer sensors. Inside the onCreate() method of CompassActivity, we initialize all the three sensors using SensorManager. We also initiate a layout file for the activity, which consists of a compass image that has North, South, East, and West marked on it. The compass image will be rotated to align its north to point toward the Earth's magnetic north. We also created four float arrays to store...

Time for action – using the fingerprint sensor


In order to support the fingerprint sensor, the Android platform has introduced a new system service, which is called the Finger Print Service, and it can be accessed using the instance of FingerprintManager. It provides all the necessary APIs to use the fingerprint sensor. In the following example, we will use the fingerprint sensor to authenticate the user. In order to make this example work, the Android device should have a fingerprint sensor, and it also should have set up or enrolled the user's fingerprint using the security settings. We also need to use two components of security (Keystore and Cipher) to use the fingerprint authentication API. Fingerprint sensor APIs require install time permission in the AndroidManifest.xml file (android.permission.USE_FINGERPRINT) and also runtime permission before using them. Now, let's look at the implementation details:

  1. Inside the onCreate() method of FingerPrintActivity, we initiated the object of...

What just happened?


We just created three small applications using three different sensors. The first one detects physical shakes using the accelerometer sensor, the second one tells the earth's magnetic direction using the orientation sensor, and the third one uses the fingerprint sensor to authenticate the user. A few important points to note are that the orientation sensor has been deprecated, so in place of it we should use the getRotationMatrix()and getOrientation() APIs to get the orientation values. Fingerprint APIs were introduced in Android Marshmallow (API Level 23), which uses both runtime and install time permissions. Thus, to use the fingerprint sensor, we should include both runtime and install time permissions.

Summary


We learned about all the motion sensors (accelerometer, gyroscope, linear acceleration, gravity, and significant motion) and position sensors (magnetometer and orientation) in detail. We also looked at the newly introduced fingerprint sensor and its supporting APIs.

In the next chapter, we will take our understanding of motion sensors (particularly the accelerometer) to the next level, and we will use the accelerometer sensor to develop an algorithm to detect walking, jogging, and running activities. We will also learn two new sensors, that is, the step detector and step counter.

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 €14.99/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