Lab 1.2: Android Sensors

Overview

In this lab, we will explore more of our android OS capabilities such as sensing the environment around. From GPS and base station triangulation location to accelerometer, gyroscope, temperature, barometer, and more.  Most smart phones come with come with an Accelerometer,  a gyro,  a proximity sensor, and a compass. In future labs (Arduino labs), we will learn how to overcome this limitations by using any sensor with android devices. In android OS, some sensors are purely hardware based (e.g., gyroscope) and some others depend on both software and hardware (e.g., orientation).

Location

The most classical way to locate yourself is by using GPS. Although GPS is most accurate, it only works outdoors, it quickly consumes battery power, and doesn’t return the location as quickly as users want. Android’s Network Location Provider determines user location using cell tower and Wi-Fi signals, providing location information in a way that works indoors and outdoors, responds faster, and uses less battery power. To obtain the user location in your application, you can use both GPS and the Network Location Provider, or just one.

The main classes we will use in this section  are:

Now, lets start building our location application.

  1. Create a new Android Studio project (name it sensors, for instance). Select a simple activity.
  2. Create a simple UI: In activity_main.xml create two TextViews each with a unique ID (ex. tvGpsLocation and tvNetworkLocation, respectively)
  3. Now, in MainActivity class, reference two TextView variables to the corresponding views in XML layout file:
  4. The next step is to create an instance of LocationManager inside onCreate method:
  5. In order to receive location values, you need to have a listener object that implements LocationListener interface. Once we create a class with these methods, we can create a listener object and send it to LocationManager as an argument. The class should be defined  inside MainActivity class in order to be able to reference to MainActivity objects and GUI resources.

  6. Inside onCreate method, instantiate a listener. We will use the listener to get coarse location based on wifi and network provider, as well as fine GPS location:

  7. We add both locations to LocationManager object:

    The first parameter in requestLocationUpdates() is the type of location provider to use (in this case, the Network Location Provider for cell tower and Wi-Fi based location). You can control the frequency at which your listener receives updates with the second and third parameter—the second is the minimum time interval between notifications and the third is the minimum change in distance between notifications—setting both to zero requests location notifications as frequently as possible. The last parameter is your LocationListener, which receives callbacks for location updates. LocationManager calls onLocationChanged method whenever a location is changed. 
  8. Implement onLocationChanged method to modify TextViews. Namely, tvGpsLocation and tvNetworkLocation.
  9. Before you launch the application, make sure you add the new permissions in AndroidManifist.xml, otherwise you will get a run time error in your device.
  10. Right click on src directory, click Run as  Android Application on your device. Make sure the GPS is enabled in your device. Note that GPS only works outdoors and takes some time to provide actual values (You may test outdoors after the lab).
  11. If you get permission error when you launch the app, then edit Gradle Scripts > build.gradle (Module:app) and change targetSdkVersion to 22 as shown below. The latest sdk version may require additional configuration for using the runtime permission system.

  12. We showed here a simple way to read GPS location, but if you want to have a production version of your app, you should consider several factors such as battery consumption due to GPS usage. For more reading, check android site.

Motion Sensors

The Android platform provides several sensors that let you monitor the motion of a device. Two of these sensors are always hardware-based (the accelerometer and gyroscope), and three of these sensors can be either hardware-based or software-based (the gravity, linear acceleration, and rotation vector sensors). For example, on some devices the software-based sensors derive their data from the accelerometer and magnetometer, but on other devices they may also use the gyroscope to derive their data. Most Android-powered devices have an accelerometer, and many now include a gyroscope. The availability of the softare-based sensors is more variable because they often rely on one or more hardware sensors to derive their data.

Figure 1: Coordinate system (relative to a device) that’s used by the Sensor API.

In general, the sensor framework uses a standard 3-axis coordinate system to express data values. For most sensors, the coordinate system is defined relative to the device’s screen when the device is held in its default orientation (see figure 1).

Gravity Sensor

The code is very similar to the previous section.

  • Add to the previous UI another TextView and name it tvGravity. Reference to the view in MainActivity class.
  • We will use two classes SensorManager, Sensor and we will implement SensorEventListener to handle sensor value changes.

  • Create a class within MainActivity class that implements SensorEventListener

Gyroscope

Gyroscope is a hardware sensor and provides the rate of angular change. Usually we need further manipulation to get the rotation angles ( you can do this example later). Just lets focus on the basic work to get it work. 

This code is almost the same as the previous section.

  • Add another TextView and name it tvGyroscope and reference it in MainActivity class.
  • Intialize the sensor and implement SensorEventListener.

  • Now implement the corresponding EventListener:

Proximity sensor

This sensors allows you to find the distance from the device screen to any object in front. Usually, this is used in the phone to know whether the device is near the ear or not in order to shut off the screen when talking. However, we can explore it further now.

  • Modify the GUI adding another TextField (tvProximity) and create another sensor with TYPE_PROXIMITY. Modify the method name onSensorChanged to read the sensor value (event.values[0]) and view the result in tvProximity.

Some proximity sensors provide only binary values representing near and far only (like my device), so if you get either 5 cm or 0, then your sensor is of this type

Light sensor

Simply change sensor type to TYPE_LIGHT  and modify the UI accordingly to view the new value.

You can full list of sensors types in android reference.

Lab Exercise 1.2

Create an application that shows your  location and the values of above sensors. Also, try out a new sensor.

Bonus: Develop a more reasonable UI using tabs and fragments.

Note: It is highly recommended to check android site for tutorials and references.

  • The exercise should be done individually.
  • Create a folder ‘lab-1.2’ and copy the final exercise files to it.
  • Add, commit, and push your changes to the remote server.
  • The deadline is before the next lab.

 

Reference

http://developer.android.com/guide/topics/sensors/index.html

 

iblogpro theme