diff --git a/app/src/main/java/com/emh/thermalprinter/MainActivity.java b/app/src/main/java/com/emh/thermalprinter/MainActivity.java
index ccb905f..77e4994 100644
--- a/app/src/main/java/com/emh/thermalprinter/MainActivity.java
+++ b/app/src/main/java/com/emh/thermalprinter/MainActivity.java
@@ -44,7 +44,7 @@ public class MainActivity extends AppCompatActivity {
Activity activity = MainActivity.this;
- Button bt, tcp, usb;
+ Button bt, tcp, usb, cashBox;
EditText ip, port;
@Override
@@ -57,7 +57,22 @@ protected void onCreate(Bundle savedInstanceState) {
usb = findViewById(R.id.button_usb);
ip = findViewById(R.id.edittext_tcp_ip);
port = findViewById(R.id.edittext_tcp_port);
+ cashBox = findViewById(R.id.openCashBox);
+ cashBox.setOnClickListener(new View.OnClickListener() {
+ @Override
+ public void onClick(View v) {
+
+ if (ip.getText().toString().length() > 0 && port.getText().toString().length() > 0) {
+
+ openCashBox(ip.getText().toString(), Integer.parseInt(port.getText().toString()));
+
+ } else {
+ Toast.makeText(activity, "IP or Port is In-Correct!", Toast.LENGTH_LONG).show();
+ }
+
+ }
+ });
bt.setOnClickListener(new View.OnClickListener() {
@Override
@@ -89,9 +104,30 @@ public void onClick(View view) {
}
- /*==============================================================================================
- ======================================BLUETOOTH PART============================================
- ==============================================================================================*/
+ /**
+ * Open Cash Box Only Part
+ */
+
+
+ public void openCashBox(String ip, int port) {
+
+ new Thread(new Runnable() {
+ public void run() {
+ try {
+ EscPosPrinter printer = new EscPosPrinter(new TcpConnection(ip, port), 203, 65f, 42);
+ printer.openCashBox();
+ printer.disconnectPrinter();
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+ }).start();
+
+ }
+
+ /**
+ * BLUETOOTH PART
+ */
public static final int PERMISSION_BLUETOOTH = 1;
@@ -166,9 +202,9 @@ public void onRequestPermissionsResult(int requestCode, @NonNull String[] permis
}
- /*==============================================================================================
- ===========================================USB PART=============================================
- ==============================================================================================*/
+ /**
+ * USB PART
+ */
private static final String ACTION_USB_PERMISSION = "com.android.example.USB_PERMISSION";
private final BroadcastReceiver usbReceiver = new BroadcastReceiver() {
@@ -263,9 +299,10 @@ public void printIt(UsbManager usbManager, UsbDevice usbDevice) {
}
- /*==============================================================================================
- =========================ESC/POS - Wifi/Ethernet/Network PRINTER PART===========================
- ================================================================================================*/
+ /**
+ * ESC/POS - Wifi/Ethernet/Network PRINTER PART
+ */
+
public void printTcp(String ip, int port) {
diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml
index 22092fd..72f420e 100644
--- a/app/src/main/res/layout/activity_main.xml
+++ b/app/src/main/res/layout/activity_main.xml
@@ -15,7 +15,7 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
- android:text="Click to print !"
+ android:text="Click to print"
android:textColor="@color/white"
android:textSize="25sp"
android:textStyle="bold" />
@@ -25,14 +25,14 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
- android:text="Print by bluetooth !" />
+ android:text="Print by bluetooth" />
+ android:text="Print by USB" />
+
+
\ No newline at end of file
diff --git a/thermalPrinter/src/main/java/com/emh/thermalprinter/EscPosPrinter.java b/thermalPrinter/src/main/java/com/emh/thermalprinter/EscPosPrinter.java
index fa40cef..81c5c40 100644
--- a/thermalPrinter/src/main/java/com/emh/thermalprinter/EscPosPrinter.java
+++ b/thermalPrinter/src/main/java/com/emh/thermalprinter/EscPosPrinter.java
@@ -164,7 +164,48 @@ public EscPosPrinter printFormattedTextAndCut(String text, int dotsFeedPaper) th
this.printFormattedText(text, dotsFeedPaper);
this.printer.cutPaper();
+ return this;
+ }
+
+ /**
+ * Print a formatted text, cut the paper and open the cash box. Read the README.md for more information about text formatting options.
+ *
+ * @param text Formatted text to be printed.
+ * @param mmFeedPaper millimeter distance feed paper at the end.
+ * @return Fluent interface
+ */
+ public EscPosPrinter printFormattedTextAndOpenCashBox(String text, float mmFeedPaper) throws EscPosConnectionException, EscPosParserException, EscPosEncodingException, EscPosBarcodeException {
+ return this.printFormattedTextAndOpenCashBox(text, this.mmToPx(mmFeedPaper));
+ }
+
+ /**
+ * Print a formatted text, cut the paper and open the cash box. Read the README.md for more information about text formatting options.
+ *
+ * @param text Formatted text to be printed.
+ * @param dotsFeedPaper distance feed paper at the end.
+ * @return Fluent interface
+ */
+ public EscPosPrinter printFormattedTextAndOpenCashBox(String text, int dotsFeedPaper) throws EscPosConnectionException, EscPosParserException, EscPosEncodingException, EscPosBarcodeException {
+ if (this.printer == null || this.printerNbrCharactersPerLine == 0) {
+ return this;
+ }
+
+ this.printFormattedTextAndCut(text, dotsFeedPaper);
+ this.printer.openCashBox();
+ return this;
+ }
+
+ /**
+ * open the cash box. Read the README.md for more information.
+ *
+ * @return Fluent interface
+ */
+ public EscPosPrinter openCashBox() throws EscPosConnectionException {
+ if (this.printer == null) {
+ return this;
+ }
+ this.printer.openCashBox();
return this;
}
diff --git a/thermalPrinter/src/main/java/com/emh/thermalprinter/EscPosPrinterCommands.java b/thermalPrinter/src/main/java/com/emh/thermalprinter/EscPosPrinterCommands.java
index d071c6a..519e553 100644
--- a/thermalPrinter/src/main/java/com/emh/thermalprinter/EscPosPrinterCommands.java
+++ b/thermalPrinter/src/main/java/com/emh/thermalprinter/EscPosPrinterCommands.java
@@ -3,6 +3,7 @@
import android.graphics.Bitmap;
import java.io.UnsupportedEncodingException;
+import java.util.Arrays;
import java.util.EnumMap;
import com.emh.thermalprinter.barcode.Barcode;
@@ -277,9 +278,9 @@ public EscPosPrinterCommands printText(String text, byte[] textSize) throws EscP
/**
* Print text with the connected printer.
*
- * @param text Text to be printed
- * @param textSize Set the text size. Use EscPosPrinterCommands.TEXT_SIZE_... constants
- * @param textColor Set the text color. Use EscPosPrinterCommands.TEXT_COLOR_... constants
+ * @param text Text to be printed
+ * @param textSize Set the text size. Use EscPosPrinterCommands.TEXT_SIZE_... constants
+ * @param textColor Set the text color. Use EscPosPrinterCommands.TEXT_COLOR_... constants
* @return Fluent interface
*/
public EscPosPrinterCommands printText(String text, byte[] textSize, byte[] textColor) throws EscPosEncodingException {
@@ -289,8 +290,8 @@ public EscPosPrinterCommands printText(String text, byte[] textSize, byte[] text
/**
* Print text with the connected printer.
*
- * @param text Text to be printed
- * @param textSize Set the text size. Use EscPosPrinterCommands.TEXT_SIZE_... constants
+ * @param text Text to be printed
+ * @param textSize Set the text size. Use EscPosPrinterCommands.TEXT_SIZE_... constants
* @param textColor Set the text color. Use EscPosPrinterCommands.TEXT_COLOR_... constants
* @param textReverseColor Set the background and text color. Use EscPosPrinterCommands.TEXT_COLOR_REVERSE_... constants
* @return Fluent interface
@@ -302,11 +303,11 @@ public EscPosPrinterCommands printText(String text, byte[] textSize, byte[] text
/**
* Print text with the connected printer.
*
- * @param text Text to be printed
- * @param textSize Set the text size. Use EscPosPrinterCommands.TEXT_SIZE_... constants
+ * @param text Text to be printed
+ * @param textSize Set the text size. Use EscPosPrinterCommands.TEXT_SIZE_... constants
* @param textColor Set the text color. Use EscPosPrinterCommands.TEXT_COLOR_... constants
* @param textReverseColor Set the background and text color. Use EscPosPrinterCommands.TEXT_COLOR_REVERSE_... constants
- * @param textBold Set the text weight. Use EscPosPrinterCommands.TEXT_WEIGHT_... constants
+ * @param textBold Set the text weight. Use EscPosPrinterCommands.TEXT_WEIGHT_... constants
* @return Fluent interface
*/
public EscPosPrinterCommands printText(String text, byte[] textSize, byte[] textColor, byte[] textReverseColor, byte[] textBold) throws EscPosEncodingException {
@@ -316,18 +317,26 @@ public EscPosPrinterCommands printText(String text, byte[] textSize, byte[] text
/**
* Print text with the connected printer.
*
- * @param text Text to be printed
- * @param textSize Set the text size. Use EscPosPrinterCommands.TEXT_SIZE_... constants
+ * @param text Text to be printed
+ * @param textSize Set the text size. Use EscPosPrinterCommands.TEXT_SIZE_... constants
* @param textColor Set the text color. Use EscPosPrinterCommands.TEXT_COLOR_... constants
* @param textReverseColor Set the background and text color. Use EscPosPrinterCommands.TEXT_COLOR_REVERSE_... constants
- * @param textBold Set the text weight. Use EscPosPrinterCommands.TEXT_WEIGHT_... constants
- * @param textUnderline Set the underlining of the text. Use EscPosPrinterCommands.TEXT_UNDERLINE_... constants
+ * @param textBold Set the text weight. Use EscPosPrinterCommands.TEXT_WEIGHT_... constants
+ * @param textUnderline Set the underlining of the text. Use EscPosPrinterCommands.TEXT_UNDERLINE_... constants
* @return Fluent interface
*/
public EscPosPrinterCommands printText(String text, byte[] textSize, byte[] textColor, byte[] textReverseColor, byte[] textBold, byte[] textUnderline) throws EscPosEncodingException {
return this.printText(text, textSize, textColor, textReverseColor, textBold, textUnderline, null);
}
+
+ private byte[] currentTextSize = new byte[0];
+ private byte[] currentTextColor = new byte[0];
+ private byte[] currentTextReverseColor = new byte[0];
+ private byte[] currentTextBold = new byte[0];
+ private byte[] currentTextUnderline = new byte[0];
+ private byte[] currentTextDoubleStrike = new byte[0];
+
/**
* Print text with the connected printer.
*
@@ -345,45 +354,59 @@ public EscPosPrinterCommands printText(String text, byte[] textSize, byte[] text
return this;
}
+ if (textSize == null) {
+ textSize = EscPosPrinterCommands.TEXT_SIZE_NORMAL;
+ }
+ if (textColor == null) {
+ textColor = EscPosPrinterCommands.TEXT_COLOR_BLACK;
+ }
+ if (textReverseColor == null) {
+ textReverseColor = EscPosPrinterCommands.TEXT_COLOR_REVERSE_OFF;
+ }
+ if (textBold == null) {
+ textBold = EscPosPrinterCommands.TEXT_WEIGHT_NORMAL;
+ }
+ if (textUnderline == null) {
+ textUnderline = EscPosPrinterCommands.TEXT_UNDERLINE_OFF;
+ }
+ if (textDoubleStrike == null) {
+ textDoubleStrike = EscPosPrinterCommands.TEXT_DOUBLE_STRIKE_OFF;
+ }
+
try {
byte[] textBytes = text.getBytes(this.charsetEncoding.getName());
this.printerConnection.write(this.charsetEncoding.getCommand());
//this.printerConnection.write(EscPosPrinterCommands.TEXT_FONT_A);
- if (textSize != null) {
+
+ if (!Arrays.equals(this.currentTextSize, textSize)) {
this.printerConnection.write(textSize);
- } else {
- this.printerConnection.write(EscPosPrinterCommands.TEXT_SIZE_NORMAL);
+ this.currentTextSize = textSize;
}
- if (textDoubleStrike != null) {
+ if (!Arrays.equals(this.currentTextDoubleStrike, textDoubleStrike)) {
this.printerConnection.write(textDoubleStrike);
- } else {
- this.printerConnection.write(EscPosPrinterCommands.TEXT_DOUBLE_STRIKE_OFF);
+ this.currentTextDoubleStrike = textDoubleStrike;
}
- if (textUnderline != null) {
+ if (!Arrays.equals(this.currentTextUnderline, textUnderline)) {
this.printerConnection.write(textUnderline);
- } else {
- this.printerConnection.write(EscPosPrinterCommands.TEXT_UNDERLINE_OFF);
+ this.currentTextUnderline = textUnderline;
}
- if (textBold != null) {
+ if (!Arrays.equals(this.currentTextBold, textBold)) {
this.printerConnection.write(textBold);
- } else {
- this.printerConnection.write(EscPosPrinterCommands.TEXT_WEIGHT_NORMAL);
+ this.currentTextBold = textBold;
}
- if (textColor != null) {
+ if (!Arrays.equals(this.currentTextColor, textColor)) {
this.printerConnection.write(textColor);
- } else {
- this.printerConnection.write(EscPosPrinterCommands.TEXT_COLOR_BLACK);
+ this.currentTextColor = textColor;
}
- if (textReverseColor != null) {
+ if (!Arrays.equals(this.currentTextReverseColor, textReverseColor)) {
this.printerConnection.write(textReverseColor);
- } else {
- this.printerConnection.write(EscPosPrinterCommands.TEXT_COLOR_REVERSE_OFF);
+ this.currentTextReverseColor = textReverseColor;
}
this.printerConnection.write(textBytes);
@@ -550,6 +573,22 @@ public EscPosPrinterCommands cutPaper() throws EscPosConnectionException {
return this;
}
+ /**
+ * Open the cash box
+ *
+ * @return Fluent interface
+ */
+ public EscPosPrinterCommands openCashBox() throws EscPosConnectionException {
+ if (!this.printerConnection.isConnected()) {
+ return this;
+ }
+
+ this.printerConnection.write(new byte[]{0x1B, 0x70, 0x00, 0x3C, (byte) 0xFF});
+// this.printerConnection.write(new byte[]{0x1B, 0x70, 0x0, 0x64, (byte) 0xFA});
+ this.printerConnection.send(100);
+ return this;
+ }
+
/**
* @return Charset encoding
*/