How to know if a sensor is present on my Android device?

Miloš picture Miloš · Jan 27, 2012 · Viewed 20.6k times · Source

i would like to know if a sensor (for exemple the accellerometer) is present on my Android device.

I am dealing with the SensorManager class. Here is the code I am using:

sensorMgr = (SensorManager) getSystemService(SENSOR_SERVICE);
sensorMgr.registerListener(this,sensorMgr.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),1);

Thank you.

Answer

Sam Felix picture Sam Felix · Jan 27, 2012

take a look in here:

http://developer.android.com/reference/android/content/pm/PackageManager.html

if think you need to do that:

PackageManager manager = getPackageManager();
boolean hasAccelerometer = manager.hasSystemFeature(PackageManager.FEATURE_SENSOR_ACCELEROMETER);