How can I get the name of the sensor?

I want to programmatically get the name Sensor . Say the name of the ambient light sensor.

How can I get his name?

+4
source share
3 answers

First you need to get an instance of SensorManager , and then get the required instance of the service 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 will get the name using the getName() method.

+3
source

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 
+8
source

Using getSensorList(Sensor.TYPE_ALL) you can get all the sensors inside the device.

 SensorManager sensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE); sensorManager.getSensorList(Sensor.TYPE_ALL); 
0
source

Source: https://habr.com/ru/post/1381108/


All Articles