#Region Service Attributes
#StartAtBoot: False
#End Region
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
Public UDPSck As UDPSocket
Private Tablet As Reflector
'Dim Device As Phone
'Dim ServerIndex As Int = -1
'Dim RequestIP As Boolean = True
Public UDPPort As Int = 4444
Public myIP As String
Type typPeers (name As String, IP As String, role As String, dtime As Long,screens As String)
Public peer As typPeers
Public peers As Map
End Sub
Sub Service_Create
Tablet.Target = Tablet.GetContext
Tablet.Target = Tablet.RunMethod2("getSystemService", "wifi", "java.lang.String")
Tablet.Target = Tablet.RunMethod2("createMulticastLock", "mylock", "java.lang.String")
Tablet.RunMethod2("setReferenceCounted", False, "java.lang.boolean") 'not really necessary but safer
Tablet.RunMethod("acquire") 'acquire the lock
UDPSck.Initialize("UDP", UDPPort, 4096) 'Port and buffer size can be changed
ToastMessageShow("Start UDP Engine --> Port " & UDPPort & " initialized.",False)
Dim server As ServerSocket : myIP = server.GetMyWifiIP
peers.Initialize
End Sub
Sub Service_Start (StartingIntent As Intent)
End Sub
Sub UDP_PacketArrived(Packet As UDPPacket)
Dim msg As String = BytesToString(Packet.Data, Packet.Offset, Packet.Length, "UTF8")
Dim IP As String = Packet.HostAddress
Dim parts() As String = Regex.Split("~", msg)
Dim s As String = "Receiving: " & msg & " from: " & IP
Log(s)
ToastMessageShow(s,True)
Select Case parts(0)
Case "BCAST_ALL"
Case "WHOAMI"
Case "KALARM"
If myIP = IP Then Exit
End Select
End Sub
Sub Service_Destroy
UDPSck.Close
'--- release when not needed to save battery
Tablet.RunMethod("release")
End Sub