Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android detect is gyroscope available

How can I detect whether the android device has gyroscope? Currently I'm using the following method to detect whether gyroscope is available. If gyroscope not available, then use accelerometer.

SensorManager mgr = (SensorManager) getSystemService(SENSOR_SERVICE);
        List<Sensor> sensors = mgr.getSensorList(Sensor.TYPE_ALL);
        for (Sensor sensor : sensors) {
            Log.i("sensors",sensor.getName());
           if(sensor.getName().contains("Gyroscope")){
               try{
                mSensorManager.registerListener(this,
                           mSensorManager.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR),
                           SensorManager.SENSOR_DELAY_FASTEST);
                return;
               }catch(Exception e){
               }
           }
        }
        mSensorManager.registerListener(this,
                mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
                SensorManager.SENSOR_DELAY_FASTEST);

However some of my app users complaint they can't use gyroscope. Is there any problem with the code?

like image 516
JR Tan Avatar asked Jul 01 '13 04:07

JR Tan


People also ask

Do all Android phones have a gyroscope?

Most Android-powered devices have an accelerometer, and many now include a gyroscope.

Do all smartphones have gyroscope?

Modern smartphones use a kind of gyroscope that consists of a tiny vibrating plate on a chip. When the phone's orientation changes, that vibrating plate gets pushed around by the Coriolis forces that affect objects in motion when they rotate.


2 Answers

You can simply use the Package Manager's System features to check if the device has a gyroscope in it:

PackageManager packageManager = getPackageManager();
boolean gyroExists = packageManager.hasSystemFeature(PackageManager.FEATURE_SENSOR_GYROSCOPE);

Your current code may not work on some devices as the sensor name is not guaranteed to contain Gyroscope in it.

like image 136
Raghav Sood Avatar answered Sep 28 '22 08:09

Raghav Sood


Use this:

mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
if(mSensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE) != null)
{

         // Sensor FOUND
}
else
{
        //Sensor NOT FOUND
}
like image 34
Rajath S Avatar answered Sep 28 '22 10:09

Rajath S