Question

I want to programatically get a Sensor's name. Say the name of the ambient light sensor.

How can I get its name?

Was it helpful?

Solution 2

First you have to obtain an instance of the SensorManager, then get the desired service instance from the manager.

String sm = Context.SENSOR_SERVICE;
SensorManager sensorManager = (SensorManager) getSystemService(sm);

/*
 * We get the light sensor in this example.
 */
Sensor someSensor = sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);

/*
 * Always perform a null check on sensors since your device
 * may not have that sensor.
 */
if (null != someSensor) {
    String sensorName = someSensor.getName();
}

You get the name using the Sensor's getName() method.

OTHER TIPS

Use the sensor manager to query (all or certain types of) available sensors. Then use Sensor.getName() to get the name of an individual sensor.

SensorManager sm = (SensorManager) getSystemService(SENSOR_SERVICE);
List<Sensor> list = sm.getSensorList(Sensor.TYPE_ALL);

for(Sensor s : list) {
    Log.d("SENSORS", s.getName());
}

Example output from the snippet above:

11-14 12:26:47.549: D/SENSORS(911): BMA150 3-axis Accelerometer
11-14 12:26:47.559: D/SENSORS(911): AK8973 3-axis Magnetic field sensor
11-14 12:26:47.559: D/SENSORS(911): AK8973 Orientation sensor
11-14 12:26:47.559: D/SENSORS(911): CM3602 Proximity sensor
11-14 12:26:47.559: D/SENSORS(911): CM3602 Light sensor
11-14 12:26:47.559: D/SENSORS(911): Gravity Sensor
11-14 12:26:47.559: D/SENSORS(911): Linear Acceleration Sensor
11-14 12:26:47.559: D/SENSORS(911): Rotation Vector Sensor

With the getSensorList(Sensor.TYPE_ALL) you can get all the sensors within the device.

SensorManager sensorManager
    = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
sensorManager.getSensorList(Sensor.TYPE_ALL);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top