Skip to content

Commit

Permalink
CashTray/CashBox/CashDrawer
Browse files Browse the repository at this point in the history
  • Loading branch information
EmHaseeb committed Feb 26, 2021
1 parent 98b5b4e commit 10977f7
Show file tree
Hide file tree
Showing 4 changed files with 166 additions and 45 deletions.
57 changes: 47 additions & 10 deletions app/src/main/java/com/emh/thermalprinter/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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) {

Expand Down
14 changes: 9 additions & 5 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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" />
Expand All @@ -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" />

<Button
android:id="@+id/button_usb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:text="Print by USB !" />
android:text="Print by USB" />

<LinearLayout
android:layout_width="wrap_content"
Expand All @@ -54,7 +54,6 @@
android:layout_height="wrap_content"
android:background="@drawable/outline"
android:digits="0123456789."
android:hint="192.168.18.29"
android:inputType="number"
android:padding="10dp"
android:textColor="@color/white"
Expand All @@ -74,7 +73,6 @@
android:layout_width="80dp"
android:layout_height="wrap_content"
android:background="@drawable/outline"
android:hint="9100"
android:inputType="number"
android:padding="10dp"
android:text="9100"
Expand All @@ -86,6 +84,12 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Print by TCP !" />

<Button
android:id="@+id/openCashBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Open Cash Box" />
</LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand All @@ -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
Expand All @@ -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 {
Expand All @@ -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.
*
Expand All @@ -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);
Expand Down Expand Up @@ -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
*/
Expand Down

0 comments on commit 10977f7

Please sign in to comment.