user2450263 user2450263 - 6 months ago 49
Android Question

Android proximity sensor issue only in Samsung devices

Specific scenario to avoid problems:

Behaviour for Activity in Samsung devices was different in the manner that every time there was a change detected, for proximity, it resulted in a call to

onPause()/onResume()
ONLY on SAMSUNG devices.

I was clearing the proximity sensors in
onPause()
which resulted in a behaviour unique to Samsung devices.

Hope this saves some time for anybody who's facing this. I removed the call of clearing proximity listeners from
onPause()
and now it works as expected on the mentioned devices.

UPDATE:

What is mentioned below is not the only issue, the proximity sensor does not consistently behave in that manner. The other issue is a l-o-c:

if (!mWakeLock.isHeld()) mWakeLock.acquire();


Randomly, the
if()
returns false for the mentioned devices hence
mWakeLock.acquire()
isn't always called.

possibly relevant info:

mWakeLock = mPowerManager.newWakeLock(field, getLocalClassName());


where

field = PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK or 32





My proximity sensor code works incorrectly only on

1. Samsung GT-I9082 Android 4.2.2 (API 17) [duos grand]

2. Samsung SM-G925I Android 5.1.1 (API 22) [s6 edge]

the code:

sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
sensor = sensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
...new SensorEventListener() {
@Override
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() == Sensor.TYPE_PROXIMITY) {
if (event.values[0] <= 5) { //Sleep
turnOffScreen();
} else { //Wake
turnOnScreen();
}...
// registering listener with SensorManager.SENSOR_DELAY_NORMAL);


the problem:
Logged values returned by proximity sensor on both devices fluctuate, even while we place a hand over the phones constantly for a few seconds; 10-15 without moving it.

Like:

Samsung 6 edge-

02-10 20:12:36.532: D/SensorManager(3467): Proximity, val = 8.0 [far]
02-10 20:12:36.532: D/SensorManager(29335): Proximity, val = 8.0 [far]
02-10 20:12:36.532: D/DisplayPowerController(3467): [sensor] setProximitySensorEnabled::unregisterListener
02-10 20:12:36.532: D/PowerManagerService(3467): [s] DisplayPowerCallbacks : onProximityNegative()
02-10 20:12:36.562: I/Sensors(3467): Proximity old sensor_state 33554560, new sensor_state : 33554432 en : 0
02-10 20:12:36.632: I/Sensors(3467): Proximity old sensor_state 33554944, new sensor_state : 33555072 en : 1
02-10 20:12:36.642: D/SensorManager(29335): registerListener :: 5, TMD4903 Proximity Sensor, 200000, 0,
02-10 20:12:36.652: D/SensorManager(29335): Proximity, val = 8.0 [far]
02-10 20:12:36.662: I/Sensors(3467): ProximitySensor - 8(cm)
02-10 20:12:36.672: D/SensorManager(29335): Proximity, val = 8.0 [far]
02-10 20:12:41.752: I/Sensors(3467): Proximity old sensor_state 33554560, new sensor_state : 33554432 en : 0
02-10 20:12:41.822: I/Sensors(3467): Proximity old sensor_state 33554432, new sensor_state : 33554560 en : 1
02-10 20:12:41.842: D/SensorManager(29335): registerListener :: 5, TMD4903 Proximity Sensor, 200000, 0,
02-10 20:12:41.842: D/SensorManager(29335): Proximity, val = 8.0 [far]
02-10 20:12:41.872: I/Sensors(3467): ProximitySensor - 8(cm)
02-10 20:12:41.872: D/SensorManager(29335): Proximity, val = 8.0 [far]
02-10 20:12:50.482: I/Sensors(3467): ProximitySensor - 0(cm)
02-10 20:12:50.482: D/SensorManager(29335): Proximity, val = 0.0 [close]
02-10 20:12:50.482: D/DisplayPowerController(3467): [sensor] setProximitySensorEnabled::registerListener
02-10 20:12:50.482: D/SensorManager(3467): registerListener :: 5, TMD4903 Proximity Sensor, 200000, 0,
02-10 20:12:50.482: D/SensorManager(3467): Proximity, val = 0.0 [close]
02-10 20:12:50.482: D/PowerManagerService(3467): [s] DisplayPowerCallbacks : onProximityPositive()
02-10 20:12:50.482: D/PowerManagerService(3467): [s] DisplayPowerCallbacks : onProximityPositive()


Samsung Duos: the fluctuation values differ in 0.0 and 5.0 unlike 8.0 above


  • What are there other ways or permutation-combinations which can be applied successfully and accurately to solve this issue?

  • Is it because of different sensors; GP2A Proximity Sensor and APDS-9930/QPDS-T930 Proximity & Light ?



have already spent quite some time on it..in vain

Answer

So turns out this is a registered issue with Samsung devices, here is a collection of pages which could help whoever, who is facing this:

  1. Use onWindowFocusChanged. This made sense to me and my implementation related to onPause() was successful on following the same.
    Source and Explanation: activity-onpause-to-handle-focus
    What helped me: instead of in onPause();

    @Override public void onWindowFocusChanged(boolean hasFocus) { super.onWindowFocusChanged(hasFocus); if(!hasFocus){ clearProximityListeners(); } }

  2. Issue 74464: Activity#onPause called ...THIS gets you the "got out of jail" moment after trying everything else without much success

  3. More reference:
    From quite a different aspect, Bug on unregister...Otto bus.. quote: For us, this only appears on Samsung devices. And after a quick search,...

Please feel free to edit, correct the answer, hope it saves some time.