In this blog we will learn how to integrate EPSON POS Printer (Model starting with TM )with your android application.
Integrating this is not at all difficult, if you follow the steps correctly.
First, download the Epson sdk for android. (You can download the sdk from here).
Now from this package, extract the ePOS2.jar file and paste it in your libs folder.
Add this line in your build.gradle file.
1 |
compile files('libs/ePOS2.jar') |
Now all you have to do is make an object of the Printer and set the content that you want to print.
Lets have a look on how to do that.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
private Printer mPrinter; // Declare a global instance of Printer mPrinter = new Printer("REPLACE THIS STRING WITH YOUR PRINTER SERIES", "REPLACE THIS WITH YOUR PRINTER LANGUAGE", this);// Initialize the printer /*Add a listener to your printer*/ mPrinter.setReceiveEventListener(new ReceiveListener() { @Override public void onPtrReceive(Printer printer, int code, PrinterStatusInfo printerStatusInfo, String s) { if (code == Epos2CallbackCode.CODE_SUCCESS){ createMyData(); printMyData(); }else{ Toast.makeText(mContext, "Printer can't be connected. Please try again", Toast.LENGTH_SHORT).show(); } } }); |
createMyData()
1 2 3 4 5 6 7 8 9 10 11 12 13 |
private void CreateMyData() throws Epos2Exception { mPrinter.addTextAlign(Printer.ALIGN_CENTER);// by default the alignment is left. mPrinter.addFeedLine(1);// Calling this API causes the printer positioned at "the beginning of the line." StringBuilder builder = new StringBuilder(); builder.append("test content "); builder.append("\n"); builder.append("test content "); builder.append("\n"); mPrinter.addText(builder.toString()); /*Adds a sheet cut command "CUT THE SHEET" to the command buffer.*/ mPrinter.addCut(Printer.CUT_FEED); } |
printMyData()
1 2 3 4 5 6 |
private void printMyData() throws Epos2Exception { mPrinter.connect("REPLACE_THIS_STRING_WITH_THE_PORT_CONNECTION", Printer.PARAM_DEFAULT); mPrinter.beginTransaction(); mPrinter.sendData(Printer.PARAM_DEFAULT); } |
Please note that in printMyData(), you need to replace the first input with the target i.e you need to set the path of the connection or more specifically the port address of your printer.
That’s all
You can connect the printer and test it yourself.