B4A Library SD: Bluetooth Printer

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)

    My new library. It allows to print text on bluetooth thermal printers.
    If you want to make a donation click on the DONATE button at the bottom of this Post
    Required B4A 7.80+ and this library: ByteConverter,JavaObiect,RandomAccessFile,Serial

    SD_BT_Printer_7

    Author: Star-Dust
    Version: 0.05
    • BT_Printer
      Note: Android 6+ add on manifest
      AddPermission(android.permission.ACCESS_COARSE_LOCATION)
      • Events:
        • ConnectedToPrint
        • DiscoveryFinished
        • DiscoveryNewPrinter (Print As NameAndMac)
        • ListPrinterAssociated (ListNameAndMac As List)
        • PrinterSelected (Mac As String)
        • Send_Terminated
      • Functions:
        • AddBuffer_ArrayByte (B As Byte()) As String
        • AddBuffer_BarCode (Code As String) As String
        • AddBuffer_Bitmap (Path As String, Filename As String, LeftSpace As Int) As String
        • AddBuffer_Bitmap2 (Path As String, Filename As String) As String
        • AddBuffer_List_Write (list As List) As String
        • AddBuffer_List_WriteLine (list As List) As String
        • AddBuffer_Tab As String
        • AddBuffer_Write (Text As String) As String
        • AddBuffer_WriteLine (Text As String) As String
        • AddTab (TabPosition As Int) As String
        • Class_Globals As String
        • ClearBuffer As String
        • ClearTAB As String
        • Close As String
        • flushAllAndClose As String
        • Initialize (CallBack As Object, EventName As String, EncodingType As String) As String
          Initializes the object. You can add parameters to this method if needed.
        • IsInitialized As Boolean
          Verifica se l'oggetto sia stato inizializzato.
        • Preview As String
        • SearchNewPrinter As String
        • SelectFromAssociatedPrinter As String
        • SelectFromMac (Mac As String) 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: Dec 26, 2017
    MarcoRome, f0raster0, scsjc and 3 others like this.
  2. Star-Dust

    Star-Dust Expert Licensed User

    Sample:

    Code:
    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:
        'Activity.LoadLayout("Layout1")
        Activity.LoadLayout("MAIN")
        ListView1.SingleLineLayout.Label.TextSize=
    14
        ListView1.SingleLineLayout.ItemHeight=
    30dip
     
        Stampa.Initialize(Me,
    "Stampa",Encoding.Windows1252)
    End Sub

    Sub Activity_Resume

    End Sub

    Sub Activity_Pause (UserClosed As Boolean)

    End Sub

    Sub Button1_Click
        ListView1.Clear
        Stampa.SelectFromAssociatedPrinter
    End Sub

    Sub Button2_Click
        ListView1.Clear
        Stampa.SearchNewPrinter
    End Sub

    Sub Stampa_ListPrinterAssociated (ListNameAndMac As List)
        ListView1.Clear
        
    For Each BT As NameAndMac In ListNameAndMac
            ListView1.AddSingleLine2(BT.Name,BT.Mac)
        
    Next
    End Sub

    Sub Stampa_DiscoveryNewPrinter (Print As NameAndMac)
        ListView1.AddSingleLine2(Print.Name,Print.Mac)
    End Sub

    Sub Stampa_DiscoveryFinished
        
    Log("Fine ricerca")
    End Sub

    Sub ListView1_ItemClick (Position As Int, Value As Object)
        Stampa.SelectFromMac(Value)
    End Sub

    Sub Stampa_ConnectedToPrint
        Printer
    End Sub

    Sub Stampa_Send_Terminated
        
    Log("Finish!!")
        Stampa.Close
    End Sub

    Sub Printer
            Stampa.AddBuffer_Writeline(ESC_POS.BoldOn & 
    "Hallo dear" & ESC_POS.DoubleOff)
            Stampa.AddBuffer_Writeline(
    "How are you?")
            Stampa.AddBuffer_Writeline(
    "Fine thank's")
            Stampa.AddBuffer_Bitmap(
    File.DirAssets,"qr.jpg",0)
            
    ' if AddBuffer_Bitmap don't work
            'Stampa.AddBuffer_Bitmap2(File.DirAssets,"qr.jpg")
            ' If you wont add a BarCode Image
            'Stampa.AddBuffer_BarCode("123456/A")
        End If
      
        Stampa.flushAllAndClose
        Label1.Text = Stampa.Preview
    End Sub
     
    Last edited: Dec 26, 2017
  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
  9. MAGAREY

    MAGAREY Member Licensed User

    When I try to run the example I get the following error
    Code:
    *** 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(
    Shell.java:755)
        at anywheresoftware.b4a.shell.Shell.raiseEventImpl(
    Shell.java:345)
        at anywheresoftware.b4a.shell.Shell.raiseEvent(
    Shell.java:249)
        at java.lang.reflect.Method.invoke(Native Method)
        at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:
    139)
        at b4a.example.main.afterFirstLayout(main.java:
    102)
        at b4a.example.main.access$
    000(main.java:17)
        at b4a.example.main$WaitForLayout.run(main.java:
    80)
        at android.os.Handler.handleCallback(Handler.java:
    751)
        at android.os.Handler.dispatchMessage(Handler.java:
    95)
        at android.os.Looper.loop(Looper.java:
    154)
        at android.app.ActivityThread.main(ActivityThread.java:
    6186)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:
    889)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:
    779)
    ** 
    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
     

    Attached Files:

    Last edited: Nov 22, 2017
    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
     
  12. MAGAREY

    MAGAREY Member Licensed User

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

    Star-Dust Expert Licensed User

    Yes
     
  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.
     
  15. MAGAREY

    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 Member Licensed User

    HOW TO CHANGE THIS libary to USB PRINTER ?
     
  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
     
Loading...