Using this code:
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 !
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 !