Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 251
Milos Cuculovic Avatar asked Jan 26 '12 23:01

Milos Cuculovic


People also ask

How can I check my phone sensors?

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.

What sensors are available on Android?

The Android platform provides two sensors that let you determine the position of a device: the geomagnetic field sensor and the accelerometer. The Android platform also provides a sensor that lets you determine how close the face of a device is to an object (known as the proximity sensor).

Do Android phones have sensors?

Stay organized with collections Save and categorize content based on your preferences. Most Android-powered devices have built-in sensors that measure motion, orientation, and various environmental conditions.


2 Answers

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);
like image 86
Sam Felix Avatar answered Oct 15 '22 21:10

Sam Felix


Here is recomendation from developer.android.com: http://developer.android.com/guide/topics/sensors/sensors_overview.html#sensors-identify

You can determine whether a specific type of sensor exists on a device by using the getDefaultSensor() method and passing in the type constant for a specific sensor. If a device has more than one sensor of a given type, one of the sensors must be designated as the default sensor. If a default sensor does not exist for a given type of sensor, the method call returns null, which means the device does not have that type of sensor.

private SensorManager mSensorManager;
...
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
if (mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD) != null){
  // Success! There's a magnetometer.
}
else {
  // Failure! No magnetometer.
}
like image 29
DmitryDzz Avatar answered Oct 15 '22 20:10

DmitryDzz