Android Question Bluetooth printing aborts

Discussion in 'Android Questions' started by Creideiki, Dec 3, 2018.

  1. Creideiki

    Creideiki Member Licensed User

    Since some months our app aborts printing via bluetooth on a DPP-250 printer in the middle of the text.

    I wrote a minimal app to test this (see attached zip). The jpg shows some tests: The first works, the next two abort, the next works, the last aborts.

    I'm using B4A 8.50 with Serial 1.26.
    The tablet is a samsung Tab A (SM-T585) with Android 8.1.0.

    The App simply opens the Serial, initializes a TextWriter with its OutputStream, prints some lines, closes the TextWriter and disconnects the serial connection. This should simply print the lines. But sometimes it doesn't print the whole text, but aborts in the middle of the transmission.
    Since I have no possibility to check if all lines are transmitted before I close, I don't see what I could do to fix this.
     

    Attached Files:

  2. Erel

    Erel Administrator Staff Member Licensed User

    You should never use TextWriter / TextReader with network or Bluetooth streams.

    Try this code:
    Code:
    Sub btSerial_Connected(Success As Boolean)
       
    If Not(Success) Then
           LogT(
    "Verbindung zu DPP-250 fehlgeschlagen")
           lblStatus.Text = 
    "Verbindung zu DPP-250 fehlgeschlagen"
           
    Return
       
    End If
       LogT(
    "Drucker verbunden.")
       
    Dim astream As AsyncStreams
       astream.Initialize(btSerial.InputStream, btSerial.OutputStream, 
    "astream")
       
    Dim out As OutputStream
       out.InitializeToBytesArray(
    1)
       
    ' 4. Textwriter erzeugen
       Dim writer As TextWriter
       writer.Initialize2(out, 
    "ISO-8859-1")
       LogT(
    "Drucken startet")
       lblStatus.Text = 
    "Printing..."

       
    ' 5. Ganz viel drucken
       writer.WriteLine("Test-Druck")
       
    For i = 1 To 10
           writer.WriteLine(
    "Zeile " & i)
       
    Next
       writer.WriteLine(
    "Letzte Zeile")
       writer.WriteLine(
    "")
       writer.WriteLine(
    "")
       writer.WriteLine(
    "")
       astream.Write(out.ToBytesArray)
       astream.SendAllAndClose
    End Sub

    Sub AStream_NewData (Buffer() As Byte)
       
    End Sub

    Sub AStream_Terminated
       
    Log("Terminated")
       
    ' 6. Textwriter schließen
       LogT("Drucken beendet, Writer wird geschlossen")
       lblStatus.Text = 
    "Closing..."

       
    ' 7. Drucker schließen
       LogT("Verbindung zum Drucker wird beendet")
       btSerial.Disconnect

       
    ' fertig.
       LogT("Fertig.")
       lblStatus.Text = 
    "Fertig."
    End Sub

    Sub AStream_Error
       
    Log("astream error")
    End Sub
    Note that there are other possible causes for such errors.
     
  3. Creideiki

    Creideiki Member Licensed User

    Thank you very much. I'll try this ASAP.

    Would it be possible to use the OutputStream from AsyncStreams directly? I think that would reduce the memory fingerprint.

    BTW: What's the problem with TextWriter and Network?
     
  4. Erel

    Erel Administrator Staff Member Licensed User

    The problem is that you are reading or writing to a network stream from the main thread. This can cause the main thread to freeze.
     
  5. Creideiki

    Creideiki Member Licensed User

    I see. So another thing one should do asynchron.
     
  6. Star-Dust

    Star-Dust Expert Licensed User

    Peter Simpson likes this.
  7. Creideiki

    Creideiki Member Licensed User

    Don't I have to close the TextWriter somewhere? Or the OutputStream?
     
    Last edited: Dec 7, 2018
  8. Creideiki

    Creideiki Member Licensed User

    Seems I have to:
    Code:
    writer.WriteLine("")
       writer.close
       astream.Write(out.ToBytesArray)
    Now it works.

    What about the OutputStream? Has it to be closed, too?
     
  9. Creideiki

    Creideiki Member Licensed User

    Well... it only works partly.
    The end of the sub looks like that:
    Code:
    out.Close
    If astream.Write(out.ToBytesArray) = False Then Log("AStream.Write returns false!")
    If astream.SendAllAndClose = False Then Log("AStream.SendAllAndClose returns false!")
    Wait For AStream_Terminated
    btSerial.Disconnect
    Sometimes it prints correctly, sometimes there are a few lines missing. They are printed the next time I try to print... but also not completely.
    out and astream are local to the sub, btSerial is global.

    I'm running out of ideas...
     
  10. Creideiki

    Creideiki Member Licensed User

    Some hundred chars worked, but with a bit graphics it went to >2k chars and that still broke... the last part of the graphics was missing.

    But now it seems to be stable.

    I had to insert a Sleep(1000) (Sleep(0) was not enough!):
    Code:
    If astream.Write(out.ToBytesArray) = False Then Log("AStream.Write returns false!")
    Sleep(
    1000)
    If astream.SendAllAndClose = False Then Log("AStream.SendAllAndClose returns false!")
    Wait For AStream_Terminated
    btSerial.Disconnect
    Can you tell me why?

    BTW: To print graphics with TextWriter isn't a good idea either... TextWriter replaces the encoding which may break the graphics, too.
    I had to write a little replacement of TextWriter with WriteRaw(b() as byte).
     
  11. Erel

    Erel Administrator Staff Member Licensed User

    That's true. Avoid using TextWriter at all. You don't really need it. Check BytesBuilder for the best option.

    There are cases where the data seems to be sent although it is actually buffered in a lower level.
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice