Android Tutorial [B4X] AsyncStreams Tutorial

Status
Not open for further replies.

TAK

Member
Licensed User
Longtime User
Sample working code posted

Hi,
As a beginner I know that it is difficult to wrap your head around Sockets, Server Sockets and Asyncstreams...

....
Hi,
the tutorial on page 1 is difficult to understand for a beginner. The example of flyingbag ist easier. I understand how i can send the stream from a device, but i dont understand how i can get and read this stream with B4J.
 

deantangNYP

Active Member
Licensed User
Longtime User
I would like to use AsyncStream.
1) May i know how can i TEST this code from post #1? install on 2 device?
2) How can i establish CONNECTION?
Please help. Tks.

B4X:
Sub Process_Globals
    Dim AStreams As AsyncStreams
    Dim Server As ServerSocket
    Dim Socket1 As Socket
End Sub
Sub Globals
    Dim EditText1 As EditText
End Sub

Sub Activity_Create(FirstTime As Boolean)
    If FirstTime Then
        Server.Initialize(5500, "Server")
        Server.Listen
        Log("MyIp = " & Server.GetMyIP)
    End If
    EditText1.Initialize("EditText1")
    EditText1.ForceDoneButton = True
    Activity.AddView(EditText1, 10dip, 10dip, 300dip, 60dip)
End Sub

Sub Server_NewConnection (Successful As Boolean, NewSocket As Socket)
    If Successful Then
        ToastMessageShow("Connected", False)
        Socket1 = NewSocket
        AStreams.InitializePrefix(Socket1.InputStream, False, Socket1.OutputStream, "AStreams")
    Else
        ToastMessageShow(LastException.Message, True)
    End If
    Server.Listen
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

Sub AStreams_Error
    ToastMessageShow(LastException.Message, True)
End Sub

'press on the Done button to send text
Sub EditText1_EnterPressed
    If AStreams.IsInitialized = False Then Return
    If EditText1.Text.Length > 0 Then
        Dim buffer() As Byte
        buffer = EditText1.Text.GetBytes("UTF8")
        AStreams.Write(buffer)
        EditText1.SelectAll
        Log("Sending: " & EditText1.Text)
    End If
End Sub

Sub Activity_Pause(UserClosed As Boolean)
    If UserClosed Then
        Log("closing")
        AStreams.Close
        Socket1.Close
    End If
End Sub
 
Last edited:

deantangNYP

Active Member
Licensed User
Longtime User
Can you point me to the post that requires 2 android devices.
thanks

Yes. You need to install it on two devices. Another option is to implement one side in B4J and run it on the PC (there are several such examples).
 

Coffeeman95

New Member
Licensed User
Longtime User
I agree! I have been programming in VB for 10 years and B4A one of the hardest ones to learn! Even the examples have errors. If I have to debug the examples just to make them work, then perhaps I have selected the wrong platform.
 

Coffeeman95

New Member
Licensed User
Longtime User
I appreciate you getting back with me so soon, and I believe I will stick with B4A, because the support is pretty darn good. I sometimes get a little frustrated when I can not see what is plain to others. After I posted that message, I took a small break and came back to re-read some examples and this time everything fell into place. Again, I thank everyone here for their support.
 

jay0103

Member
Licensed User
Longtime User
hi

I have two activitys : Activity1 and Activity2.

Acitvity1 : (Socket1 connected)
If receiving datas , AStreams_NewData event occurs.

Acitvity2 : (Socket1 connected)
If receiving datas , AStreams_NewData event doesn't occur.
When it returns Activity1, AStreams_NewData event occur.

How could the event AStreams_NewData occur, either program is in activity1 or activity2 ?
 

Coffeeman95

New Member
Licensed User
Longtime User
Hello Everyone.
First, Thank you again for the great support from everyone. My question regarding the example in this thread is this: The code complied with NO errors and ran with no errors as well, but my question is where did my data go? How can I check to be sure the data I sent is correct and not corrupted. I do have 2 Android devices, but I did not see the data on the receiving Android. Both Android devices showed the " Log("MyIp = " & Server.GetMyIP) as correct 192.168.1.103 and the other 192.168.1.104" I have looked and pondered the example and while it is probably simple to see, I am missing it somehow. Thank you in advance for a nudge in the right direction.
Gary
 

Coffeeman95

New Member
Licensed User
Longtime User
AsyncStream always not initialized

I'm having a problem with AsyncStreams. IsInitialized always returns false.
I tried the flyingbag example above and it does the same thing.
Any pointers to what the problem might be?
Thanks.

Hi flyingbag. I downloaded your example .zip files and hopefully this will answer the question I asked previously. I will need some time, as you can tell I am not the brightest programmer compared the rest of the group. Thanks!
Gary
 

r.babazadeh

Member
Licensed User
Longtime User
HI
I Send below data Whit micro controller via Bluetooth

Tx_tbl(01) = "L"
Tx_tbl(02) = Ip
Tx_tbl(03) = Nip
Tx_tbl(04) = L_speed
Tx_tbl(05) = H_speed
Tx_tbl(06) = light


And In B4A, I Cant Receive byte
I receive: L?????

Each character also has a 3-byte ASCII code that the data submitted do not change
In Vb6 i can comfortable with ASCII command , converted received characters to bytes
 

Manuel Moreno-Eguilaz

New Member
Licensed User
Longtime User
Is there any kind of "DoEvents" for AsyncStreams?

Using USBSerial, we have noticed it is necessary to finish one subroutine in the main activity before receiving data from asyncstreams.

Thank you.

Manuel
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…