iOS Question Any example to use AsyncStreamsObject to receive data from server please?!

rkwan

Member
Licensed User
Hello!

I am a newbie to the iOS app development as I just purchased the B4i yesterday.

I tried to use the example from

https://www.b4x.com/android/forum/threads/asyncstreamsobject-module.48276/#content

but I don't see any explicit use to read data from the connected server there.

In fact, I did use the B4A to have my simple Android app to connect to the server with:-

Sub TcpStreams_NewData (Buffer() As Byte)
Dim Rx_String As String

If Buffer.Length = 4 Then
Rx_String = BytesToString(Buffer, 0, 4 , "UTF8")
dataLabel.Text = Rx_String
End If
End Sub

and

Sub WiFi_Socket_Connected (Successful As Boolean)

If Successful = True Then
ToastMessageShow("Connected",True)
TcpStreams.Initialize(WiFi_Socket.InputStream,WiFi_Socket.OutputStream,"tcpStreams")
Else
ToastMessageShow("Server Not Available. Pls check for ESP_0191A9 Wifi.",True)
End If

End Sub

Therefore, I would like to ask if it would be that straight-forward without any change on server side,
I can use something like:-

Sub astreamO_NewData (Buffer() As Byte)
Dim Rx_String As String

If Buffer.Length = 4 Then
Rx_String = BytesToString(Buffer, 0, 4 , "UTF8")
dataLabel.Text = Rx_String
End If
End Sub

if I follow the example to have

Private astreamO As AsyncStreamsObject

and

Sub StartAstream(s As Socket)
astreamO.Start(s.InputStream, s.OutputStream)
SetUIState
End Sub

Or it should be

Sub Astream_NewData (Buffer() As Byte)

instead?!

Thanks & Best Regards,
Robert
 

rkwan

Member
Licensed User
Hmm...I just realized that I interpreted the sample codes incorrectly yesterday.

Actually, data receiving from server can be accessed via:

Sub astreamO_NewObject(Key As String, Value As Object)
Select Key
Case "simple value"
Dim number As Int = Value
hd.ToastMessageShow("Received lucky number: " & number, False)
End Select
End Sub

Correct?!

Then, my correct question should be how I should modify my ESP8266 or STM Wifi server code to send the object instead please?!

In my B4A app before, my ESP8266 or STM Wifi server just sent simply the numeric string only.

Can I modify it to send as "simple value XXXX" where XXXX is the numeric string please?

Or any way I can keep the same server code but just modify the AsyncStreamObject code please?!


Thanks & Best Regards,
Robert
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Upvote 0

rkwan

Member
Licensed User
Thanks, Erel!

Yes, I do have trouble with the B4A example as it is using

Type=Service

but this is not supported in B4i, right?
How should I modify it please?

On the other hand, I tried mimick another B4A code from another chat before without the TCP part:-

B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'Public variables can be accessed from all modules.
    Public App As Application
    Public NavControl As NavigationController
    Private Page1 As Page
    Private astream1 As AsyncStreams
    Private server As ServerSocket
    Private client As Socket
    Private port As Int = 5000
    Private hd As HUD
    Private distanceLabel As Label
   
    Dim MobileIP As String
    Dim ServerIp As String = "172.24.226.1"    ' STM IP Address

    Private ipaddLabel As Label
    Private s_ip_addLabel As Label
    Private port_addLabel As Label

End Sub

Private Sub Application_Start (Nav As NavigationController)
    NavControl = Nav
    Page1.Initialize("Page1")
    Page1.RootPanel.LoadLayout("main1")
    NavControl.ShowPage(Page1)
    server.Initialize(port, "server")
    client.Initialize("client")
    MobileIP = server.GetMyIP
    ipaddLabel.Text = MobileIP
    port_addLabel.Text = port
    s_ip_addLabel.Text = ServerIp
    server.Listen
    client.Connect(s_ip_addLabel.Text, port, 30000)
End Sub

Sub Application_Active
  
End Sub

Sub StartAstream(s As Socket)
    astream1.InitializePrefix(s.InputStream, True, s.OutputStream, "astream1")
End Sub

Sub astream1_NewData (Buffer() As Byte)
    Dim Rx_String As String

        Rx_String = BytesToString(Buffer, 0, 4 , "UTF8")
        distanceLabel.Text = Rx_String
End Sub

Sub astream1_Error
    Log("Error")
    server.Listen
End Sub

