Android Question Error sending large amounts of data vía BH

wildfandango

Member
Licensed User
Longtime User
Hello everyone I would appreciate some help with a problem that I am not able to solve....

I tried this code snippet

it works perfectly, but when I try to print a lot of data it just doesn't work.

If I make a loop that prints for example 100 lines, there comes a time when it stops working. So, If I put a pause (e.g. msgbox) on that loop... it works! ¿¿??

I have to flush at the end of each line?
I have to wait for something before sending the next line or closing the BT connection?

Here is an example of my code....

B4X:
Sub btPRINTER_Connected (Success As Boolean)
    Dim prnBUF As TextWriter
    Dim txtENC As String
    Dim lstBUF As List
    Dim i As Int

    ' I dont know the apropiate CP for spanish chars like ó, ñ or €
    txtENC = "CP858"
    lstBUF = File.ReadList(dir, fic)

    If Success Then
        prnBUF.Initialize2(btCONN.OutputStream,txtENC)
        For i = 0 To lstBUF.Size - 1
            prnBUF.WriteLine(lstBUF.Get(i))
            prnBUF.Flush
        
            ' For large amount of lines only works with this pause
            Msgbox("LIN." & i & CRLF & lstBUF.Get(i),"TEST")
        Next
        prnBUF.Flush
        prnBUF.Close
        ToastMessageShow("("&lstBUF.Size&") Done...",False)
    Else
        Msgbox("ERROR...","ERROR BT")
    End If
 
    ' I must wait before close?
    btCONN.Disconnect
End Sub
 
Last edited:

wildfandango

Member
Licensed User
Longtime User
SOLVED!

Thx EREL :)

With a few changes in my code, works perfectly!

B4X:
Dim Astream As AsyncStreams
....
....

Sub btPRINTER_Connected (Success As Boolean)
    Dim txtENC As String
    Dim txtBUF As String

    dr = base.itemx(prnFIC,";",1)
    fc = base.itemx(prnFIC,";",2)

    txtENC = "UTF8" ' Still no € symbol

    txtBUF =  File.ReadString(dir, fic)

    If Success Then
        If Not(Astream.IsInitialized) Then Astream.Initialize(btCONN.InputStream,btCONN.OutputStream,"printerAS")
        Astream.Write(txtBUF.GetBytes(txtENC))
    Else 
        Msgbox("ERROR...","ERROR BT")
    End If

    ' DO NOT DISCONECT!
    ' btCONN.Disconnect
End Sub
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Try it with:
(it requires a recent version of B4A)
B4X:
Sub btPRINTER_Connected (Success As Boolean)
    Dim txtENC As String
    Dim txtBUF As String

    dr = base.itemx(prnFIC,";",1)
    fc = base.itemx(prnFIC,";",2)

    txtENC = "UTF8" ' Still no € symbol

    txtBUF =  File.ReadString(dir, fic)

    If Success Then
        If Astream.IsInitialized Then AStream.Close
        Astream.Initialize(btCONN.InputStream,btCONN.OutputStream,"printerAS")
        Astream.Write(txtBUF.GetBytes(txtENC))
        AStream.SendAllAndClose
        Wait For AStream_Terminated
        Log("done")
        btCONN.Disconnect
    Else 
        MsgboxAsync("ERROR...","ERROR BT")
    End If
End Sub
 
Upvote 0

wildfandango

Member
Licensed User
Longtime User
Thank you, Erel.

I'm going to update to the new version (@ac2.*), until now I had 6.80 and had already noticed the "Wait For" that looks very good...

I hope the project will load and compile smoothly in the new version...
 
Upvote 0

wildfandango

Member
Licensed User
Longtime User
All done but...

but the line Wait For AStream_Terminated

compiling shows this error

Static code modules cannot handle events
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
you need to use the communication part in a Service or an Activity or in a Class. It does not work in Code-Modules.
 
Upvote 0
Top