Categories
Android

List of the sensors in a device

In December 2012 I wrote a post about an app that displays the sensor list in an android device, now after about a year and a half the API 19 have new sensor types and methods and I thought to update the app.

The main changes have been made to the class MySensor:

package eu.lucazanini.sensorlist;

import android.annotation.TargetApi;
import android.content.Context;
import android.hardware.Sensor;
import android.os.Build;

public class MySensor {

    private final static String MICRO = "μ";
    private static final int SDK = Build.VERSION.SDK_INT;
    private final static String SQUARE = "²";
    private Context context;
    private float maximumRange, minDelay, power, resolution;
    private String name, vendor;
    private int type, version, fifoMaxEventCount, fifoReservedEventCount;

    public MySensor(Sensor sensor, Context context) {
	this.name = sensor.getName();
	this.vendor = sensor.getVendor();
	this.type = sensor.getType();
	this.version = sensor.getVersion();
	this.maximumRange = sensor.getMaximumRange();
	this.power = sensor.getPower();
	this.resolution = sensor.getResolution();
	if (SDK >= Build.VERSION_CODES.GINGERBREAD)
	    MySensorAPI9(sensor);
	if (SDK >= Build.VERSION_CODES.KITKAT)
	    MySensorAPI19(sensor);
	this.context = context;
    }

    public String getDelayUnits() {
	return MICRO + "s";
    }

    public int getFifoMaxEventCount() {
	return fifoMaxEventCount;
    }

    public int getFifoReservedEventCount() {
	return fifoReservedEventCount;
    }

    public float getMaximumRange() {
	return maximumRange;
    }

    public float getMinDelay() {
	return minDelay;
    }

    public String getName() {
	return name;
    }

    public float getPower() {
	return power;
    }

    public String getPowerUnits() {
	return "mA";
    }

    public float getResolution() {
	return resolution;
    }

    public int getType() {
	return type;
    }

    public String getTypeDescription() {
	String description = null;

	switch (type) {
	case Sensor.TYPE_ACCELEROMETER:
	    description = context.getResources().getString(
		    R.string.accelerometer);
	    break;
	case Sensor.TYPE_AMBIENT_TEMPERATURE:
	    description = context.getResources().getString(
		    R.string.ambient_temperature);
	    break;
	case Sensor.TYPE_GAME_ROTATION_VECTOR:
	    description = context.getResources().getString(
		    R.string.game_rotation);
	    break;
	case Sensor.TYPE_GEOMAGNETIC_ROTATION_VECTOR:
	    description = context.getResources().getString(
		    R.string.geomagnetic_rotation);
	    break;
	case Sensor.TYPE_GRAVITY:
	    description = context.getResources().getString(R.string.gravity);
	    break;
	case Sensor.TYPE_GYROSCOPE:
	    description = context.getResources().getString(R.string.gyroscope);
	    break;
	case Sensor.TYPE_GYROSCOPE_UNCALIBRATED:
	    description = context.getResources().getString(
		    R.string.uncalibrated_gyroscope);
	    break;
	case Sensor.TYPE_LIGHT:
	    description = context.getResources().getString(R.string.light);
	    break;
	case Sensor.TYPE_LINEAR_ACCELERATION:
	    description = context.getResources().getString(
		    R.string.linear_acceleration);
	    break;
	case Sensor.TYPE_MAGNETIC_FIELD:
	    description = context.getResources().getString(
		    R.string.magnetic_field);
	    break;
	case Sensor.TYPE_MAGNETIC_FIELD_UNCALIBRATED:
	    description = context.getResources().getString(
		    R.string.uncalibrated_magnetic_field);
	    break;
	case Sensor.TYPE_ORIENTATION:
	    description = context.getResources()
		    .getString(R.string.orientation);
	    break;
	case Sensor.TYPE_PRESSURE:
	    description = context.getResources().getString(R.string.pressure);
	    break;
	case Sensor.TYPE_PROXIMITY:
	    description = context.getResources().getString(R.string.proximity);
	    break;
	case Sensor.TYPE_RELATIVE_HUMIDITY:
	    description = context.getResources().getString(
		    R.string.relative_humidity);
	    break;
	case Sensor.TYPE_ROTATION_VECTOR:
	    description = context.getResources().getString(
		    R.string.rotation_vector);
	    break;
	case Sensor.TYPE_SIGNIFICANT_MOTION:
	    description = context.getResources().getString(
		    R.string.significant_motion);
	    break;
	case Sensor.TYPE_STEP_COUNTER:
	    description = context.getResources().getString(
		    R.string.step_counter);
	    break;
	case Sensor.TYPE_STEP_DETECTOR:
	    description = context.getResources().getString(
		    R.string.step_detector);
	    break;
	case Sensor.TYPE_TEMPERATURE:
	    description = context.getResources()
		    .getString(R.string.temperature);
	    break;
	default:
	    description = context.getResources().getString(R.string.unknown);
	    break;
	}

	return description;
    }

