Skip to content

codivoire/cordova-plugin-bluetoothprinter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

cordova-plugin-bluetoothprinter

FOSSA Status npm version npm downloads Supports Android Apache 2.0

A cordova plugin for bluetooth printer for android platform.

This code is being adapted from a fork of Cordova-Plugin-Bluetooth-Printer, of free use and modifications that will arise for the improvement of the plugin.

Suggestions, critiques are welcome, participate and send a commit helping to improve the plugin for the community.

Thanks!

Support

Install

Using the Cordova CLI and NPM, run:

cordova plugin add cordova-plugin-bluetoothprinter

Usage

Get list of paired bluetooth devices, including printers, if any:

BluetoothPrinter.list(function (data) {
  console.log("Success");
  console.log(data);
},function (err) {
  console.log("Error");
  console.log(err);
})

Returns an array with the format:

data[0] = Device 1 name
data[1] = Device 1 MAC address
data[2] = Device 1 type
data[3] = Device 2 name
data[4] = Device 2 MAC address
data[5] = Device 2 type

Where device name is the required string to connect to the printer.

Check Bluetooth status

BluetoothPrinter.status(function(data){
  console.log("Success");
  console.log(data)
},function(err){
  console.log("Error");
  console.log(err)
})

Connect printer

BluetoothPrinter.connect("PrinterName", function(data){
  console.log("Success");
  console.log(data)
},function(err){
  console.log("Error");
  console.log(err)
})

Check if printer is connected

BluetoothPrinter.isConnected(function(data){
  console.log("Success");
  console.log(data)
},function(err){
  console.log("Error");
  console.log(err);
})

Disconnect printer

BluetoothPrinter.disconnect("PrinterName", function(data){
  console.log("Success");
  console.log(data)
}, function(err){
  console.log("Error");
  console.log(err)
})

Obs

I thought it best to create the function within a timeout

setTimeout(function(){
  BluetoothPrinter.disconnect("PrinterName", function(data){
    console.log("Success");
    console.log(data)
  },function(err){
    console.log("Error");
    console.log(err)
  });
}, 1500)

Set text encoding

BluetoothPrinter.setEncoding("ISO-8859-1", function(data){
  console.log("Success");
  console.log(data)
},function(err){
  console.log("Error");
  console.log(err)
})

Refer to printer's manual for supported encodings and codepages.

Print simple text

BluetoothPrinter.printText("String to Print", function(data){
  console.log("Success");
  console.log(data)
},function(err){
  console.log("Error");
  console.log(err)
})

Print text with size and align

BluetoothPrinter.printTextSizeAlign("String to Print", 0, 0, function(data){
  console.log("Success");
  console.log(data)
},function(err){
  console.log("Error");
  console.log(err)
})

Print image from path with align

BluetoothPrinter.printImageUrl("Path String", 0, function(data){
  console.log("Success");
  console.log(data);
},function(err){
  console.log("Error");
  console.log(err);
})

In android tests with /storage/emulated/0/Pictures/myfolder/myimage.jpg - size max: 300x300px.

Print image from base64 with align

  • with align still in tests not work alignment)
BluetoothPrinter.printBase64("Image Base64 String", '0', function(data){
  console.log("Success");
  console.log(data);
},function(err){
  console.log("Error");
  console.log(err);
})

Print title with size and align

  • with align still in tests not work alignment)
BluetoothPrinter.printTitle("String text", 0, 0, function(data){
  console.log("Success");
  console.log(data);
},function(err){
  console.log("Error");
  console.log(err);
})

POS printing

BluetoothPrinter.printPOSCommand("0C", function(data){
  console.log("Success");
  console.log(data)
},function(err){
  console.log("Error");
  console.log(err)
})

Print QRCode

var data = "https://github.com/CesarBalzer/Cordova-Plugin-BluetoothPrinter";
var align = 1; /* 0, 1, 2 */
var model = 49; /* https://reference.epson-biz.com/modules/ref_escpos/index.php?content_id=140 */
var size = 32; /* https://reference.epson-biz.com/modules/ref_escpos/index.php?content_id=141 */
var eclevel = 50; /* https://reference.epson-biz.com/modules/ref_escpos/index.php?content_id=142 */

BluetoothPrinter.printQRCode(data, align, model, size, eclevel, function(data){
  console.log("Success");
  console.log(data);
},function(err){
  console.log("Error");
  console.log(err);
})

Print Barcode

var system = 0; /* Barcode system, defined as "m" at https://reference.epson-biz.com/modules/ref_escpos/index.php?content_id=128 */
var data = "012345678901"; /* Barcode data, according to barcode system */
var align = 1; /* 0, 1, 2 */
var position = 2; /* Text position: https://reference.epson-biz.com/modules/ref_escpos/index.php?content_id=125 */;
var font = 0; /* Font for HRI characters: https://reference.epson-biz.com/modules/ref_escpos/index.php?content_id=126 */
var height = 64; /* Set barcode height: https://reference.epson-biz.com/modules/ref_escpos/index.php?content_id=127*/

BluetoothPrinter.printBarcode(system, data, align, position, font, height, function(data) {
  console.log("Success");
  console.log(data);
}, function(err){
  console.log("Error");
  console.log(err);
})

Notice that UPC-A, UPC-E, EAN13 and ITF accepts only:

Numbers 0-9

Sending other characters will return a plugin error with the proper description.

CODE39 accepts:

0 – 9, A – Z, SP, $, %, *, +, -, ., /

CODABAR accepts:

0 – 9, A – D, a – d, $, +, −, ., /, :

Size options

0 = CHAR_SIZE_01 // equivalent 0x1B, 0x21, 0x00
8 = CHAR_SIZE_08 // equivalent 0x1B, 0x21, 0x08
10 = CHAR_SIZE_10 // equivalent 0x1B, 0x21, 0x10
11 = CHAR_SIZE_11 // equivalent 0x1B, 0x21, 0x11
20 = CHAR_SIZE_20 // equivalent 0x1B, 0x21, 0x20
30 = CHAR_SIZE_30 // equivalent 0x1B, 0x21, 0x30
31 = CHAR_SIZE_31 // equivalent 0x1B, 0x21, 0x31
51 = CHAR_SIZE_51 // equivalent 0x1B, 0x21, 0x51
61 = CHAR_SIZE_61 // equivalent 0x1B, 0x21, 0x61

Align options

0 = ESC_ALIGN_LEFT // equivalent 0x1B, 0x61, 0x00
1 = ESC_ALIGN_CENTER // equivalent 0x1B, 0x61, 0x01
2 = ESC_ALIGN_RIGHT // equivalent 0x1B, 0x61, 0x02

Sample receipt

The following sample receipt was printed with the plugin demo app using a generic portable bluetooth printer:

Receipt