Android proximity sensor issue only in Samsung devices
Asked Answered
F

1

11

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

Filter answered 10/2, 2016 at 15:2 Comment(6)
See: #33405759Resurrection
accuracy is not my problem, either the question has been misunderstood or i didn't understand what you want to say, can you elaborateFilter
I think its that beyond 1 bit of value the meaning of the proximity sensor is up to the ODM so the fact that the value varies while covered/uncovered just means that its an implementation detail and not 'incorrect' behavior.Resurrection
thats where i think i'm missing something... even with keeping it covered constantly for 15-20seconds , it fluctuates within that duration...while its covered. So maybe there has to be another condition to check with if less than or equal to 5 ..check powerManager service and wakelock examples also, but the same device has perfectly working proximity wakelock for native and whatsapp callsFilter
Have you checked using the accelerometer/orientation sensor. I was under the belief that locking the screen only makes sense when you are holding the phone next to your ear vs cupping the screen in bright sunlight.Resurrection
@MorrisonChang observed that onResume() was being called with changes in proximity, for only Samsung devicesFilter
F
3

So turns out this is a registered issue with Samsung devices, here is a collection of pages which could help anybody 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 without Activity#onResume, or can Context#registerReceiver failed

  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 edit, correct, improve the answer if there's any more clarity to it.. hope it saves some time.

Filter answered 29/5, 2016 at 10:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.