android - 如何在不使用供应商 SDK 的情况下在 android 上使用热敏打印机(USB/以太网)?

标签 android usb ethernet thermal-printer

我已经实现了 EPSON SDK(用于蓝牙)并且工作正常,但不能在其他打印机上工作,是否有任何通用的方法来完成它。什么是 ESC 命令,它是如何工作的?,

最佳答案

请查找this one.It 将帮助您解决问题。 ESC/POS 命令引用提供了有关 ESC/POS 命令的详细信息,例如标准命令语法和协议(protocol)。 它面向希望使用 ESC/POS 命令控制打印机的程序员。

ESC/POS 命令引用作为纸卷打印机的 ESC/POS APG 的替代品提供。因此,用于纸卷打印机的 ESC/POS APG 将不再修订。 ESC/POS Command Reference 包含标准机型如ANK机型或日 native 型的指令信息,可能包含中国机型或南亚机型。 其他型号如定制可能支持不同的命令或有不同的范围,或不同的命令参数默认值。请引用每个产品规范。

使用下面的代码
注意:你可以使用 OutPutStream 对象来写打印机,无论是蓝牙还是以太网还是 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();
}
}

利用上面的方法可以直接写ESC/POS指令输出流

关于android - 如何在不使用供应商 SDK 的情况下在 android 上使用热敏打印机(USB/以太网)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47524508/

相关文章:

android - Titanium SDK 在 Ubuntu 12.04 上无法识别 Titanium CLI、node、alloy 或 npm

java - 对象不是有效的 JSONArray

Android Studio 3.1 卡在 Configure Build 超过 30 分钟

python - 点亮大容量存储器的 LED

java - 对数据摄取Hadoop感到困惑

android - 迁移到 Androidx 后 SwitchCompat 为白色

android - 从 Android 应用程序控制串行设备(如 USB 打印机)

usb - 用 USB 设备打开电脑

c - IP、TCP 和 VLAN 的结构

c - 如何匹配Ethernet Shield和PC的子网