B4A Library SD: BT Printer (Bluetooth)

Discussion in 'Additional libraries, classes and official updates' started by Star-Dust, Nov 4, 2017.

  1. Star-Dust

    Star-Dust Expert Licensed User

    (No WRAP, No Java Only B4A)

    BT_Printer library allows you to print text and image to a bluetooth thermal printers
    (Here for B4J, Here for B4i)

    To print the QR CODES with your BT thermal printer you can create an image containing the QR code with the help of the @Erel library (here) or can use library of @Johan Schoeman (here)

    Required: B4A 8.8+ and Serial Library 1.30+

    To try print with the demo version you can only use the FlushAndClose command which lasts 14 days and then disables.
    The full version of the library will be issued to those making a donation (Contact me in private before making a donation). In the donation indicate the library you want and your NickName. Those requesting the full version will receive updates for 3 months.

    Video1.gif upload_2018-9-2_17-24-41.png


    Author: Star-Dust
    Version: 0.15
    • BT_Printer
      Note: Android 6+ add on manifest
      • Events:
        • BluetoothIsDisabled
        • ConnectedToPrint (Success As Boolean)
        • DisconnectToPrint (Mac As String)
        • DiscoveryFinished
        • DiscoveryNewPrinter (PrintedFound As Map, DeviceClass As Int)
        • DiscoveryNoDeviceFound
        • ErrorDiscovery
        • ListPrinterAssociated (ListNameAndMac As Map)
        • SendingError (Mac As String)
        • SendTerminated
      • Fields:
        • Const_AUDIO_VIDEO As Int
        • Const_COMPUTER As Int
        • Const_HEALTH As Int
        • Const_IMAGING As Int
        • Const_MISC As Int
        • Const_NETWORKING As Int
        • Const_PERIPHERAL As Int
        • Const_PHONE As Int
        • Const_TOY As Int
        • Const_UNCATEGORIZED As Int
        • Const_WEARABLE As Int
      • Functions:
        • AddBuffer_ArrayByte (B As Byte()) As String
        • AddBuffer_BarCode (Code As String) As String
        • AddBuffer_Bitmap (Bmp As Bitmap, ShiftLeft As Int) As String
        • AddBuffer_BitmapAlternativeCoding (Bmp As Bitmap) As String
        • AddBuffer_List_Write (list As List) As String
        • AddBuffer_List_WriteLine (list As List) As String
        • AddBuffer_Tab (ArrayTab As Int()) As String
        • AddBuffer_Write (Text As String) As String
        • AddBuffer_WriteLine (Text As String) As String
        • AddPrintDefineImage As String
        • Class_Globals As String
        • ClearBuffer As String
        • Close As String
        • Connected As Boolean
        • DefineImage (Image As Bitmap)
        • flushAllAndClose
        • Initialize (CallBack As Object, EventName As String, EncodingType As String) As String
          Initializes the object. You can add parameters to this method if needed.
        • InitializePrinter As String
        • IsInitialized As Boolean
          Verifica se l'oggetto sia stato inizializzato.
        • Preview As Bitmap
        • ResetPreViewWidth (NewWidth As Int) As String
        • SearchNewPrinter As String
          select --------------------------------------------
        • SelectFromAssociatedPrinter As String
        • SelectFromMac (Mac As String) As String
        • SendBufferToPrinter
    • ESC_POS
      Code module
      Subs in this code module will be accessible from all modules.
      • Fields:
        • BoldOff As String
        • BoldOn As String
        • DoubleOff As String
        • DoubleOn As String
        • FontA_Bold As String
        • FontA_DoubleHight As String
        • FontA_DoubleWide As String
        • FontA_DoubleWideHight As String
        • FontA_Normal As String
        • FontB_Bold As String
        • FontB_DoubleHight As String
        • FontB_DoubleWide As String
        • FontB_DoubleWideHight As String
        • FontB_Normal As String
        • InitializePrinter As String
      • Functions:
        • Process_Globals As String
    • Encoding
      Code module
      Subs in this code module will be accessible from all modules.
      • Fields:
        • ISO8859 As String
        • UTF8 As String
        • Windows1252 As String
      • Functions:
        • Process_Globals As String

    Attached Files:

    Last edited: May 29, 2019
    DonManfred, vvg, fabricio and 18 others like this.
  2. Star-Dust

    Star-Dust Expert Licensed User


    Sub Process_Globals
    'These global variables will be declared once when the application starts.
        'These variables can be accessed from all modules.
        Dim Stampa As BT_Printer
    End Sub

    Sub Globals
    'These global variables will be redeclared each time the activity is created.
        'These variables can only be accessed from this module.
        Private Label1 As Label
    Private ListView1 As ListView
    Private RadioButton1 As RadioButton
    Private RadioButton2 As RadioButton
    Private RadioButton3 As RadioButton
    End Sub

    Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    End Sub

    Sub Activity_Resume

    End Sub

    Sub Activity_Pause (UserClosed As Boolean)

    End Sub

    Sub Button1_Click
    End Sub

    Sub Button2_Click
    End Sub

    Sub Stampa_ListPrinterAssociated (ListNameAndMac As List)
    For Each BT As NameAndMac In ListNameAndMac
    End Sub

    Sub Stampa_DiscoveryNewPrinter (Print As NameAndMac, DeviceClass As Int)
    Log("DevCl: " & DeviceClass)
    End Sub

    Sub Stampa_DiscoveryFinished
    Log("Fine ricerca")
    End Sub

    Sub ListView1_ItemClick (Position As Int, Value As Object)
    End Sub

    Sub Stampa_ConnectedToPrint
    End Sub

    Sub Stampa_Send_Terminated
    End Sub

    Sub Printer
            Stampa.AddBuffer_Writeline(ESC_POS.BoldOn & 
    "Hallo dear" & ESC_POS.DoubleOff)
    "How are you?")
    "Fine thank's")

    ' if AddBuffer_Bitmap don't work

    ' If you wont add a BarCode Image
        End If
        Label1.Text = Stampa.Preview
    End Sub
    The library is equipped with a series of ESC commands, you can use them by listening to ESC_POS.

    For example, if you would like to reset the printer:
    Last edited: Nov 17, 2018
    mendiburen likes this.
  3. NJDude

    NJDude Expert Licensed User

    Just a correction, you should say:
    Star-Dust likes this.
  4. Star-Dust

    Star-Dust Expert Licensed User

    thanks. my english is bad. I use Google translate
  5. imbault

    imbault Well-Known Member Licensed User

    is that an ESC/POS based library?
  6. Star-Dust

    Star-Dust Expert Licensed User

    For the moment I added a few commands. In the future I will put other commands if I can, and if I can also print BITMAP.

    For now, you have to be satisfied with this :p
    MAGAREY likes this.
  7. Erel

    Erel Administrator Staff Member Licensed User

  8. Star-Dust

    Star-Dust Expert Licensed User

    In fact, I found this problem and I did not understand the reason. I followed other examples using TextWriter and TextReader.

    Thanks for the tip. I update the source code.
    Last edited: Nov 18, 2017

    MAGAREY Member Licensed User

    When I try to run the example I get the following error
    *** Service (starter) Create ***
    Service (starter) Start **
    Activity (main) Create, isFirst = true **
    java.lang.NoSuchMethodError: No static method 
    ToastMessageShow(Ljava/lang/String;Z)V in class Lanywheresoftware/b4a/keywords/Common; or its super classes (declaration of 'anywheresoftware.b4a.keywords.Common' appears in /data/app/b4a.example-1/base.apk)
        at b4a.example.bt_printer._initialize(bt_printer.java:424)
        at java.lang.reflect.Method.invoke(Native Method)
        at anywheresoftware.b4a.shell.Shell.runVoidMethod(
        at anywheresoftware.b4a.shell.Shell.raiseEventImpl(
        at anywheresoftware.b4a.shell.Shell.raiseEvent(
        at java.lang.reflect.Method.invoke(Native Method)
        at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:
        at b4a.example.main.afterFirstLayout(main.java:
        at b4a.example.main.access$
        at b4a.example.main$WaitForLayout.run(main.java:
        at android.os.Handler.handleCallback(Handler.java:
        at android.os.Handler.dispatchMessage(Handler.java:
        at android.os.Looper.loop(Looper.java:
        at android.app.ActivityThread.main(ActivityThread.java:
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:
    Activity (main) Resume **
  10. Star-Dust

    Star-Dust Expert Licensed User

    The library is for b4A version 6.50 (or at most 6.80) because I use this version

    Someone did me kindness to compile a version for B4A 7+
    This version modifies a method: Send_Buffer_to_Print becomes Send_Buffer_to_Print_flush

    you can also find an Library for 7+ and example in the attachment

    Update for B4A 8 see post#1
    Last edited: Sep 12, 2018
    scsjc and MAGAREY like this.
  11. Star-Dust

    Star-Dust Expert Licensed User

    You still have to thoroughly test the 2 methods for bitmap printing. They may not work properly.
    Eventually let me know if it works for you

    MAGAREY Member Licensed User

    it is based on ESC/POS?
  13. Star-Dust

    Star-Dust Expert Licensed User

  14. aviario

    aviario Active Member Licensed User

    Hi. first of all, sorry for my english.
    We were testing the example in B4A 7.3 and it prints rare characters instead of the image.

    MAGAREY Member Licensed User

    this also happened to me, check the type of language your printer uses, and change it to esc/pos, my printer was a zebra and has by default the zpl lenguage
  16. Star-Dust

    Star-Dust Expert Licensed User

    At this time, the method for printing images was not finished. It is not included in the description. We will have to wait for an update.
    For the moment the method does not work on all printers.
  17. aviario

    aviario Active Member Licensed User

    I have it set com esc / pos
  18. hears

    hears Active Member Licensed User

  19. Star-Dust

    Star-Dust Expert Licensed User

    Probably at the next update will be included the printing of images, and the QR and USB connection. For the moment the project is stopped because I'm working on other libraries and apps with higher priority

    But if you have good will on this forum find the necessary to develop your own library,
    There are some B4A developers who have already add print/pos with USB to App
  20. Star-Dust

    Star-Dust Expert Licensed User

    Update to version 0.05 at post#1 The AddBuffer_Bitmap function has been tested and is working on most printers. Alternatively you can use AddBuffer_Bitmap2
  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