#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.
Dim ast As AsyncStreamsText
Dim server As ServerSocket
Dim Socket1 As Socket 'receive
Dim AStreams As AsyncStreams 'send
Dim DEST_IP As String ="192.168.178.29"
Dim DEST_PORT As Int =51042
Dim notify1 As Notification
Dim notify2 As Notification
Dim Timer1 As Timer
Dim Received As String=""
Dim Status As String=""
Dim socket_connected As Boolean=False
End Sub
Sub Service_Create
server.Initialize(DEST_PORT, "server")
Log(server.GetMyWifiIP)
server.Listen
Socket1.Initialize("Socket1")
Socket1.Connect(DEST_IP , DEST_PORT, 5000)
Timer1.Initialize("Timer1", 5000)
Timer1.Enabled=False
notify1.Initialize
notify1.Icon = "icon"
notify1.Vibrate = False
' notify1.Vibrate = True
notify1.Sound = False
' notify1.Sound = True
notify2.Initialize
' notify2.Icon = "icon"
notify2.Icon = "chip_black_hsm" 'Achtung Icon im Ordner \Objects\res\drawable UND Schreibgeschützt !!!
notify2.Vibrate = False
notify2.Sound = False
End Sub
Sub Timer1_Tick
' Try to connect again
Timer1.Enabled=False
If socket_connected=False Then
Socket1.Close
Socket1.Initialize("Socket1")
Socket1.Connect(DEST_IP , DEST_PORT, 5000)
End If
End Sub
Sub Socket1_Connected(Connected As Boolean)As Boolean
If Connected = True Then
Timer1.Enabled=False
'ToastMessageShow("Connected",True)
AStreams.Initialize(Socket1.InputStream,Socket1.OutputStream,"Astreams")
SendData2("connected "&DateTime.Date(DateTime.Now) & "-" & DateTime.Time(DateTime.Now))
SendData2("My IP="&server.GetMyWifiIP)
socket_connected=True
notify2.AutoCancel=False 'notify..verschwindet nicht wenn User drauftippt
notify2.OnGoingEvent=True
notify2.SetInfo2("H-S-M Service running", DateTime.Date(DateTime.Now) & "-" & DateTime.Time(DateTime.Now),"0", Me)
notify2.Notify(2)
Else
' ToastMessageShow("Server not available",True)
Status="Server not available"
socket_connected=False
notify2.cancel(2)
Timer1.Enabled=True
End If
' btnConnect.Enabled = True
End Sub
Sub SendData2(msg As String)
Dim Buffer() As Byte
' Buffer = msg.GetBytes("UTF8")
Buffer = msg.GetBytes("ISO-8859-1")
AStreams.Write(Buffer)
End Sub
Sub server_NewConnection (Successful As Boolean, NewSocket As Socket)
If Successful Then
If ast.IsInitialized Then ast.Close
ast.Initialize(Me, "ast", NewSocket.InputStream, NewSocket.OutputStream) 'initialize AsyncStreamsText with the socket streams.
' notify2.AutoCancel=False 'notify..verschwindet nicht wenn User drauftippt
' notify2.OnGoingEvent=True
' notify2.SetInfo2("H-S-M Service running", DateTime.Date(DateTime.Now) & "-" & DateTime.Time(DateTime.Now),"0", Me)
' notify2.Notify(2)
Else
' notify2.cancel(2)
Log(LastException)
End If
server.Listen
End Sub
Sub ast_NewText(Text As String)
Dim Buffer() As Byte
Buffer = Text.GetBytes("UTF8")
Text=BytesToString(Buffer, 0, Buffer.Length, "UTF8")
Log("Text: " & Text)
Received=Text 'wird in main über timer_received abgeholt
' notify1.SetInfo(Text, DateTime.Date(DateTime.Now) & "-" & DateTime.Time(DateTime.Now), Me)
notify1.AutoCancel=True 'notify..verschwindet wenn User drauftippt
notify1.SetInfo2(Text, DateTime.Date(DateTime.Now) & "-" & DateTime.Time(DateTime.Now),"0", Main)
notify1.Notify(1)
End Sub
Sub ast_Terminated
Log("Connection terminated")
Timer1.Enabled=True
socket_connected=False
' notify2.OnGoingEvent=False
' notify2.Cancel(2)
End Sub
Sub astreams_disconnected
Log("Connection terminated")
Timer1.Enabled=True
socket_connected=False
End Sub
Sub astreams_error
Timer1.Enabled=True
socket_connected=False
End Sub
Sub Service_Start (StartingIntent As Intent)
Status="Service startet"
End Sub
Sub Service_Destroy
Socket1.Close
Status="Service stopped"
notify2.OnGoingEvent=False
notify2.Cancel(2)
End Sub
Sub Service_TaskRemoved
'This event will be raised when the user removes the app from the recent apps list.
Socket1.Close
notify1.Cancel(1)
notify2.OnGoingEvent=False
notify2.Cancel(2)
End Sub