I looked over the Bluetooth code, but I don’t see its relevance
I looked over the Bluetooth code, but I must be missing something; I don’t see its relevance to my attempt to send and receive via sockets.
This code below will successfully contact my Java server, but the server does not show sent text until I fully shut down the Android client(thank you Rbsoft; I added AStreams_NewData (Buffer() As Byte), and it may help after I get through this issue)
So, when I hit btnTest, I see nothing on the server, but when I exit the Android app all the “hello’s” appear.
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
Dim Socket1 As Socket
Dim ServerSocket1 As ServerSocket
Dim AStreams As AsyncStreams
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.
Dim Timer1 As Timer
Dim btnConnect As Button
Dim btnClose As Button
Dim btnTest As Button
Dim lvLog As ListView
Dim lblMyIP As Label
Dim label1 As Label
Dim label2 As Label
Dim label4 As Label
End Sub
Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("socket3")
'Todo: Change typeface of lvLog
' lvlog.Initialize("")
'lvlog.Color ="white"
' lvLog.SingleLineLayout.ItemHeight = 16dip
' lvLog.SingleLineLayout.Label.TextSize = 12
lblMyIP.Initialize("")
label1.text = ServerSocket1.GetMyIP
End Sub
Sub btnTest_Click
Dim Buffer() As Byte
msg = "Hello 2222"
Buffer = msg.GetBytes("UTF8")
Astreams.Write(Buffer)
' lvLog.AddSingleLine ("Sent " & msg)
label2.Text = "Sent " & msg
End Sub
''''''''''''''''
Sub AStreams_NewData (Buffer() As Byte)
Dim msg As String
msg = BytesToString(Buffer, 0, Buffer.Length, "UTF8")
label2.Text = "BACK !!!!! " & msg
End Sub
''''''''''
Sub btnConnect_Click
Socket1.Initialize("Socket1")
Socket1.Connect("192.168.0.45",2222,5000)
'lvLog.AddSingleLine ("Do Connect")
End Sub
Sub btnClose_Click
Socket1.Close
' lvLog.AddSingleLine ("Do Close")
label2.Text = "close"
End Sub
Sub Socket1_Connected(Connected As Boolean)As Boolean
If Connected = True Then
AStreams.Initialize(Socket1.InputStream,Socket1.OutputStream,"Astreams")
'lvLog.AddSingleLine ("Is Connected")
label1.text = "connected"
End If
End Sub
Sub Activity_Resume
ServerSocket1.Listen
End Sub
Sub Activity_Pause (UserClosed As Boolean)
If UserClosed Then
Timer1.Enabled = False
Socket1.Close
ServerSocket1.Close 'stop listening
End If
End Sub
Sub ServerSocket1_NewConnection (Successful As Boolean, NewSocket As Socket)
If Successful Then
Socket1 = NewSocket
Timer1.Enabled = True
InputStream1 = Socket1.InputStream
OutputStream1 = Socket1.OutputStream
ToastMessageShow("Connected", True)
Else
Msgbox(LastException.Message, "Error connecting")
End If
ServerSocket1.Listen 'Continue listening to new incoming connections
label4.text = InputStream1
End Sub
=======================
This code, taken verbatim from the site, appears not to function; that is my java server shows no indication of being contacted.
Sub process_globals
Dim UDPSocket1 As UDPSocket
End Sub
Sub Globals
End Sub
Sub Activity_Create(FirstTime As Boolean)
If FirstTime Then
UDPSocket1.Initialize("UDP", 0, 8000)
End If
Dim Packet As UDPPacket
Dim data() As Byte
data = "Hello from Android".GetBytes("UTF8")
Packet.Initialize(data, "192.168.0.45", 2222)
UDPSocket1.Send(Packet)
End Sub
Sub UDP_PacketArrived (Packet As UDPPacket)
Dim msg As String
msg = BytesToString(Packet.Data, Packet.Offset, Packet.Length, "UTF8")
Msgbox("Message received: " & msg, "")
End Sub