Cordova bluetooth printer demo

Cordova Bluetooth Printer plugin demo

Demo app for testing the functions from the cordova-plugin-btprinter, which allows to print text, images, barcodes and QR codes to any bluetooth printer from your Cordova app. Source code available in GitHub.

Apache Cordova logo phonegap

Overview

To test the demo app sample code provided here:

  1. Pair your android device with the bluetooth printer and turn bluetooth on
  2. Start the demo app and click status to check if bluetooth is active
  3. Click list to view all paired bluetooth devices
  4. Click connect and wait a few seconds
  5. Click connected to check if the connection is active
  6.  Start testing the provided methods as desired.

Screenshots

Methods

This is a summary of the demo app’s methods. Refer to plugin’s README for details

Status

Checks current bluetooth active status. Return values:

List

Retrieves the list of paired BT devices and populates the Select Printer dropdown box.

Reset

Send ESC @ (hex 1B40) to initialize the bluetooth printer. This command has the same effect as restarting the printer, so your encoding set with setEncoding will be cleared as well.

Line feed

Send LF command (hex A0) to insert a line feed and advance the paper in the printer.

Font demo

Prints a sample of different font types: underline 1-dot, underline 2-dot, bold, font type A, font type B.

Size demo

Prints a sample of different font sizes: 00 normal, 01 reduced, 10 double-height, 20 double-width, 30 double width and height.

Beep x1/x3

Sends a 1B420109 or 1B420309 POS command to beep the printer’s buzzer 1 or 3 times respectively. These codes do not match the EPSON buzzer codes, but they worked on the generic printer I’ve tested. This method is useful to beep the device after the printing process is complete.

Set encoding

Sets the encoding method used by the printer plugin, applied with mmOutputStream.write(msg.getBytes(encoding)); whenever a string is sent to the printer.

Set inverted mode

Sends POS command 1D4201 to enable inverted mode text and 1D4200 to disable it.

Send POS command

Sends an arbitrary ESC/POS command to the bluetooth printer. Refer to your particular printer model for supported commands, most printers should support the official EPSON commands by default.

Remember to send decimal values in hexadecimal, for example, to set the Denmark II character set (10) send it as 0A. The full POS command to set this charset would be 1B520A. You may also send a test print command to check the printer’s supported characters.

Print base 64 image

Tests the printBase64 method to print the provided base-64 encoded image. There is another method to print images by path, but it’s not implemented in this demo app to avoid dealing with the endless caveats of file system plugin.

Print QR Code

Prints a Quick Response code with these options: model, size, error correction level. With my generic printer, all models show the same QR code, but size and alignment can be properly set.

BTPrinter plugin QR code sample

Print Barcode

Prints a barcode with these options: system (format), characters position, font, height. With my cheap portable bluetooth printer, all fonts are the same, and only 2 height settings are applicable. Formats supported:

  • UPC-A (numbers 0-9)
  • UPC-E (numbers 0-9)
  • EAN13 / JAN13 (numbers 0-9)
  • EAN8 / JAN8 (numbers 0-9)
  • CODE39 (supports numbers+text)
  • ITF (does not work in my brandless printer)
  • CODABAR (supports numbers+text)

BTPrinter plugin barcode sample

Ελληνικά Test

Greek language test, works as follows:

  • Calls the BTPrinter.setEncoding function with ISO-8859-7 character set.
  • Sends an ESC + t command (1B74 in hex) followed by the character tables: 11, 14, 15, 38, 47 in hex format, to set the character code table, and prints a sample text with each table. These tables correspond to all Greek code page variations CP851, CP737, ISO-8859-7, CP869 and CP1253 for EPSON printers.
  • By calling the demo app function testCP(cp_table_number) you can test any character table easily, however, remember to set the encoding properly before printing.

This function is quite tricky, and it failed to print properly in my generic printer, even after setting the encoding correctly. However, the printer’s demo ticket does not show non-latin-1 characters properly either, my guess is that the printer has left out these languages to save a few KB of memory (or neither me nor the manufacturer knows the table numbers for other languages). This should work just fine with decent printers, or those aimed for specific non-western-european markets.

greek language cordova bluetooth printer plugin btprinter

Greek language test with printer plugin using generic cheap printer for latin american market

Please send your comments and feedback in this page regarding printing in other languages and charsets with your specific printer model.

Sample ticket created by demo app

Bluetooth thermal printer cordova btplugin sample ticket

Bluetooth thermal printer cordova btplugin sample ticket

The previous ticket shows the thermal printer’s output for different text formats and sizes, a base 64 encoded image sample, and a reversed color text sample.

Cordova BTPrinter plugin usage considerations

  • The plugin does not discover nor pair with devices, therefore the user must pair with the printer using the Android Bluetooth/Connection settings before using the print functions.
  • The plugin will list all paired Bluetooth devices, not just printers. If a single device is found, we could assume it is a bluetooth printer and go ahead with the connection, but if more than 1 device is found a Select Bluetooth Device dialog should be implemented.
  • Do not reconnect to an already connected printer, this will return an error. Use the connected method to check if a printer is already connected and jump directly to printing if this is the case.

Printer used for this demo

The demo app has been tested with a generic bluetooth portable thermal printer with a meaningless model number, as shown here. Device fails to print non-latin-1 characters even in the demo ticket printed by itself, probably because it was specifically aimed to the latin american market.

Portable bluetooth thermal printer

Portable bluetooth thermal printer for latin american market

Download demo app

Feedback

Use the comments section here for demo app-related issues, and the GitHub issues tracker for plugin-related ones.

More plugin demos

Check out the following Cordova plugin demo apps ready for download on your device. The source code is included for compiling to any platform.

Disclaimer

The content in this post is for general information purposes only. The information is provided by the author and/or external sources and while we endeavour to keep the information up to date and correct, we make no representations or warranties of any kind, express or implied, about the completeness, accuracy, reliability, suitability or availability with respect to the website or the information, products, services, or related graphics contained on the post for any purpose. Some of the content and images used in this post may be copyrighted by their respective owners. The use of such materials is intended to be for educational and informational purposes only, and is not intended to infringe on the copyrights of any individuals or entities. If you believe that any content or images used here violate your copyright, please contact us and we will take appropriate measures to remove or attribute the material in question.