After working for last 7 days i got the right way to get the print and then cut that receipt by the cutter. ESC commands are very important to get the print and other tasks related to the printer. We have to pass those ESC CMD in form of the byte array to the Line Printer.
There are number of commands like below
ESC FF DataPrint,
[code] <1B>H<OC>H,
[Function] Print all the data in the print area collectively.
So just simple create a byte array of this command and pass it to the printer.
e.g. byte[] print = {0x1b,0x0c};
now pass it to the printer. There must be some method like getCmd(), or writeCmd() etc. depending upon the printer.
How can we execute these ESC commands in android by programming ?? Below is the code to do that
BuildinEx840 lpd=new BuildinEx840();
lpd.setMulticharMode(LinePrinterDeviceBase.CHARACTERSET_USA);
//initialise Cutter
lpd.initCutter();
LinePrinter lp=new LinePrinter();
lp.open(lpd);
lpd.open();
try{
lpd.init();
}catch(IOException e){
e.printStackTrace();
}
for(int i=0; i<5;i++){
lp.printNormal("Testing the Line Printer");
}
int totalLinefeed=listofItemList.size();
//ESC CMD for line feeds
byte[] lfs=new byte[]{0x1B,'d', 5};
sendtoExprinter(lpd,lfs);
//ESC CMD for paper cut
lfs=new byte[]{0x1B,'i'};
sendtoExprinter(lpd, lfs);
private void sendtoExprinter(BuildinEx840 dev, byte[] instr) {
try{
dev.write(instr);
}catch(IOException e){
e.printStackTrace();
}
}
UPDATE: Printing an image through Thermal Printer
In some of the printer you can define the image in non-volatile memory of the printer and then print the image from there through ESC cmd 1C 70 01 30. In some of the other printers you can give a direct path of the image while printing an image through thermal printer.
In any line printer we have to pass the ESC CMD by this printerObj.write(command); way. I want to Thanks to SO community and special thanks to TheBlastOne who guided me to the right way.
If someone having any problem in integrating thermal printers feel free to ask.