SHARE
TWEET

Listing all sensors (Android)

ulfben Mar 7th, 2017 64 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. private SensorManager mSensorManager;
  2.  
  3. public void onCreate(Bundle savedInstanceState) {
  4.     super.onCreate(savedInstanceState);
  5.     setContentView(R.layout.main);
  6.    
  7.     // Get the texts fields of the layout and set to invisible
  8.     TextView sensorCountText =
  9.         (TextView) findViewById(R.id.sensorCountText);
  10.     TextView sensorNamesText =
  11.         (TextView) findViewById(R.id.sensorNamesText);
  12.    
  13.     // Get the SensorManager
  14.     mSensorManager =
  15.         (SensorManager) getSystemService(Context.SENSOR_SERVICE);
  16.    
  17.     // get a list of available sensors
  18.     List<Sensor> sensors =
  19.         mSensorManager.getSensorList(Sensor.TYPE_ALL);
  20.        
  21.     // Print how may Sensors there are
  22.     sensorCountText.setText(sensors.size() +" sensors available.");
  23.    
  24.     // Print the name of each Sensor
  25.     StringBuilder out = new StringBuilder();
  26.     for(Sensor s : sensors){       
  27.         out.append(s.getName()).append("\n");
  28.     }
  29.     sensorNamesText.setText(out.toString());
  30. }
RAW Paste Data
Want to get better at Java?
Learn to code Java in 2017
Top