-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSensorCast.b4a
More file actions
164 lines (140 loc) · 5.14 KB
/
SensorCast.b4a
File metadata and controls
164 lines (140 loc) · 5.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
Build1=Default,soac.uvigo
File1=Layout.bal
FileGroup1=Default Group
Group=Default Group
Library1=core
Library2=network
Library3=phone
Library4=xui
Library5=json
ManifestCode='This code will be applied to the manifest file during compilation.~\n~'You do not need to modify it in most cases.~\n~'See this link for for more information: https://www.b4x.com/forum/showthread.php?p=78136~\n~AddManifestText(~\n~<uses-sdk android:minSdkVersion="21" android:targetSdkVersion="35"/>~\n~<supports-screens android:largeScreens="true" ~\n~ android:normalScreens="true" ~\n~ android:smallScreens="true" ~\n~ android:anyDensity="true"/>)~\n~SetApplicationAttribute(android:icon, "@drawable/icon")~\n~SetApplicationAttribute(android:label, "$LABEL$")~\n~CreateResourceFromFile(Macro, Themes.LightTheme)~\n~'End of default text.~\n~AddPermission(android.permission.INTERNET)~\n~AddPermission(android.permission.ACCESS_NETWORK_STATE)~\n~AddPermission(android.permission.ACCESS_WIFI_STATE)
Module1=Starter
NumberOfFiles=1
NumberOfLibraries=5
NumberOfModules=1
Version=13.4
@EndOfDesignText@
#Region Project Attributes
#ApplicationLabel: Sensor Android SCOA
#VersionCode: 1
#VersionName:
'SupportedOrientations possible values: unspecified, landscape or portrait.
#SupportedOrientations: unspecified
#CanInstallToExternalStorage: False
#End Region
#Region Activity Attributes
#FullScreen: False
#IncludeTitle: True
#End Region
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
Dim U As UDPSocket
Dim S As ServerSocket
End Sub
Sub Globals
'These global variables will be redeclared each time the activity is created.
Dim ps As PhoneSensors
Dim psa As PhoneSensors
Private LabelMagX As Label
Private LabelMagY As Label
Private LabelMagZ As Label
Private LabelAccX As Label
Private LabelAccY As Label
Private LabelAccZ As Label
Private lblIPServer As Label
Private ListaDeClientes As List
Dim acc() As Float
Dim mag() As Float
End Sub
Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("Layout")
ps.Initialize(ps.TYPE_MAGNETIC_FIELD)
psa.Initialize(ps.TYPE_ACCELEROMETER)
' Inicializa servidor UDP en el puerto 51042
U.Initialize("UDP", 51042, 8192) 'tag, puerto, buffer
S.Initialize(0, "S")
Dim ipAny As String = S.GetMyIP
Dim ipWifi As String = S.GetMyWifiIP
Log($"Mi IP: ${ipAny} (WiFi: ${ipWifi})"$)
lblIPServer.Text = $"IP: ${ipAny} (WiFi: ${ipWifi})"$
ListaDeClientes.Initialize
End Sub
Sub Activity_Resume
ps.StartListening("SensorMag")
psa.StartListening("SensorAcc")
End Sub
Sub Activity_Pause (UserClosed As Boolean)
ps.StopListening
psa.StopListening
End Sub
Sub SensorMag_SensorChanged (Values() As Float)
' Valores del campo magnético en µT
Dim magX As Float = Values(0)
Dim magY As Float = Values(1)
Dim magZ As Float = Values(2)
mag=Values
LabelMagX.Text = "X: " & magX
LabelMagY.Text = "Y: " & magY
LabelMagZ.Text = "Z: " & magZ
EnviarDatosUDP
End Sub
Sub SensorAcc_SensorChanged (Values() As Float)
' Valores del campo magnético en µT
Dim accX As Float = Values(0)
Dim accY As Float = Values(1)
Dim accZ As Float = Values(2)
acc=Values
LabelAccX.Text = "X: " & accX
LabelAccY.Text = "Y: " & accY
LabelAccZ.Text = "Z: " & accZ
EnviarDatosUDP
End Sub
Sub EnviarDatosUDP()
If acc <> Null And mag <> Null And acc.Length > 0 And mag.Length > 0 Then
Dim Datos As Map
Datos.Initialize
' Map para acelerómetro
Dim accMap As Map
accMap.Initialize
accMap.Put("x", acc(0))
accMap.Put("y", acc(1))
accMap.Put("z", acc(2))
' Map para magnetómetro
Dim magMap As Map
magMap.Initialize
magMap.Put("x", mag(0))
magMap.Put("y", mag(1))
magMap.Put("z", mag(2))
Datos.Put("accelerometer", accMap)
Datos.Put("magnetometer", magMap)
' Convertir a JSON
Dim JSONGen As JSONGenerator
JSONGen.Initialize(Datos)
Dim jsonString As String = JSONGen.ToString
' Enviar por UDP a todos los clientes registrados
For Each Cliente As String In ListaDeClientes
Dim Packet As UDPPacket
Packet.Initialize(jsonString.GetBytes("UTF8"), Cliente, 51043) 'puerto de destino
U.Send(Packet)
Next
End If
End Sub
Sub UDP_PacketArrived (Packet As UDPPacket)
Dim msg As String
msg = BytesToString(Packet.Data, Packet.Offset, Packet.Length, "UTF8")
Log($"Paquete recibido de: ${Packet.Host} con mensaje: "${msg}"$)
' 1. Comprobamos si es un mensaje de registro de un nuevo cliente
If msg = "HOLA" Then
' 2. Verificamos si la IP del cliente NO está ya en la lista
If ListaDeClientes.IndexOf(Packet.Host) = -1 Then
' 3. Si es nuevo, lo añadimos a la lista
ListaDeClientes.Add(Packet.Host)
LogColor($"Nuevo cliente detectado y añadido: ${Packet.Host}"$, Colors.Green)
Log($"Clientes conectados: ${ListaDeClientes.Size}"$)
Else
LogColor($"El cliente ${Packet.Host} ya estaba registrado."$, Colors.Blue)
End If
End If
' Aquí podrías añadir un 'Else If msg = "UNREGISTER_ME"' para que los clientes se den de baja
End Sub