Unexpected false return value from android.hardware.SensorManager.registerListener in Android 4.3

Used LG Nexus 4, Android 4.3

The return value is sometimes false, and I do not understand why. The documentation says: "true if the sensor is supported and successfully turned on."

It is strange that no examples that I saw check the return value from SensorManager.registerListener.

It is also strange that if I ignore that a false value is returned, everything works as expected!

Documentation: http://developer.android.com/reference/android/hardware/SensorManager.html

android.hardware.SensorManager, android.hardware.SystemSensorManager

My code is:

SensorManager sensorMgr = (SensorManager)_context.getSystemService(Context.SENSOR_SERVICE); if (sensorMgr == null) { Log.w(TAG, "200410::Sensors not supported"); return false; } Sensor sensor = sensorMgr.getDefaultSensor(Sensor.TYPE_PROXIMITY); result = sensorMgr.registerListener(this, sensor, SensorManager.SENSOR_DELAY_NORMAL); if( result == false) { // Normally something should be done here // since the false return value indicated that registration failed. // But I found it better doing nothing since the registration seams to be ok } return result; 

Strange false return value didn't happen before Android 4.3

So, I'm fine if the result value is not used, as in all examples. Maybe I should be satisfied with this, but I have 3 questions:

  • Why has this return value never been tested in the examples?
  • Why is false returned in Android 4.3?
  • Why does it work even if the return value is false?

I understand that these questions are difficult to answer, but I will be very happy if I get information that someone else has a problem.

I got the same behavior with both Sensor.TYPE_ACCELEROMETER and Sensor.TYPE_PROXIMITY

+4
source share

Source: https://habr.com/ru/post/1500395/


All Articles