List of the sensors in an Android device

This is a simple app that displays all the sensors in the phone reporting the following characteristics:

  • Name: name of the sensor
  • Version: version of the sensor’s module
  • Vendor: vendor of this sensor
  • Type: type of this sensor
  • Max Range: maximum range of the sensor
  • Resolution: resolution of the sensor
  • Min Delay: minimum delay allowed between two events (equals to zero if this sensor only returns a value when the data it’s measuring changes)
  • Power: the power of the sensor

  1. create an Android project with package eu.lucazanini.sensorlist and without a starting activity
  2. replace the file AndroidManifest.xml
  3. replace the file res/values/strings.xml
  4. create the file res/values/types.xml
  5. replace the file res/values/styles.xml

    in this file .xml I set 4 custom styles for the views so labels are in italic and the fields more important are in bold and with a larger font
  6. create the resource res/layout/list_item.xml

    this layout is a single item of the ListView in which you see the details of only one sensor
    the views in this layout point to the styles defined in res/values/styles.xml
  7. create the class eu/lucazanini/sensorlist/MainActivity.java
  8. the main activity extends the class ListActivity and not Activity, because it is more easy to display a list of items
    in the onCreate event there are 3 fundamental steps:

    • I create an ArrayList<MySensor>, that it is an array containing the Sensor objects that are the sensors of the phone
    • I create a SensorAdapter object, in whose constructor I also pass the ArrayList<MySensor> of the previous step; it is important that SensorAdapter extends ArrayAdapter<MySensor>
    • I pass the SensorAdapter object as argument to setListAdapter to fill the ListView

    in the getSensors event I find all the sensors of the phone and I add them as objects of the class MySensor to a List that I set to ArrayList<MySensor> in the onCreate event

  9. create the class eu/lucazanini/sensorlist/SensorAdapter.java

    when you extend an ArrayAdapter, you override the method getView to set the layout (res/layout/list_item.xml)
    I display and set the text views related to “Min Delay” only if the API level is equal to or greater than Gingerbread
    some strings are displayed using the method Html.fromHtml(String source) because they contain special characters (μ or ²)
  10. create the class eu/lucazanini/sensorlist/MySensor.java

    the method getMinDelay of the class Sensor is new with GingerBread (API 9, Android 2.3), then you can use it only after the condition that the API level of the device is equal to or greater than 9
    the annotation @TargetApi avoids a compilation error
    the variables MICRO e SQUARE define some special characters in the method Html.fromHtml(String source) of the class SensorAdapter

The app running on emulator with API Level 7

The app running on emulator with API Level 17

The app running on Galaxy Nexus with API Level 17

The icon is different from the default one because I have replaced the icons ic_launcher.png in the folders res/drawable-*

3 Replies to “List of the sensors in an Android device”

  1. Code compiles but does not run. I get the title bar “Sensor List” and a blank white page, then after a couple of seconds, an error message “Unfortunately, Sensor List has stopped”, with an Ok button. I’m on Ice cream sandwich, Samsung Galaxy Ace 2x

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.