Sub client_Connected (Successful As Boolean)
    If Successful Then
        StartAstream(client)
        MobileIP = server.GetMyIP
        ipaddLabel.Text = MobileIP
    Else
        hd.ToastMessageShow("Error: " & LastException, True)
    End If
End Sub

Sub Server_NewConnection (Successful As Boolean, NewSocket As Socket)
    If Successful Then
        StartAstream(NewSocket)
        MobileIP = server.GetMyIP
        ipaddLabel.Text = MobileIP
    Else
        hd.ToastMessageShow("Error: " & LastException, True)
    End If
End Sub

but it doesn't work. I mean, no data seems to be received even though the server connection seems correct.

Any idea please?


Thanks & Best Regards,
Robert
 
Upvote 0

rkwan

Member
Licensed User
And again, my functional B4A app before for your reference please.

B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Dim WiFi_Socket As Socket
    Dim TcpStreams As AsyncStreams
    Dim ServerSocket1 As ServerSocket 
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 MobileIP As String 
    Dim ServerIp As String = "172.24.226.1"    ' STM Wifi IP Address - Varies, depending on country
    Dim Port As Int = 5000                    ' Fixed Port Number

    Private exitButton As Button
   
    Private ipaddLabel As Label
    Private s_ip_addLabel As Label
    Private port_addLabel As Label
    Private distanceLabel As Label
    Private dummyLabel As Label
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("Main")
    WiFi_Socket.Initialize("WiFi_Socket")
    WiFi_Socket.Connect(ServerIp,Port,10000)
End Sub

Sub Activity_Resume
    ServerSocket1.Initialize(0, "")
    MobileIP = ServerSocket1.GetMyIP 
    ipaddLabel.Text = MobileIP
    port_addLabel.Text = Port
    s_ip_addLabel.Text = ServerIp
   
End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub WiFi_Socket_Connected (Successful As Boolean)
   
    If Successful = True Then
        ToastMessageShow("Connected",True)
        TcpStreams.Initialize(WiFi_Socket.InputStream,WiFi_Socket.OutputStream,"tcpStreams")   
    Else
        ToastMessageShow("Server Not Available. Pls check for STM_Net Wifi.",True)
    End If
       
End Sub

Sub exitButton_Click
    'send the 'Message' text as a Message
   WiFi_Socket.Close
    TcpStreams.Close
    Activity.Finish
End Sub

Sub connectButton_Click
    WiFi_Socket.Initialize("WiFi_Socket")
    ServerIp = s_ip_addLabel.Text
    Port = port_addLabel.Text
    WiFi_Socket.Connect(ServerIp,Port,5000)
End Sub

' Reception part
Sub TcpStreams_NewData (Buffer() As Byte)
    Dim Rx_String As String

    If Buffer.Length = 4 Then
        Rx_String = BytesToString(Buffer, 0, 4 , "UTF8")
        distanceLabel.Text = Rx_String
    End If


End Sub

Thanks & Best Regards,
Robert
 
Upvote 0

rkwan

Member
Licensed User
void loop() {
client1 = server1.available();

String readString;

// Wait until the client sends some data
Serial.println("new client");
while( client1.connected() )
{
char buffer[10];

//Hard-coded string to be sent to B4i app
readString = "S123";

if (readString.substring(0,1) == "S")
{
char charBuf[4];
int count = 0;
readString.substring(1).toCharArray(charBuf, 4) ;
for (int i=0; i<readString.length(); i++)
{
if (isDigit(charBuf))
{
count++;
}
}
if (count == readString.length()-1)
{
distance = atoi(&charBuf[0]);
}
snprintf(buffer,5,"%4d",distance);

client1.print(buffer); // Sending to B4a & B4i app
client1.flush();
}
readString="";

} // while (client.connected)

Serial.println("new client disconnected");
client1.stop();
}
 
Upvote 0

rkwan

Member
Licensed User
Hi Erel,

Reposted it back here correctly.

As I said before, I would prefer not to have much changes to the current Arduino codes until I really need to do more complicated object based communications.
Therefore, I hope that this can be addressed without the need of B4R for the moment...


Thanks & Best Regards,
Robert
 
Upvote 0

rkwan

Member
Licensed User
No, not today with your latest example.
I did try yesterday but not sure if I used the parameters correctly or not?!
Are they the same parameters as for Prefix mode please?!

Thanks & Best Regards,
Robert
 
Upvote 0
Top