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.
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.
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).
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.
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);
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.
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With