    public String getUnits() {
	String units = null;

	switch (type) {
	case Sensor.TYPE_ACCELEROMETER:
	    units = "m/s" + SQUARE;
	    break;
	case Sensor.TYPE_AMBIENT_TEMPERATURE:
	    units = "°C";
	    break;
	case Sensor.TYPE_GAME_ROTATION_VECTOR:
	    units = "";
	    break;
	case Sensor.TYPE_GEOMAGNETIC_ROTATION_VECTOR:
	    units = "";
	    break;
	case Sensor.TYPE_GRAVITY:
	    units = "m/s" + SQUARE;
	    break;
	case Sensor.TYPE_GYROSCOPE:
	    units = "rad/s";
	    break;
	case Sensor.TYPE_LIGHT:
	    units = "SI lux";
	    break;
	case Sensor.TYPE_LINEAR_ACCELERATION:
	    units = "m/s" + SQUARE;
	    break;
	case Sensor.TYPE_MAGNETIC_FIELD:
	    units = MICRO + "T";
	    break;
	case Sensor.TYPE_MAGNETIC_FIELD_UNCALIBRATED:
	    units = MICRO + "T";
	    break;
	case Sensor.TYPE_ORIENTATION:
	    units = "°";
	    break;
	case Sensor.TYPE_PRESSURE:
	    units = "hPa";
	    break;
	case Sensor.TYPE_PROXIMITY:
	    units = "cm";
	    break;
	case Sensor.TYPE_RELATIVE_HUMIDITY:
	    units = "";
	    break;
	case Sensor.TYPE_ROTATION_VECTOR:
	    units = "";
	    break;
	case Sensor.TYPE_SIGNIFICANT_MOTION:
	    units = "";
	    break;
	case Sensor.TYPE_STEP_COUNTER:
	    units = "";
	    break;
	case Sensor.TYPE_STEP_DETECTOR:
	    units = "";
	    break;
	case Sensor.TYPE_TEMPERATURE:
	    units = "°C";
	    break;
	default:
	    units = "unknown";
	    break;
	}

	return units;
    }

    public String getVendor() {
	return vendor;
    }

    public int getVersion() {
	return version;
    }

    public void setFifoMaxEventCount(int fifoMaxEventCount) {
	this.fifoMaxEventCount = fifoMaxEventCount;
    }

    public void setFifoReservedEventCount(int fifoReservedEventCount) {
	this.fifoReservedEventCount = fifoReservedEventCount;
    }

    public void setMaximumRange(float maximumRange) {
	this.maximumRange = maximumRange;
    }

    public void setMinDelay(float minDelay) {
	this.minDelay = minDelay;
    }

    public void setName(String name) {
	this.name = name;
    }

    public void setPower(float power) {
	this.power = power;
    }

    public void setResolution(float resolution) {
	this.resolution = resolution;
    }

    public void setType(int type) {
	this.type = type;
    }

    public void setVendor(String vendor) {
	this.vendor = vendor;
    }

    public void setVersion(int version) {
	this.version = version;
    }

    @Override
    public String toString() {
	return name;
    }

    @TargetApi(Build.VERSION_CODES.KITKAT)
    private void MySensorAPI19(Sensor sensor) {
	this.fifoMaxEventCount = sensor.getFifoMaxEventCount();
	this.fifoReservedEventCount = sensor.getFifoReservedEventCount();
    }

    @TargetApi(Build.VERSION_CODES.GINGERBREAD)
    private void MySensorAPI9(Sensor sensor) {
	this.minDelay = sensor.getMinDelay();
    }

}

The layout has been modified to show the values ​​of the fields fifoMaxEventCount and fifoReservedEventCount, you can download the files of the app here.

This is the list of new types of sensors:

  • TYPE_GAME_ROTATION_VECTOR
  • TYPE_GEOMAGNETIC_ROTATION_VECTOR
  • TYPE_GYROSCOPE_UNCALIBRATED
  • TYPE_SIGNIFICANT_MOTION
  • TYPE_STEP_COUNTER
  • TYPE_STEP_DETECTOR

I find very interesting TYPE_GAME_ROTATION_VECTOR that it is like TYPE_ROTATION_VECTOR because it doesn’t use the magnetometer but only the accelerometer to return the rotation vector.
In this way you can’t get the orientation of the device in Earth’s coordinate system but it is more accurate, in other words you know how much the device is rotated but you can’t know the orientation with respect to magnetic north.

There are these new methods in class Sensor with API 19:

  • public int getFifoMaxEventCount()
  • public int getFifoReservedEventCount()

and in the Nexus 5 they are always different from zero except for TYPE_SIGNIFICANT_MOTION, then the batch mode is almost always supported.

sensorlist_nexus5

Leave a Reply

Your email address will not be published.

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