AsyncStream with WIFI connection

cmartins

Member
Licensed User
Longtime User
Hi everybody


I am using the wifi connections to send text data to other device.

This is working well on some devices, but there are problems in others.

Usually happens to lack of data transmission when the device is not unrooted. Below is a sample test done between 2 devices.

i.g. 01:

Rooted cell phone (server listening on port 2222) - unrooted device.
unrooted device (client) message: Successfully connected
rooted device (server) message: Connected.
data transmition OK

UnRooted cell phone (server listening on port 2222) - Rooted device (client).
Rooted device (client) message: Successfully connected
UnRooted device (server) message: NO MESSAGE
data transmition - Fail

How can I solve it?





B4X:
'Activity module
Sub Process_Globals
    Dim Server As ServerSocket
    Dim ClientSocket As Socket        
    Dim AStream As AsyncStreams        '
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Display")
If FirstTime Then
   Server.Initialize(2222, "Server")
End If
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub Close_Click
    If ClientSocket.Connected Then
       Astream.Close
       ClientSocket.Close
    End If
End Sub



Sub ServerClick_Click
   Server.Listen
End Sub

Sub Server_NewConnection (Successful As Boolean, NewSocket As Socket)
    If Successful Then
        ToastMessageShow("Connected", False)
        Socket1 = NewSocket
        AStreams.Initialize(Socket1.InputStream, Socket1.OutputStream, "AStreams")
    Else
        ToastMessageShow(LastException.Message, True)
    End If
    Server.Listen
End Sub


Sub Connect_Click
    If Not(ClientSocket.IsInitialized) Then
       ClientSocket.Initialize("ClientSocket")
    End If
    ClientSocket.Connect("192.168.154.85", 2222, 2000)
End Sub



Sub ClientSocket_Connected (Successful As Boolean)
    If Successful Then
       AStream.Initialize(ClientSocket.InputStream,    
                                ClientSocket.OutputStream, "AStreams")

    ToastMessageShow("Successfully connected", True)
    Else
    ToastMessageShow("No connection!", True)
   End If
    
End Sub

Sub AStreams_NewData (Buffer() As Byte)
    Dim msg As String
    msg = BytesToString(Buffer, 0, Buffer.Length, "UTF8")
    ToastMessageShow(msg, False)
    Log(msg)
End Sub
 

cmartins

Member
Licensed User
Longtime User
I took peaces of code, but this is the code



B4X:
Sub SendData
If AStreamClient.IsInitialized Then
   txtSds = "Data Test"
   If txtSds.Length > 0 Then
      Dim buffer() As Byte
      buffer = txtSds.GetBytes("UTF8")
      AStreamClient.Write(buffer)
   End If
End If
End Sub
 
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
Using streams with unrooted devices (both clients and servers) 99% of the time, I can assure you, there is no problem with it. There has to be something with your connection or streams code. Before looking into it, sure you set your devices IPs in the correct manner? Sometimes, we tend to do everything except from looking at the simplest causes.
 
Upvote 0

cmartins

Member
Licensed User
Longtime User
Solved


I was putting on

Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("Display")
If FirstTime Then
Server.Initialize(2222, "Server")
End If
End Sub

Now I put on the button click and use only when I really need

In the original application. my Sub activity_create starts lots services include screen orientation
 

Attachments

  • WIFITransfer.zip
    7.4 KB · Views: 390
Upvote 0
Top