android detect is gyroscope available

Posted by Tan Jit Ren on Stack Overflow See other posts from Stack Overflow or by Tan Jit Ren
Published on 2013-07-01T04:09:56Z Indexed on 2013/07/01 4:21 UTC
Read the original article Hit count: 177

Filed under:
|

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?

© Stack Overflow or respective owner

Related posts about android

Related posts about gyroscope