Sensors - but which one?

LineCutter

Active Member
Licensed User
Longtime User
I seem to have missed something when it comes to sensor events. As long as it's just the one sensor I'm OK, but despite trimming the sensors example down to it's essence I'm unsure about getting the INT that tells me which sensor fired the event.
Minimalist code (all extraneous stuff such as stopping listening has been removed. Run at your own risk!):
B4X:
Sub Process_Globals
End Sub

Sub Globals
End Sub

Sub Activity_Create(FirstTime As Boolean)
    If FirstTime Then
        Dim ps As PhoneSensors 'This object is only used to access the type constants.
        Log("Accel="&ps.TYPE_ACCELEROMETER)
Msgbox(ps.TYPE_ACCELEROMETER,"Accelerometer")
'These appear to return 1 for the Value of TYPE_ACCELEROMETER
        ps.Initialize(ps.TYPE_ACCELEROMETER)
'This is basically what happens in the AddSensor Sub in the example program
        ps.StartListening("Sensor")
'Starts listening to the accelerometer
    End If
End Sub

Sub Activity_Resume
End Sub

Sub Activity_Pause (UserClosed As Boolean)
End Sub

Sub Sensor_SensorChanged (Values() As Float)
    Dim ps As PhoneSensors
    ps=Sender
'ps has a constant for TYPE_ACCELEROMETER, but how do I read what TYPE the sender is?
    Log(Sender)
    Msgbox(Sender,ps.TYPE_ACCELEROMETER)
End Sub
Is it mandatory to use a map & use the whole sensor object as a key in order to identify it?:sign0085:
 

LineCutter

Active Member
Licensed User
Longtime User
There is only one sensor, but I can't tell which one it is from the sensor_Sensorchanged event.... I think that a light may have come on:
If I set up the listening with different senor names then I can use different events to tell which one has fired.
BUT that's 3 lots of code to maintain if I use the magnetic, orientation & accelerometer sensors. Using sender would be better. I haven't worked out how to do that (does it require a map in order to match the phonesensors object to a usable tag which I can use for an IF...Then statement?).
 
Upvote 0

LineCutter

Active Member
Licensed User
Longtime User
Thanks Erel :icon_clap:

It's not flattering to admit how much time I spent trying to get this to work as a 1 step process. (& impressive how the penny can drop as soon as you make your confusion public)

FWIW this strategy works:
B4X:
Sub ACCEL_SensorChanged (Values() As Float)
    Sensor_Changed("Accel",Values)
End Sub
Sub MAGNET_SensorChanged (Values() As Float)
    Sensor_Changed("Magnet",Values)
End Sub
Sub ORIENT_SensorChanged (Values() As Float)
    Sensor_Changed("Orient",Values)
End Sub
Sub Sensor_Changed (Sensor As String,Values() As Float)
log(Sensor&" "&NumberFormat(Values(0), 0, 3))
End Sub
 
Upvote 0

Beja

Expert
Licensed User
Longtime User
was thinking about raising a help flag, but was a little hesitant.
I am only interested in a light sensor example (just light)...
a sensors' example with so many sensors makes it a little confusing for beginners
like myself.
 
Upvote 0
Top