Main:
Starter:
B4X:
#Region Project Attributes
#ApplicationLabel: B4A Example
#VersionCode: 1
#VersionName:
'SupportedOrientations possible values: unspecified, landscape or portrait.
#SupportedOrientations: portrait
#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.
End Sub
Sub Globals
'These global variables will be redeclared each time the activity is created.
'These variables can only be accessed from this module.
Private btnConnect As Button
Private btnSend As Button
Private IPtxt As EditText
Private lblStatus As Label
Private txtPort As EditText
Private ImageView1 As ImageView
End Sub
Sub Activity_Create(FirstTime As Boolean)
'Do not forget to load the layout file created with the visual designer. For example:
Activity.LoadLayout("FrmMain")
End Sub
Sub Activity_Resume
SetState
End Sub
Public Sub SetState
btnSend.Enabled = Starter.connected
If Starter.connected Then
btnConnect.Text = "Disconnect"
lblStatus.Text = "Connected"
Else
btnConnect.Text = "Connect"
lblStatus.Text = "Disconnected"
End If
End Sub
Sub btnSend_Click
CallSub2(Starter, "SendData", "0")
End Sub
Public Sub Data_Received (data() As Byte)
End Sub
Sub btnConnect_Click
If Starter.connected = False Then
If IPtxt.Text.Length = 0 Then
ToastMessageShow("Please enter the server ip address.", True)
Return
Else
CallSub3(Starter, "ConnectToServer", IPtxt.Text, txtPort.Text)
End If
Else
CallSub(Starter, "Disconnect")
End If
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub
Starter:
B4X:
#Region Service Attributes
#StartAtBoot: False
#ExcludeFromLibrary: 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.
Public connected As Boolean
Private client As Socket
Private astream As AsyncStreams
End Sub
Sub Service_Create
'This is the program entry point.
'This is a good place to load resources that are not specific to a single activity.
End Sub
Sub Service_Start (StartingIntent As Intent)
End Sub
Public Sub ConnectToServer(Host As String, Port As String)
Log("Trying to connect to: " & Host)
CloseExistingConnection
Dim client As Socket
client.Initialize("client")
client.Connect(Host, Port, 10000)
Wait For Client_Connected (Successful As Boolean)
If Successful Then
astream.InitializePrefix(client.InputStream, False, client.OutputStream, "astream")
UpdateState (True)
Else
Log("Failed to connect: " & LastException)
End If
End Sub
Public Sub Disconnect
CloseExistingConnection
End Sub
Sub CloseExistingConnection
If astream.IsInitialized Then astream.Close
If client.IsInitialized Then client.Close
UpdateState (False)
End Sub
Sub UpdateState (NewState As Boolean)
connected = NewState
CallSub(Main, "SetState")
End Sub
Sub AStream_Error
UpdateState(False)
End Sub
Sub AStream_Terminated
UpdateState(False)
End Sub
Attachments
Last edited: