B4A Library [class] AsyncStreamsText - Useful when working with streams of text

Martin Larsen

Active Member
Licensed User
Please use [code]code here...[/code] tags when posting code.

Note that AsyncStreamsText is more sophisticated than the code you previously used. It collects the text so if a message is split into several "packets" it will still be parsed correctly.
I have edited the post to include code tags. It looks much better that way :)

AsyncStreamsText is without doubt more sophisticated, but my code actually also collects packets as btText is a global variable. So even if the text arrives in chunks it is appended to btText and only interpreted when a CRLF is detected.
 

Reyes5

Member
Licensed User
Hi Erel,
I am still strugling with using AsyncStreamText class in your bluetooth example.
I have to communicate with a microprocessor uart over BT.
It worked fine in older android versions with textreader and writer ... but I have to port to Android KitKat and Lollipop now.
Your Bluetooth example with Asyncstreams works fine, than I try to build it with AsyncStreamText class,
but then I get this error as soon as BT is connected :

** Activity (main) Resume **
** Activity (main) Resume **
RE000505

:00:00:12:06:52:81
connected: true
** Activity (main) Pause, UserClosed = false **
** Activity (chatactivity) Create, isFirst = true **
Error occurred on line: 17 (asyncstreamstext)
java.lang.RuntimeException: Object should first be initialized (InputStream).
at anywheresoftware.b4a.AbsObjectWrapper.getObject(AbsObjectWrapper.java:46)
at anywheresoftware.b4a.samples.bluetooth.asyncstreamstext._initialize(asyncstreamstext.java:55)
at anywheresoftware.b4a.samples.bluetooth.chatactivity._activity_create(chatactivity.java:308)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:636)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:305)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:238)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:121)
at anywheresoftware.b4a.samples.bluetooth.chatactivity.afterFirstLayout(chatactivity.java:98)
at anywheresoftware.b4a.samples.bluetooth.chatactivity.access$100(chatactivity.java:16)
at anywheresoftware.b4a.samples.bluetooth.chatactivity$WaitForLayout.run(chatactivity.java:76)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
** Activity (chatactivity) Resume **

I add the zip of the project.
Where do I go in error ? Thanks very much for your help !
Paul.
 

Attachments

HARRY

Active Member
Licensed User
Hi,

I want to use AsyncStreamsText as a client. The server is an Arduino, which collects data. The B4A program reads over Wifi from time to time the collected data. In the Arduino program I take care of CRLF when needed.

Up till now I cannot find a complete example to use AsyncStreamsText as a client. With complete I mean with connecting/disconnecting to the server, initializing the sockets , etc.. Can somebody help me?

Harry
 

Erel

Administrator
Staff member
Licensed User
AsyncStreamsText is not related to client or server app. It manages a connection.

It doesn't matter if the current app is the server or client.
 

GuyBooth

Active Member
Licensed User
I want to use this approach for my code but struggling to make it work.
I have tried the class and example exactly as written, and added a line in the FIrstTime loop to prove that the server initializes.
The server initializes, but the event server_NewConnection doesn't trigger. As a result the ast doesn't initialize, and the code fails when activity_Click is triggered.
The log is:

B4X:
** Activity (main) Create, isFirst = true **


192.168.3.121


Server.IsInitialized is true
** Activity (main) Resume **


java.lang.RuntimeException: Object was not initialized.


    at anywheresoftware.b4a.debug.Debug.PushSubsStack(Debug.java:121)
    at b4a.example.asyncstreamstext._write(asyncstreamstext.java:240)
    at b4a.example.main._activity_click(main.java:343)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:187)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:175)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:171)
    at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:77)
    at android.view.View.performClick(View.java:4222)
    at android.view.View$PerformClick.run(View.java:17273)
    at android.os.Handler.handleCallback(Handler.java:615)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:4895)
    at java.lang.reflect.Method.invokeNative(Native Method)


    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:994)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)
    at dalvik.system.NativeStart.main(Native Method)
I have run out of ideas - can anyone help? This should be soooo simple :( ...
 

mr23

Active Member
Licensed User
I want to use this approach for my code but struggling to make it work.
I have tried the class and example exactly as written, and added a line in the FIrstTime loop to prove that the server initializes.
The server initializes, but the event server_NewConnection doesn't trigger. As a result the ast doesn't initialize, and the code fails when activity_Click is triggered.
The log is:

B4X:
** Activity (main) Create, isFirst = true **


192.168.3.121


Server.IsInitialized is true
** Activity (main) Resume **


java.lang.RuntimeException: Object was not initialized.


    at anywheresoftware.b4a.debug.Debug.PushSubsStack(Debug.java:121)
...
I have run out of ideas - can anyone help? This should be soooo simple :( ...

It would help get a response if you posted a simplified version of your code, or a zip of the project.
 

GuyBooth

Active Member
Licensed User
Sorry if I wasn't clear, but this is entirely based on the example at the beginning of this thread - I haven't been able to get far enough to incorporate it into my own project at this stage.
From what I see in my tests, the server_NewConnection has to be triggered by an incoming message - but I have to send a message first in order to trigger an incoming one. Catch 22 if this is the case ... so what is missing from the example?
 

Erel

Administrator
Staff member
Licensed User
You don't need to send a message in order to connect the sockets. These are two different actions. First the sockets are connected and then you can send messages over the connection.

There is no catch 22 here...
 

Declan

Active Member
Licensed User
Hi,
I am attempting to use AsyncStreamsText to read the message received on the USB from an Arduino UNO.
I have modified the USB Serial Example code.
The code compiles OK, but when I run the app on my Android device:
I get the messagebox displaying the Device Information.
BUT, when I press the OK button on the MessageBox, I get:
"Unfortunately, USB Serial Example has stopped"

The code where the app stops is:
B4X:
Sub btnOpen_Click
   If usb.UsbPresent = usb.USB_NONE Then
     Log("Msgbox - no device")
     Msgbox("No USB device or accessory detected!", "Error")
     Log("Msgbox - returned")
     Return
   End If
  
    Log("Checking permission")
    If (usb.HasPermission) Then
        Msgbox(usb.DeviceInfo, "Device Information")
        Dim dev As Int
        dev = usb.Open(9600)       
        If dev <> usb.USB_NONE Then
            Log("Connected successfully!")
            btnOpen.Enabled = False
            btnClose.Enabled = True
            btnSend.Enabled = True           
            ast.Initialize(usb,"ast",usb.GetInputStream,usb.GetOutputStream)
        Else
            Log("Error opening USB port")
        End If
    Else
        usb.RequestPermission
    End If
End Sub
I cannot understand what is causing the app to stop?
 

Erel

Administrator
Staff member
Licensed User
You need to check the logs and post the full error message (in a new thread please).
 

Erel

Administrator
Staff member
Licensed User
Are you testing it in debug mode?

B4A-Bridge will only show the logs in debug mode.
 

Chuckman200

New Member
Licensed User
All,

I am missing something with AsysnStreamsText
I am just using Erel's sample code.
Upon Compiling I receive the following error:

B4A version: 5.80 BETA #1
Parsing code. Error
Error parsing program.
Error description: Unknown type: asyncstreamstext
Are you missing a library reference?
Occurred on line: 16 (Main)
Dim ast As AsyncStreamsText


I thought that AsyncStreams was a portion of the Random Access Library.
Reviewing the documentation it has no member "Text"
Is their another Library I should include?

Thank you all for the help.
 

Erel

Administrator
Staff member
Licensed User
You need to add the AsyncStreamsText class from the first post (Project - Add Existing Module).
 
Top