B4A Library SD: BT Printer Bluetooth

Star-Dust

Expert
Licensed User
Longtime User
Some Explanations on the new version
In the previous version of the library I used only FlushAndClose and each print required the connection. This is because the SendBufferToPrinter command in different printers fails with the second sending of data. So if you send data on several occasions you had to reconnect

Now I have created a system that allows you to use SendBufferToPrinter, in case the printer disconnects or goes into error, automatically try again to connect and print for another 2 attempts.

I always suggest waiting a few seconds between the first and the second. The bluetooth printer (and the BT connection) takes a few seconds to synchronize​

New property
Connected
This property is used to know the connection status with the printer​
New Event
DisconnectToPrint (Mac AsString)
This event is raised if a print submission fails and the connection is compromised. But only after trying twice reconnection and printing​
 
Last edited:

Star-Dust

Expert
Licensed User
Longtime User
Updated examples for Android 7 (With RuntimePermissions request )
 

Lello1964

Well-Known Member
Licensed User
Longtime User
i'm using SearchNewPrinter function, but if no printer is found i see only "No device found" toast message, but havn't an Event to know thath.
Can you help me ?
 

fabio borges

Member
Licensed User
Longtime User
I am trying to use the library but it displays the message: Demo library - Sorry not avaliable.
 

Star-Dust

Expert
Licensed User
Longtime User

Star-Dust

Expert
Licensed User
Longtime User
Use the FlushAndClose command to print instead of SendBufferToPrinter. The flushAllAndClose command lasts for 7 days and then is disabled. Send to the printer and close the connection
 

acidorus

Member
Licensed User
Longtime User
Hello .. Good evening..
is there any way I can tell if the printer has finished printing the entire buffer?
because I give the command 'flushAllAndClose'. and it activates the SendTerminated event even without having finished printing the entire buffer
sorry the English. I used google translator.

ex:

Sub bt_priter_functions_ConnectedToPrint
ToastMessageShow(mensagens.content_msg_conect_success_bt, True)
bt_priter_functions.Connected
For Each mp_modalit As Map In Main.lst_print_infos
Dim r As Map = mp_modalit.Get("r")
' Log( r.Get("dados"))
bt_priter_functions.AddBuffer_WriteLine( r.Get("dados"))
bt_priter_functions.SendBufferToPrinter
bt_priter_functions.ClearBuffer
Next
bt_priter_functions.AddBuffer_WriteLine(mutil.Align_miniPrinters(mensagens.system_version & mensagens.update_version,mutil.ALING_CENTER))
bt_priter_functions.SendBufferToPrinter
bt_priter_functions.ClearBuffer
bt_priter_functions.AddBuffer_WriteLine(mutil.Align_miniPrinters(Main.current_date &" "& DateTime.Time(DateTime.Now),mutil.ALING_CENTER))
bt_priter_functions.SendBufferToPrinter
bt_priter_functions.ClearBuffer
bt_priter_functions.AddBuffer_WriteLine(CRLF & CRLF & CRLF)
bt_priter_functions.SendBufferToPrinter
bt_priter_functions.ClearBuffer
' Log(bt_priter_functions)
' bt_priter_functions
bt_priter_functions.flushAllAndClose
End Sub

Sub bt_priter_functions_SendTerminated
bt_priter_functions.SendBufferToPrinter
bt_priter_functions.ClearBuffer
bt_priter_functions.Close
bluetooth_functions.TurnBluetoothOff
' Log(bt_priter_functions.Preview.Height)
pnl_option_send_print_poule.Visible = False
' Activity_Pause (True)
Activity.Finish
StartActivity(Main)
End Sub
 

Star-Dust

Expert
Licensed User
Longtime User
Unfortunately, the printer's bluetooth channel only receives but not answers like the print end.
So the SendTerminate event refers to the completion of the submission.
Otherwise it would be called PrintingTerminated

I'm so sorry
 

acidorus

Member
Licensed User
Longtime User
Good morning ..
when I make several prints in a row, it shows this message in Log "sending message to waiting queue (btconnection_connected)" and does not print

B4X:
Sub Class_Globals
    Private bluetooth_functions   As Toggle
    Private bt_priter_functions      As BT_Printer
    Private retono_print     As Int
    
    Private int_count_time_window As Int = 0
    Private tempo_visualiza_impressao  As Timer
    Private time_finish  As Int
        
    Dim qr As QRcode
    Dim bm As Bitmap
End Sub

Public Sub Initialize(temp_milisecon As Int)
    tempo_visualiza_impressao.Initialize("tempo_visualiza_impressao",10000)
    tempo_visualiza_impressao.Enabled = True
    
    time_finish = temp_milisecon
    int_count_time_window = 0
    retono_print = 0
    bt_priter_functions.Initialize(Me,"bt_priter_functions",Encoding.UTF8)
    bt_priter_functions.Close
    
    Dim ImpPadrao As String
    If File.Exists(File.DirDefaultExternal,"ConfImp.cfg") Then
        ImpPadrao = File.GetText(File.DirDefaultExternal, "ConfImp.cfg") ' default printer mac
        bt_priter_functions.ClearBuffer
        bt_priter_functions.SelectFromMac(ImpPadrao)
        bt_priter_functions.Connected
        qr.Initialize
        bm = qr.drawQRCode(Main.str_url_link)
    End If
