Android Question How to "Modal print" using Bluetooth

EduardoElias

Active Member
Licensed User
Longtime User
Using this code:

B4X:
Public Sub DirectPrint(aPrinter As String, aData As String)
    Dim PairedDevices As Map
    Dim LocalBTMac As String
    Dim LocalOSerial As Serial
    Dim LocalPrinter As TextWriter
    PairedDevices = OSerial.GetPairedDevices
    For i = 0 To PairedDevices.Size - 1
        Dim device As String = PairedDevices.GetKeyAt(i)
        If device.Contains(aPrinter) Then
            LocalBTMac = PairedDevices.get(device)
            LocalOSerial.Initialize("DirectPrint")
            LocalOSerial.Connect(LocalBTMac)
            LocalPrinter.Initialize(LocalOSerial.OutputStream)
            LocalPrinter.WriteLine(aData)
            LocalPrinter.Flush
            LocalPrinter.Close
            LocalOSerial.Disconnect           
        End If
    Next
End Sub

It should work to get a a String and send to BT printer.

HOWEVER, the Serial.Connect is done in background and it is not immediate, so the next line (Printer.Initialize) does not work because is not ready yet.

How could I have a kind of Modal use of the Bluetooth, because I do not want to have overlapped use of Bluetooth. I need to open, print and close, since are more than one printer and used many times.

Thank you !
 

Robert Valentino

Well-Known Member
Licensed User
Longtime User
I believe after you do the LocalOSerial.Connect(LocalBTMac) you need to handle the _Connected message.

The code below is from a Bluetooth Print example I posted a few years back

B4X:
Sub Class_Globals

  Private mBTMacAddress         As String

  Private mBTConnection         As Serial
  Private mBTPrinter         As TextWriter
  Private mBTConnected         As Boolean
End Sub

Public  Sub Initialize
       mBTConnected     = False
       mBTMacAddress     = ""
       
      mBTConnection.Initialize("BTPrinter")
End Sub

Private Sub BTPrinter_Connected(Connected As Boolean)     '  I believe your sub should be "DirectPrint_Connected"
       mBTConnected = Connected
 
       If  mBTConnected Then
         Try
           mBTPrinter.Initialize(mBTConnection.OutputStream)
           mPCL3.Initialize(mBTConnection.OutputStream)
         Catch
         Log(LastException.Message)
         End Try
       End If
 
       CallSub2(mBTNotify, mBTNotifyRoutine, mBTConnected)
End Sub

Been a while since I have modified this code

BobVal
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
This is a good place to use the new Wait For keyword:
B4X:
Public Sub DirectPrint(aPrinter As String, aData As String)
    Dim PairedDevices As Map
    Dim LocalBTMac As String
    Dim LocalOSerial As Serial
    Dim LocalPrinter As TextWriter
    PairedDevices = OSerial.GetPairedDevices
    For i = 0 To PairedDevices.Size - 1
        Dim device As String = PairedDevices.GetKeyAt(i)
        If device.Contains(aPrinter) Then
            LocalBTMac = PairedDevices.get(device)
            LocalOSerial.Initialize("DirectPrint")
            LocalOSerial.Connect(LocalBTMac)
            Wait For DirectPrint_Connected (Success As Boolean)
            If Success Then
             LocalPrinter.Initialize(LocalOSerial.OutputStream)
             LocalPrinter.WriteLine(aData)
             LocalPrinter.Flush
             LocalPrinter.Close
             LocalOSerial.Disconnect 
            End If         
        End If
    Next
End Sub
 
Upvote 0

EduardoElias

Active Member
Licensed User
Longtime User
Erel this wait for come in the right moment ! very handy, and it is working.

I am having another problem with bluetooth.

I used this before to comunicate with a device that send data when requested, and I used to open it when the app is loaded and keep open always.

Now I am in need to open and close, since other tablets can have a chance to do the same, I am trying to share bluetooth printers. 2 tablets using 1 printer, for example.

However i am succesful (using the code above) to print to one printer one time only. I have 3 printers paired. Any other attempt to print to any of these printer fail. It fires the _connected event with success = false

How do I know what is going on.

IN theory the tablet that I am using should work. Motorola Xoom. It is old, but very good to work. Android 4.0.4

I am using the b4a 7 final.

1) I need to be able to connect, print and disconnect quickly I can
2) I need to know when the printer is busy and wait
3) and how to know why is failing (error code etc)

Any help appreciated.

Eduardo
 
Upvote 0

EduardoElias

Active Member
Licensed User
Longtime User
Are you closing the connection after you finish printing?

You see in the code that I am calling disconnect

But for some reason it is NOT disconnecting

I have tested with a factory software apk in 3 printers and all work fine, so hardware is not the problem

What is needed to really close the connection?
 
Upvote 0
Top