Android Question Proximity sensor

luke2012

Well-Known Member
Licensed User
Longtime User
Hi to all !

I'm trying to implement a screen management within a phone app.
My target is to switch off the screen when the user put the phone near the ear and switch on when put off the phone from ear.

B4X:
Sub Process Global
    Dim ps As PhoneSensors
End Sub

Sub Activity_Create(FirstTime As Boolean)
    If FirstTime Then
      ps.Initialize(ps.TYPE_PROXIMITY)
      ps.StartListening("ps")
    End If
End Sub

Sub ps_SensorChanged (Values() As Float)
   Log(Values(0))
   If Values(0)=0 Then 
        Log("Screen switch off")
        phoneWS.ReleaseKeepAlive
   Else 
        Log("Screen switch on")
        phoneWS.KeepAlive(True)
   End If
End Sub

I'm running the code to handle the sensor but I it doesn't work as expected : the screen switch on / off isn't excuted.
Any Ideas?
 

luke2012

Well-Known Member
Licensed User
Longtime User
Does the device you are testing your code on has a proximity sensor?, I tested your code and it works on my phone.

Yes! I can see the Values of the sensor in the log.

So on your device the screen is switched off when you put the phone near your ear?
 
Upvote 0

luke2012

Well-Known Member
Licensed User
Longtime User
The sensor listening works fine on my device (Nexus 4 - Android JB 4.3).
In debug mode I can see the values of the prox. sensor in realtime.

What I don't able to do is switch off and switch on the screen on demand in the "Sub ps_SensorChanged".

The methods "phoneWS.ReleaseKeepAlive" and "phoneWS.KeepAlive(True)" (see the code above) do nothing on my device (Android 4.3).
 
Last edited:
Upvote 0

luke2012

Well-Known Member
Licensed User
Longtime User
So how to solve this problem?

I can monitor the prox. sensor but I cannot disable / enable the touchscreen on demand :-(
 
Upvote 0

luke2012

Well-Known Member
Licensed User
Longtime User
Thanks Erel! I'll try.

But the real need is handle the screen dim and bright while the App is in the foreground.
I don't undersand why we have to put the code in the service ?
 
Upvote 0

luke2012

Well-Known Member
Licensed User
Longtime User
Well, that is weird, on my phone I can see the screen go dim and bright if I get near or step away.

NJDude thanks for your feedback!
Can I ask you some info about the environment where the code was executed ?

Android version, B4A Version, Special permissions ?
The App have only the main module?
 
Upvote 0
Top