How to check TYPE_SIGNIFICANT_MOTION sensor availability in android device?

2.2k Views Asked by At

I have used the below code to check the TYPE_SIGNIFICANT_MOTION sensor availability.

    SensorManager mSensorManager = (SensorManager) context
            .getSystemService(Context.SENSOR_SERVICE);

    List<Sensor> sensors = mSensorManager
            .getSensorList(Sensor.TYPE_SIGNIFICANT_MOTION);

    boolean isSensorAvailable = Boolean.valueOf(sensors.size() > 0);

I have tested in three different devices. All the devices returning false.

In doc they given that TYPE_SIGNIFICANT_MOTION Constants added in API 18. How to check this sensor availability to version prior to 18. Correct me to get the right sensor availability status.

1

There are 1 best solutions below

2
On

Based on the documentation, it's suggested to check the availability like this:

if (mSensorManager.getDefaultSensor(Sensor.TYPE_SIGNIFICANT_MOTION) != null){
    mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_SIGNIFICANT_MOTION);
}

For version prior to 18, it'll always be null since it's not defined yet.

Since the significant motion sensors is based on accelerometer, the reason all the devices you tested are returning false when checking this sensor might be that the manufactures didn't implement this software sensor using the accelerometer. But you can always implement something similar yourself.

Hope this helps!