Android Code Snippet Print to a Bluetooth POS-receipt printer with matching codepage

Discussion in 'Code Snippets' started by KMatle, Mar 2, 2017.

  1. KMatle

    KMatle Expert Licensed User

    Original thread: https://www.b4x.com/android/forum/threads/bluetooth-printing-via-spp.17692/page-12

    I simlified the above's example to print on a BT POS printer like this

    Unbenannt.JPG

    with the correct codepage to print special chars like "äöüßéè", etc. (codepage 1252).

    Steps:

    - switch on the printer
    - switch on BT on your device
    - pair your device with the printer (pw on my printer is 1234, take a look at the manual)
    - wait 'till it's paired and see the printer's name
    - insert the first unique chars in the code (the MAC address is retrieved via the printers name)
    - run the app and print

    Code:
    #Region Module Attributes
        
    #FullScreen: False
        
    #IncludeTitle: True
        
    #ApplicationLabel: Bluetooth POS Printing
        
    #VersionCode: 1
        
    #VersionName:
        
    #SupportedOrientations: portrait
        
    #CanInstallToExternalStorage: False
    #End Region

    Sub Process_Globals
        
    Dim PrintBuffer As String

        
    Dim BtAdmin As BluetoothAdmin
        
    Dim BTConnection As Serial
        
    Dim Printer As TextWriter
    End Sub

    Sub Globals
       
    End Sub

    Sub Activity_Create(FirstTime As Boolean)
        
    If FirstTime Then
            BtAdmin.Initialize(
    "BlueTooth")
            BTConnection.Initialize(
    "Printer")
            
    Dim PairedDevices As Map
            PairedDevices = BTConnection.GetPairedDevices
            
    Dim l As List
            
    Dim DeviceName, MacAddress As String
            l.Initialize
            
    For i = 0 To PairedDevices.Size - 1 'Check all devices
                l.Add(PairedDevices.GetKeyAt(i))
                DeviceName=PairedDevices.Getkeyat(i)
                MacAddress=PairedDevices.GetValueAt(i)
                
    Log(DeviceName & " -> " & MacAddress)
                
    If DeviceName.Contains("Thermal"Then 'Insert the BT-Name of the printer or use the MAC address
                    Exit
                
    End If
            
    Next
            BTConnection.Connect(MacAddress)
        
    End If
    End Sub

    Sub Activity_Resume
       
    End Sub

    Sub Activity_Pause
       
    End Sub

    Sub Printer_Connected (Success As Boolean)
        
    If Success Then
            Printer.Initialize2(BTConnection.OutputStream,
    "windows-1252"'important to print f.e. German/French chars
            PrintBuffer=Chr(27)&"t"&Chr(16)&"Hello öäüßéèê" 'Set codepage 1252 
            Printer.WriteLine(PrintBuffer)
            Printer.Flush

            
    Msgbox("Printed!","")
            Printer.Close
            BTConnection.Disconnect 
    'disable this if you like
        Else
            
    Msgbox("No printer found...","Print error")
        
    End If
    End Sub
     
  2. hasexxl1988

    hasexxl1988 Active Member Licensed User

    Nice is helpful, and have your a a solution for Image printing? :D
     
  3. Widget

    Widget Well-Known Member Licensed User

  4. KMatle

    KMatle Expert Licensed User

    See here: https://www.b4x.com/android/forum/threads/send-bitmap-data-to-esc-pos-printer.73599/#post-467662 but I did not try so far.

    What you can do is search Google for a VB(.net) example and port the code. I think there are tons evailable. Maybe I do a convert.

    I added "receipt" to the title :) And yes, the printer (about 50€) has a good quality and it pairs very fast and reliable.
     
    f0raster0 likes this.
  5. Tom Christman

    Tom Christman Active Member Licensed User

    I believe POS is "Point of Sale".
     
  6. Widget

    Widget Well-Known Member Licensed User

    Correct. But when I first read it I read it too quickly and thought it stood for "Piece of S...t Printer". LOL
     
    KMatle likes this.
  7. KMatle

    KMatle Expert Licensed User

  8. HAH

    HAH Active Member Licensed User

    Great for English ..

    Arabic windows-1256 not working!
    Also how to print right to left direction?
    I want using your printing approach not another library.
     
    abolfazlmarj likes this.
  9. scsjc

    scsjc Active Member Licensed User

    Hi,
    i try to convert a code vb to print image... but can't have a solution... my sample code is https://www.b4x.com/android/forum/threads/print-image-on-bluetooth-printers.85345/ and only cant a print pixels on a bucle 0-255, i don't know if that can you help to find a print image ...
    thanks
     
  10. Eugene Puah

    Eugene Puah New Member Licensed User

    Matle, thanks a lot for your POS Printing example which has been a great help for me in picking up BT printing.

    Eugene Puah(Malaysia)
     
    KMatle likes this.
  11. DonManfred

    DonManfred Expert Licensed User

    Please do answer with the default font. It is not needed to change the font. You can use bold for single words to mark them. But writing everything bold and italic only make the post more difficult to read...
     
    KMatle likes this.
  12. Eugene Puah

    Eugene Puah New Member Licensed User

    Don,
    Sorry about the bold fonts. I will pay attention to this next time.
    Eugene Puah
     
Loading...
  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