Sub Process_Globals
End Sub
Sub Globals
Dim Socket As JavaObject
Dim arr() As String
Dim t As Thread
End Sub
Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("timeguardian1")
t.Initialise("ConnectToServer_Task") ' Initializes thread
arr = Array As String("192.168.1.50","12345") 'IP address of PC, open port of PC
End Sub
Sub ButtonStop_Click
Log("Sending STOP command...")
ConnectToServerAsync
End Sub
Sub ConnectToServerAsync()
Log("log1")
If t.IsInitialized Then Log("is initialized")
t.Start(Me, "ConnectToServer_Task", arr) ' !!!!This instruction does not start the sub ConnectToServer_Task!!!!
Log("log2")
End Sub
Sub ConnectToServer_Task()
Log("log3")
Dim jo As JavaObject
Try
jo.InitializeNewInstance("java.net.Socket", Array As Object(arr))
Log("Connection successful!")
CallSubDelayed2(Me, "ConnectionEstablished", jo)
Catch
Log("Error during connection: " & LastException.Message)
End Try
End Sub
Sub ConnectionEstablished(jo As JavaObject)
Socket = jo
Log("Connection established, ready to send commands.")
SendCommand("STOP")
End Sub
Sub SendCommand(command As String)
If Socket.IsInitialized Then
Try
Dim outputStream As JavaObject = Socket.RunMethod("getOutputStream", Null)
outputStream.RunMethod("write", Array As Object(command.GetBytes("UTF-8")))
outputStream.RunMethod("flush", Null)
Log("Command sent successfully: " & command)
Catch
Log("Error while sending command: " & LastException.Message)
End Try
Else
Log("Error: the connection is not initialized.")
End If
End Sub
Sub CloseConnection
If Socket.IsInitialized Then
Try
Socket.RunMethod("close", Null)
Log("Connection closed.")
Catch
Log("Error while closing the connection: " & LastException.Message)
End Try
End If
End Sub