Android Question Bluetooth printing aborts

Similar threads

Bug? Bluetooth printing aborts
B4A Tutorial Text files
B4A Library Printing and Pdf creation
B4A Question Classes are soon coming...
B4A Question SSL Websocket client

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.
 

Attachments

Erel

Administrator
Staff member
Licensed User
You should never use TextWriter / TextReader with network or Bluetooth streams.

Try this code:
B4X:
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.
 

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?
 

Erel

Administrator
Staff member
Licensed User
BTW: What's the problem with TextWriter and Network?
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.
 

Creideiki

Member
Licensed User
Seems I have to:
B4X:
   writer.WriteLine("")
   writer.close
   astream.Write(out.ToBytesArray)
Now it works.

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

Creideiki

Member
Licensed User
Well... it only works partly.
The end of the sub looks like that:
B4X:
 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...
 

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!):
B4X:
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).
 

Erel

Administrator
Staff member
Licensed User
To print graphics with TextWriter isn't a good idea either
That's true. Avoid using TextWriter at all. You don't really need it. Check BytesBuilder for the best option.

I had to insert a Sleep(1000) (Sleep(0) was not enough!):
There are cases where the data seems to be sent although it is actually buffered in a lower level.
 
Top