ProximitySensor.StopListening won't work

NeoTechni

Well-Known Member
Licensed User
Longtime User
I'd like to turn off my proximity sensor when I'm done with it but I can't get it to do it.

I've used StopListening, and it does nothing.
I've replaced it with Null, and it still kept working.
I've move ProximitySensor from my service to my Activity's Global sub hoping it would get cleared when I close the Activity

But I still get data updates from ValueChanged
 

NeoTechni

Well-Known Member
Licensed User
Longtime User
Ive ripped out all the code for it and redid it from scratch, twice, to be sure it wasnt me
Its not being dimmed twice, and i have it logging where it starts listening and stopping. I can call stoplistening a hundred times and still get valuechanged

I did not put that emoticon
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
I've tried this code and it works as expected:
B4X:
Sub Process_Globals
   Dim ps As PhoneSensors
End Sub

Sub Globals

End Sub

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

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub ps_SensorChanged (Values() As Float)
   Log(Values(0))
End Sub

Sub Activity_Click
   Log("stop")
   ps.StopListening
End Sub
 
Upvote 0

NeoTechni

Well-Known Member
Licensed User
Longtime User
I've rechecked my code, running in debug mode with StartListening with a red dot next to it. I can watch StopListening get called a hundred times and it'll still keep listening. I've made sure the object is only dimmed in one spot.
 
Upvote 0

NeoTechni

Well-Known Member
Licensed User
Longtime User
Yes.

I even have Proximity_SensorChanged call Proximity.StopListening, I'm still getting Proximity_SensorChanged events
I've got it in debug mode and I am watching Proximity.StopListening get called repeatedly without starting it back up again and I'm still getting updates
 
Last edited:
Upvote 0

NeoTechni

Well-Known Member
Licensed User
Longtime User
I removed all the callsubdelayed's I was using in other modules. Turns out stoplistening wont work if you started it from a different module/service.
 
Upvote 0

pfillion

Member
Licensed User
Longtime User
Hi, I tough that it would help to add my finding to this question... I had the same problem as my app has an option to enable and disable the proximity sensor.

It turns out that the problem is not to start listening from a different module/service but it is if you start it again when it is already listening... I tested it and it can still be called from another service/activity with callsubdelayed's.

If you put a Boolean flag to track if it is already started before you try to start it again, it will properly stop listening when StopListening is called.

Don't know if this could be fixed internally in the library...
 
Upvote 0

Scooter Willis

Member
Licensed User
Longtime User
I am having the same issue where stopListening does not work and only have one startListening in the code. I am trying to do everything in a service managed by timers. Reading pressure data and it seems to ignore the speed hint. Basically get a pressure update every second. Would like to a pressure update every 15 minutes as a service. So trying to control the start and stop with a timer at a slow refresh rate.

Based on the Java code that was posted from a prior version in one of the threads the Phone class is static which complicates things for listeners. Any particular reason it is static?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Upvote 0
Top