How use thermal printer(USB/Ethernet) on android, without using vendor SDK,?
Asked Answered
B

2

7

I already implemented EPSON SDK(for Bluetooth) and working fine, but not Working on other printers, is there is any general way to accomplish it. what is ESC command, How it works?,

Bicycle answered 28/11, 2017 at 5:22 Comment(3)
you can use thermal printer have_lookPhenetole
But it only support Bluetooth right? i need USB or Ethernet ... :(Bicycle
I think u still need vendor SDK, only vendor know the how I2C communication link with their hardware for USB/EthernetMoonrise
S
12

Please find this one.It will help your problem. ESC/POS Command Reference provides detailed information on ESC/POS commands, such as standard command syntax and protocol. It targets programmers who want to control the printer with ESC/POS commands.

ESC/POS Command Reference is provided as replacement of ESC/POS APG for Paper Roll Printers. The ESC/POS APG for Paper Roll Printers, therefore, will not be revised anymore. ESC/POS Command Reference contains the command information for Standard models such as ANK model or Japanese model, and may contain Chinese models or South Asia models. The other models such as customized may support different commands or have a different range, or different default value of command parameters. Please refer to each product specification for them.

Use below code
Note : you can use OutPutStream object to write printer, no matter bluetooth or ethernet or wifi

public class PrinterConstants {
public static int PORT=9100,TOTAL_CHAR=45,DIV1=10,DIV2=5,DIV3=10,DIV4=10,DIV5=10;
public static String IP="192.168.1.35";
private OutputStream printer;
public static final String UUID="00001101-0000-1000-8000-00805f9b34fb";
public PrinterConstants(OutputStream printer){
    this.printer=printer;
}

public void printString(String str) throws IOException {
    Log.i("PRINTER_PRE",str);
    printer.write(str.getBytes());
    printer.write(0xA);
    printer.flush();
}
public void storeString(String str) throws IOException {
    printer.write(str.getBytes());

    printer.flush();
}
public void printStorage() throws IOException {
    printer.write(0xA);

    printer.flush();
}
public void feed(int feed) throws IOException {
    //escInit();
    printer.write(0x1B);
    printer.write("d".getBytes());
    printer.write(feed);printer.flush();

}
public void printAndFeed(String str, int feed) throws IOException {
    //escInit();
    printer.write(str.getBytes());
    printer.write(0x1B);
    printer.write("d".getBytes());
    printer.write(feed);printer.flush();

}
public void setBold(Boolean bool) throws IOException {
    printer.write(0x1B);
    printer.write("E".getBytes());
    printer.write((int)(bool?1:0));printer.flush();
}
/**
 * Sets white on black printing
 * */
public void setInverse(Boolean bool) throws IOException {
    bool=false;
    printer.write(0x1D);
    printer.write("B".getBytes());
    printer.write( (int)(bool?1:0) );printer.flush();

}
public void resetToDefault() throws IOException {
    setInverse(false);
    setBold(false);
    setUnderline(0);
    setJustification(0);printer.flush();
}
/**
 * Sets underline and weight
 *
 * @param val
 *      0 = no underline.
 *      1 = single weight underline.
 *      2 = double weight underline.
 * */
public void setUnderline(int val) throws IOException {
    printer.write(0x1B);
    printer.write("-".getBytes());
    printer.write(val);printer.flush();
}
/**
 * Sets left, center, right justification
 *
 * @param val
 *      0 = left justify.
 *      1 = center justify.
 *      2 = right justify.
 * */

public void setJustification(int val) throws IOException {
    printer.write(0x1B);
    printer.write("a".getBytes());
    printer.write(val);
    printer.flush();
}
public void setLeftRight(String left,String right) throws IOException {
    printer.write(0x1B);
    printer.write("a".getBytes());
    printer.write(0);
    printString(left);

    printer.write(0x1B);
    printer.write("a".getBytes());
    printer.write(2);
    printString(right);

    printer.flush();
}
public void printBarcode(String code, int type, int h, int w, int font, int pos) throws IOException {

    //need to test for errors in length of code
    //also control for input type=0-6

    //GS H = HRI position
    printer.write(0x1D);
    printer.write("H".getBytes());
    printer.write(pos); //0=no print, 1=above, 2=below, 3=above & below

    //GS f = set barcode characters
    printer.write(0x1D);
    printer.write("f".getBytes());
    printer.write(font);

    //GS h = sets barcode height
    printer.write(0x1D);
    printer.write("h".getBytes());
    printer.write(h);

    //GS w = sets barcode width
    printer.write(0x1D);
    printer.write("w".getBytes());
    printer.write(w);//module = 1-6

    //GS k
    printer.write(0x1D); //GS
    printer.write("k".getBytes()); //k
    printer.write(type);//m = barcode type 0-6
    printer.write(code.length()); //length of encoded string
    printer.write(code.getBytes());//d1-dk
    printer.write(0);//print barcode

    printer.flush();
}

public void beep() throws IOException {
    printer.write(0x1B);
    printer.write("(A".getBytes());
    printer.write(4);
    printer.write(0);
    printer.write(48);
    printer.write(55);
    printer.write(3);
    printer.write(15);printer.flush();
}

public void setLineSpacing(int spacing) throws IOException {

    //function ESC 3
    printer.write(0x1B);
    printer.write("3".getBytes());
    printer.write(spacing);

}
public void cut() throws IOException {
    printer.write(0x1D);
    printer.write("V".getBytes());
    printer.write(48);
    printer.write(0);printer.flush();
}
}

by using above you can write ESC/POS commands to output stream directly

Stain answered 28/12, 2017 at 7:51 Comment(3)
This work perfect! do you know how we can print image?? thanksTented
ESC Refer this section 'Bit Image Command'Stain
Is this working with USB Bluetooth Printer?Zackzackariah
B
1

You have find and create an implementation for few different variants of printers, Most are compatible with another so it wont be that hard (plus you will be copying the vendors SDKs).

Then Create an interface which both implementation will use such as initialize, scan, printText, printImage, printBarCode

Read the device like so...

static String getDeviceName() {
        String manufacturer = Build.MANUFACTURER;
        String model = Build.MODEL;
        if (model.startsWith(manufacturer)) {
            return capitalize(model);
        } else {
            return capitalize(manufacturer) + " " + model;
        }
    }

And use the results to determine which implementation to use, before defaulting to the one that works on most devices. An Interface will make you soon forget the trouble you went through.

an ESC command, is just an instruction to the printer, these are almost identical to most devices... they are used to start a new line, align text, bold etc.. Think of them like html markups (strong,h1,center) because you mix them with text you are going to print making it super easy to create cool looking prints.

Bibeau answered 27/12, 2017 at 4:30 Comment(5)
As for credible and/or official sources... last time i checked thermal printing from android was not standardized.. well at least no manufacture follows any standard.Bibeau
Build.MANUFACTURER is return android devices manufacture right? how i can use ESC commands ? how write it to an OutPutStream ?Bicycle
almost every manufactures following ESC standard for their printers, but how can use it?Bicycle
Hi Anu... You write escape string like this new byte[] { 0x1b, 0x57, 0x02 }; on the same buffer you write text. The manufacture may provide yow with hex string such as "0A" which you will to convert to bytes.Bibeau
Manufactures are following ESC standards for an extent... like i said things like new line,center are common but the result might not be the same.. and some printers may lack features or implement them differentlyBibeau

© 2022 - 2024 — McMap. All rights reserved.