2
votes

I am building an app that prints out a receipt using a bluetooth thermal printer.I am able to connect and print using the printer but I can't figure what all these ESC/POS commands mean.

The printer prints my text white on a black background and I actually want the text to be black and background white. I am not sure how to achieve this type of formatting using the ESC/POS commands.

Here is my printing code:

if (btsocket == null) {
            Intent BTIntent = new Intent(getApplicationContext(), DeviceList.class);
            this.startActivityForResult(BTIntent, DeviceList.REQUEST_CONNECT_BT);
        } else {
            OutputStream opstream = null;
            try {
                opstream = btsocket.getOutputStream();
            } catch (IOException e) {
                e.printStackTrace();
            }
            outputStream = opstream;

            //print command
            try {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                outputStream = btsocket.getOutputStream();

                byte[] format = { 27, 33, 0 };
                byte[] printformat = {0x1B, 0 * 21, FONT_TYPE};
                outputStream.write(format);

                //print title
                printUnicode();
                //print normal text
                outputStream.write(format);
                printCustom(message, 0, 0);
                //printPhoto(R.drawable.img);
                printNewLine();
                outputStream.write(format);
                printText("     >>>>   Thank you  <<<<     "); // total 32 char in a single line
                //resetPrint(); //reset printer
                //printUnicode();
                printNewLine();
                printNewLine();

                outputStream.flush();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

The first line printUnicode(); actually prints just fine, black characters on a white background, but the rest of the paper gets printed on black background with white characters. Is there a document out there explaining all the ESC/POS commands?

1
No experience with your API, but printUnicode may actually write a BOM character U+FEFF whose bytes may constitute a white-on-black control command; comment it out.Joop Eggen
The printer language should be documented in the printer manual which accompanied the printer initially. Otherwise it is probably available online from the manufacturer. You can then look up the documentation for the printer language.Thorbjørn Ravn Andersen

1 Answers

2
votes

You can check those pages for the ESC/POS command, works great for me:

https://github.com/escpos/escpos/blob/master/lib/escpos.rb

https://github.com/stefanosbou/esc-pos-java/blob/master/src/main/java/io/github/escposjava/print/Commands.java


Also my piece of code, hope helps:

public class MainActivity extends AppCompatActivity {
Button printButton;

final byte[] ALIGN_CENTER = {0x1b, 0x61, 0x01};
final byte[] ALIGN_LEFT = {0x1b, 0x61, 0x00};
final byte[] ALIGN_RIGHT = {0x1b, 0x61, 0x02};
final byte[] TEXT_SIZE_NORMAL = {0x1b, 0x21, 0x00};
final byte[] TEXT_SIZE_LARGE = {0x1b, 0x21, 0x30};
final byte[] INVERTED_COLOR_ON = {0x1d, 0x42, 0x01};
final byte[] BEEPER = {0x1b,0x42,0x05,0x05};
final byte[] INIT = {0x1b, 0x40};
//final byte[] CUT_PAPER = {0x1d, 0x56, 0x00};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    printButton = findViewById(R.id.main_print_button);

    printButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            new PrintTask().execute();
        }
    });
}

private class PrintTask extends AsyncTask<Void, Void, Void> {
    @Override
    protected Void doInBackground(Void... voids) {
        try {
            String text1 = "No setting size text" + "\n\n";
            String text2 = "Inverted color text" + "\n\n";
            String text3 = "Large size text" + "\n\n\n";

            Socket socket = new Socket("192.168.1.241", 9100);              //one socket responsible for one device
            OutputStream outputStream = socket.getOutputStream();

            outputStream.write(text1.getBytes("GBK"));                      //when printing text, "write()" will print before "println()"

            outputStream.write(INVERTED_COLOR_ON);

            outputStream.write(text2.getBytes("GBK"));

            outputStream.write(new byte[]{0x1D, 0x56, 0x41, 0x10});                    //"0x1d, 0x56, 0x41" is for paper cut and "0x10" is for line feed

            //outputStream.write(BEEPER);                                               //hardware turn on

            outputStream.write(INIT);

            outputStream.close();

            socket.close();
        } catch (UnknownHostException e) {
            Log.e("Print()", "UnknownHostException");
        } catch (IOException e) {
            Log.e("Print()", "IOException");
        }

        return null;
    }
}