Skip to content

add package.json for cordova 6.1.0+ #41

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 14 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ A cordova plugin for bluetooth printer for android platform, which support text
- POS Commands
- Image Printing (todo)
- Barcode Printing (todo)
- QRCode Printing (on work)


##Tested with:
58mm POS-5802LD

##Install
Using the Cordova CLI and NPM, run:
Expand Down Expand Up @@ -85,7 +90,7 @@ BTPrinter.printText(function(data){
Print image

```
BTPrinter.printText(function(data){
BTPrinter.printImage(function(data){
console.log("Success");
console.log(data)
},function(err){
Expand All @@ -94,26 +99,26 @@ BTPrinter.printText(function(data){
}, "Image Base64 String")
```

POS printing

```
BTPrinter.print(function(data){
BTPrinter.printPOSCommand(function(data){
console.log("Success");
console.log(data)
},function(err){
console.log("Error");
console.log(err)
}, "Base64 String of Image")
}, "0C")
//OC is a POS command for page feed
```


POS printing
QRCode printing

```
BTPrinter.printPOSCommand(function(data){
BTPrinter.printQRCode(function(data){
console.log("Success");
console.log(data)
},function(err){
console.log("Error");
console.log(err)
}, "0C")
//OC is a POS command for page feed
```
}, qrData)
38 changes: 38 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"name": "BluetoothPrinter",
"version": "0.0.1-dev",
"description": "A cordova plugin for bluetooth printer for android platform, which support text printing and POS printing.",
"cordova": {
"id": "cordova-plugin-bluetooth-printer",
"platforms": [
"android"
]
},
"repository": {
"type": "git",
"url": "git+https://github.com/srehanuddin/Cordova-Plugin-Bluetooth-Printer.git"
},
"keywords": [
"cordova",
"bluetooth",
"printer",
"pos",
"text",
"barcode",
"image",
"ecosystem:cordova",
"cordova-android"
],
"engines": [
{
"name": "cordova",
"version": ">=3.0.0"
}
],
"author": "Syed Rehanuddin",
"license": "Apache 2.0",
"bugs": {
"url": "https://github.com/srehanuddin/Cordova-Plugin-Bluetooth-Printer/issues"
},
"homepage": "https://github.com/srehanuddin/Cordova-Plugin-Bluetooth-Printer#readme"
}
2 changes: 1 addition & 1 deletion plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<plugin xmlns="http://apache.org/cordova/ns/plugins/1.0"
xmlns:android="http://schemas.android.com/apk/res/android"
id="cordova-plugin-bluetooth-printer"
version="0.0.1-dev">
version="0.0.2-dev">

<name>BluetoothPrinter</name>
<description>A cordova plugin for bluetooth printer for android platform, which support text printing and POS printing.</description>
Expand Down
61 changes: 60 additions & 1 deletion src/android/BluetoothPrinter.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,16 @@ else if (action.equals("printPOSCommand")) {
}
return true;
}
else if (action.equals("printQRCode")) {
try {
String msg = args.getString(0);
printQRCode(callbackContext, msg);
} catch (IOException e) {
Log.e(LOG_TAG, e.getMessage());
e.printStackTrace();
}
return true;
}
return false;
}

Expand Down Expand Up @@ -336,6 +346,55 @@ boolean printPOSCommand(CallbackContext callbackContext, byte[] buffer) throws I
return false;
}

boolean printQRCode(CallbackContext callbackContext, String str) throws IOException {
try {
int nVersion = 0;
int nErrorCorrectionLevel = 3;
int nMagnification = 8;

byte[] bCodeData = null;
try
{
bCodeData = str.getBytes("UTF-8");

}
catch (UnsupportedEncodingException e)
{
String errMsg = e.getMessage();
Log.e(LOG_TAG, errMsg);
e.printStackTrace();
callbackContext.error(errMsg);
}

byte[] command = new byte[bCodeData.length + 7];

command[0] = (byte) 27;
command[1] = (byte) 90;
command[2] = ((byte)nVersion);
command[3] = ((byte)nErrorCorrectionLevel);
command[4] = ((byte)nMagnification);
command[5] = (byte)(bCodeData.length & 0xff);
command[6] = (byte)((bCodeData.length & 0xff00) >> 8);
System.arraycopy(bCodeData, 0, command, 7, bCodeData.length);

//mmOutputStream.write(("Inam").getBytes());
//mmOutputStream.write((((char)0x0A) + "10 Rehan").getBytes());
mmOutputStream.write(command);
//mmOutputStream.write(0x0A);

// tell the user data were sent
Log.d(LOG_TAG, "Data Sent");
callbackContext.success(command);
return true;
} catch (Exception e) {
String errMsg = e.getMessage();
Log.e(LOG_TAG, errMsg);
e.printStackTrace();
callbackContext.error(errMsg);
}
return false;
}

// disconnect bluetooth printer.
boolean disconnectBT(CallbackContext callbackContext) throws IOException {
try {
Expand All @@ -359,7 +418,7 @@ public byte[] getText(String textStr) {
// TODO Auto-generated method stubbyte[] send;
byte[] send=null;
try {
send = textStr.getBytes("GBK");
send = textStr.getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
send = textStr.getBytes();
}
Expand Down
43 changes: 23 additions & 20 deletions www/BluetoothPrinter.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,30 @@
var exec = require('cordova/exec');

var BTPrinter = {
list: function(fnSuccess, fnError){
exec(fnSuccess, fnError, "BluetoothPrinter", "list", []);
},
connect: function(fnSuccess, fnError, name){
exec(fnSuccess, fnError, "BluetoothPrinter", "connect", [name]);
},
disconnect: function(fnSuccess, fnError){
exec(fnSuccess, fnError, "BluetoothPrinter", "disconnect", []);
},
print: function(fnSuccess, fnError, str){
exec(fnSuccess, fnError, "BluetoothPrinter", "print", [str]);
},
printText: function(fnSuccess, fnError, str){
exec(fnSuccess, fnError, "BluetoothPrinter", "printText", [str]);
},
printImage: function(fnSuccess, fnError, str){
exec(fnSuccess, fnError, "BluetoothPrinter", "printImage", [str]);
list: function (fnSuccess, fnError) {
exec(fnSuccess, fnError, "BluetoothPrinter", "list", []);
},
printPOSCommand: function(fnSuccess, fnError, str){
exec(fnSuccess, fnError, "BluetoothPrinter", "printPOSCommand", [str]);
}
connect: function (fnSuccess, fnError, name) {
exec(fnSuccess, fnError, "BluetoothPrinter", "connect", [name]);
},
disconnect: function (fnSuccess, fnError) {
exec(fnSuccess, fnError, "BluetoothPrinter", "disconnect", []);
},
print: function (fnSuccess, fnError, str) {
exec(fnSuccess, fnError, "BluetoothPrinter", "print", [str]);
},
printText: function (fnSuccess, fnError, str) {
exec(fnSuccess, fnError, "BluetoothPrinter", "printText", [str]);
},
printImage: function (fnSuccess, fnError, str) {
exec(fnSuccess, fnError, "BluetoothPrinter", "printImage", [str]);
},
printPOSCommand: function (fnSuccess, fnError, str) {
exec(fnSuccess, fnError, "BluetoothPrinter", "printPOSCommand", [str]);
},
printQRCode: function (fnSuccess, fnError, str) {
exec(fnSuccess, fnError, "BluetoothPrinter", "printQRCode", [str]);
}
};

module.exports = BTPrinter;