#Region Project Attributes
#ApplicationLabel: B4A Example
#VersionCode: 1
#VersionName: Socket Example
'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 my_ssl_socket As SocketSSL
Dim AStreams_ssl As AsyncStreamsSSL
Dim Thread1 As Thread
Dim Thread2 As Thread
Dim Lock1 As Lock
Dim Lock2 As Lock
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.
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("Layout1")
If FirstTime Then
Thread1.Initialise("Thread1")
Thread2.Initialise("Thread2")
Lock1.Initialize(True)
Lock2.Initialize(True)
End If
Dim args(0) As Object
Thread1.Start(Null, "ThreadSub1", args)
'Thread2.Start(Null, "ThreadSub2", args) '<< Run this code to disconnect the SSL Socket
End Sub
Sub ThreadSub1
End1 = 0
Dim Count As Int
Dim Params(1) As Object
Do While Count < 1000
Count = Count + 1
Params(0) = Count
end1 = Count
ok = False
Do Until ok
' this is because Android seems to lose the run message if the user presses back button
' this way no message will be ignored
Thread1.RunOnGuiThread("Update1", Params)
ok = Lock1.WaitFor(1000)
Loop
Loop
End Sub
Sub ThreadSub2
End1 = 0
Dim Count As Int
Dim Params(1) As Object
Do While Count < 1000
Count = Count + 1
Params(0) = Count
end1 = Count
ok = False
Do Until ok
' this is because Android seems to lose the run message if the user presses back button
' this way no message will be ignored
Thread1.RunOnGuiThread("Update2", Params)
ok = Lock2.WaitFor(1000)
Loop
Loop
End Sub
Sub Update1(count As Int)
If my_ssl_socket.IsInitialized = False Then my_ssl_socket.Initialize("my_ssl_socket")
my_ssl_socket.Connect("192.168.0.100","3051",5000)
Lock1.Unlock
End Sub
Sub Update2(count As Int)
my_ssl_socket.Close
Lock2.Unlock
End Sub
Sub my_ssl_socket_Connected (Successful As Boolean)
Log("Connected - SSL Socket")
AStreams_ssl.Initialize(my_ssl_socket.InputStream ,my_ssl_socket.OutputStream ,"AStreams")
End Sub
Sub AStreams_NewData (Buffer() As Byte)
' Data from connection
Dim msg As String
Dim cMsg As String
msg = BytesToString(Buffer, 0, Buffer.Length, "ASCII")
Log("incoming data - " & msg)
End Sub
Sub SendData(msg As String)
Dim Buffer() As Byte
Buffer = msg.GetBytes("UTF8")
AStreams_ssl.Write(Buffer)
AStreams_ssl.Write(Array As Byte(254))
Log("outgoing data - " & msg)
End Sub
Sub Activity_Resume
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub