title: Android proximity sensor issue only in Samsung devices
body: <p><strong>Specific scenario to avoid problems:</strong><br>
  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 <code>onPause()/onResume()</code> ONLY on SAMSUNG devices.</p>
  
  <p>I was clearing the proximity sensors in <code>onPause()</code> which resulted in a behaviour unique to Samsung devices.</p>
  
  <p>Hope this saves some time for anybody who's facing this. I removed the call of clearing proximity listeners from <code>onPause()</code> and now it works as expected on the mentioned devices.</p>
  
  <p><strong><em>UPDATE:</em></strong></p>
  
  <p>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:  </p>
  
  <p><code>if (!mWakeLock.isHeld()) mWakeLock.acquire();</code>  </p>
  
  <p>Randomly, the <code>if()</code> returns false for the mentioned devices hence <code>mWakeLock.acquire()</code> isn't always called.</p>
  
  <p>possibly relevant info:    </p>
  
  <pre><code>mWakeLock = mPowerManager.newWakeLock(field, getLocalClassName()); 
  </code></pre>
  
  <p>where  </p>
  
  <pre><code>field = PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK or 32
  </code></pre>
  
  <hr>
  
  <p>My proximity sensor code works <strong>incorrectly only on</strong><br>
  1. Samsung GT-I9082 Android 4.2.2 (API 17) [duos grand]<br>
  2. Samsung SM-G925I Android 5.1.1 (API 22) [s6 edge]</p>
  
  <p><strong>the code:</strong>  </p>
  
  <pre><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] &lt;= 5) { //Sleep
                              turnOffScreen();
                          } else { //Wake
                              turnOnScreen();
                          }...
   // registering listener with SensorManager.SENSOR_DELAY_NORMAL);  
  </code></pre>
  
  <p><strong>the problem:</strong>
  Logged values returned by proximity sensor on both devices <strong>fluctuate</strong>, even while we place a hand over the phones constantly for a few seconds; 10-15 without moving it.<br>
  Like:<br>
  Samsung 6 edge-  </p>
  
  <pre><code>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()
  </code></pre>
  
  <p>Samsung Duos: the fluctuation values differ in 0.0 and 5.0 unlike 8.0 above </p>
  
  <ul>
  <li><p>What are there other ways or permutation-combinations which can be applied successfully and accurately to solve this issue?</p></li>
  <li><p>Is it because of different sensors; GP2A Proximity Sensor and APDS-9930/QPDS-T930 Proximity &amp; Light ?</p></li>
  </ul>
  
  <p>have already spent quite some time on it..in vain</p>