End Sub

Sub tempo_visualiza_impressao_Tick
    If  retono_print > 0 Then
        int_count_time_window = int_count_time_window + 1
        Log(int_count_time_window)
        If int_count_time_window > time_finish Then
            bt_priter_functions.flushAllAndClose
        End If
    End If
End Sub

Sub bt_priter_functions_ListPrinterAssociated (ListNameAndMac As List)
End Sub

Sub bt_priter_functions_DiscoveryNewPrinter (Print As NameAndMac)
End Sub

Sub bt_priter_functions_DiscoveryFinished
End Sub

Sub bt_priter_functions_DiscoveryNoDeviceFound
    ToastMessageShow("Device not found",True)
End Sub

Sub bt_priter_functions_ConnectedToPrint
    Dim is_qr_code As Boolean = False
    retono_print = 1
    bt_priter_functions.Connected
    Log(Main.lst_print_infos.Size)
    For Each mp_modalit As Map In Main.lst_print_infos
        Dim r As Map = mp_modalit.Get("r")
        If IsNumber( r.Get("dados")) Then
            Dim int_dados As Int = r.Get("dados")
            If int_dados = -11 Then
                is_qr_code = True
            End If
        Else
            is_qr_code = False
        End If
        
        If is_qr_code = True Then
            bt_priter_functions.AddBuffer_Bitmap(bm,95)
            bt_priter_functions.SendBufferToPrinter
            bt_priter_functions.ClearBuffer
        Else
            bt_priter_functions.AddBuffer_WriteLine( r.Get("dados"))
            bt_priter_functions.SendBufferToPrinter
            bt_priter_functions.ClearBuffer
        End If
    Next
    bt_priter_functions.AddBuffer_WriteLine(mutil.Align_miniPrinters(mensagens.system_version & mensagens.update_version,mutil.ALING_CENTER))
    bt_priter_functions.SendBufferToPrinter
    bt_priter_functions.ClearBuffer
    bt_priter_functions.AddBuffer_WriteLine(CRLF  & CRLF & CRLF)
    bt_priter_functions.SendBufferToPrinter
    bt_priter_functions.ClearBuffer
End Sub

Sub bt_priter_functions_SendTerminated
    bt_priter_functions.SendBufferToPrinter
    bt_priter_functions.ClearBuffer
End Sub
 

Star-Dust

Expert
Licensed User
Longtime User
Why empty the buffer immediately after sending the print?
It is not necessary, it will be emptied automatically, but if you want to do it you have to wait a few seconds (maybe with the command Sleep)

B4X:
bt_priter_functions.SendBufferToPrinter
bt_priter_functions.ClearBuffer
 

acidorus

Member
Licensed User
Longtime User
I did this because of a printer buffer problem. if I send a lot of things sometimes it would not print .. I'll try to use sleep, and see if it solves ..
Thank you for your attention
 

Star-Dust

Expert
Licensed User
Longtime User
Often if they have trouble with the printer buffer, in addition to the fact that it is not possible to know when it has received all the data nor when it has completed printing.

Not having these feedback we usually use pauses and a few seconds to make sure that the buffer has all been transferred now that the print has already been done
 

Star-Dust

Expert
Licensed User
Longtime User
Update rel 0.12
Required Serial library 1.30+

DiscoveryNewPrinter (Print As NameAndMac, DeviceClass As Int)
When it raises the DiscoveryNewprinter event it also identifies the class of the found device.
You also find a list of constants that represent a specific type of device
B4X:
Sub BTPrinter_DiscoveryNewPrinter (Print As NameAndMac, DeviceClass As Int)
    ' Find new printer
    If DeviceClass=Printer.Const_PERIPHERAL Then
        ListView1.AddSingleLine2(Print.Name,Print.Mac)
    End If
End Sub

Those who have purchased the complete library can ask for an update
 
Last edited:

rkmoray

Active Member
Licensed User
Longtime User
Your library is working great, thank you.
I do have 1 more question however.
The receipt I am printing on has a left perforation, and I want to print something vertically (receipt # to be torn off) between the left edge and the perf.
How do I print something vertically?
 

Star-Dust

Expert
Licensed User
Longtime User
Unfortunately, the Bluetooth printer does not provide vertical printing.

The BTs do not work like the Ink-Jet or Laser printers that the text is transformed into graphics and then sent, but it works like the Needles printers, ie the text is sent as UTF-8 (or other text encoding formats)

So the only way to have text vertically and convert the text into a bitmap and send it to the printer, but this will understand how complicated and lengthens transmission and printing times.
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…