Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display an Android Sensors List

Tags:

I'm trying to display a list of available sensors but it's like there are not!
I was thinking that it was because of the emulator, but i tried it on the phone and the result is the same.

private SensorManager mSensorManager; TextView mSensorsTot,mSensorAvailables;  public void onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);     setContentView(R.layout.main);     // Get the texts fields of the layout and setup to invisible     mSensorsTot   = (TextView) findViewById(R.id.sensoritot);     mSensorAvailables  = (TextView) findViewById(R.id.sensoridisponibili);      // Get the SensorManager      mSensorManager= (SensorManager) getSystemService(SENSOR_SERVICE);      // List of Sensors Available     List<Sensor> msensorList = mSensorManager.getSensorList(SensorManager.SENSOR_ALL);      // Print how may Sensors are there     mSensorsTot.setText(msensorList.size()+" "+this.getString(R.string.sensors)+"!");      // Print each Sensor available using sSensList as the String to be printed     String sSensList = new String("");     Sensor tmp;     int x,i;     for (i=0;i<msensorList.size();i++){      tmp = msensorList.get(i);      sSensList = " "+sSensList+tmp.getName(); // Add the sensor name to the string of sensors available     }     // if there are sensors available show the list     if (i>0){      sSensList = getString(R.string.sensors)+":"+sSensList;      mSensorAvailables.setText(sSensList);     } } 
like image 257
Skatephone Avatar asked May 18 '10 14:05

Skatephone


People also ask

How do I find my sensor list on Android?

Go to the activity_main. xml file which represents the UI of the application, and create a TextView inside a ScrollView that shall list the sensors present in the device. Below is the code for the activity_main. xml file.

How many sensors are there in Android phone?

The Android smartphone provides two sensors that let you determine position of device- geomagnetic field sensor with combination of accelerometer sensor. This sensor works in controlling brightness level of screen.

How do I know if my Android phone has a sensor?

Open the app and tap the Diagnostic button on the home screen. Tap the individual icons to run diagnostic tests on the battery, SIM card, sensors, touch screen, flashlight, camera, microphone, speaker, Bluetooth, Wi-Fi, and more. Alternatively, tap the Test all button to perform all the tests one after the other.


1 Answers

The constant SensorManager.SENSOR_ALL is deprecated and doesn't seem to work anymore.

Query the sensor list with Sensor.TYPE_ALL instead and it should work (my emulator returns a "Goldfish 3-axis Accelerometer").

like image 107
Henning Avatar answered Oct 02 '22 08:10

Henning