Search code examples
androidthermal-printer

How to send characeters to the print command and have them printed in one line?


public void printText(String text, int fontsize, boolean doubleW, boolean bold, boolean center) {

    if (this.mPrinterModule != null) {

        byte [] alignment = alignRight();
        byte [] alignment = alignCenter();
        byte [] line_space = setLineSpacing(10);
        byte [] font_size  = fontSizeSetBig(fontsize);
        byte [] left_margin = setLeftMargin(24,0);

        this.mPrinterModule.sendData(left_margin);
        this.mPrinterModule.sendData(font_type);
        this.mPrinterModule.sendData(line_space);
        this.mPrinterModule.sendData(font_size);
        this.mPrinterModule.sendData(alignment);

        PrinterModule printerModule = this.mPrinterModule;
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(text);
        stringBuilder.append("\r\n");
        printerModule.sendMessage(stringBuilder.toString(), "GBK");
    }
}

This is a print method I use to get text printed using my thermal printer. Instead of text I want to send character by character with their own individual style format. All the characters should be printed in one line as well. Any help will be greatly appreciated.Thank you in advance

public void sendMessage(String message, String charset) { 

    super.sendMessage(message, charset); 

    if (checkUsbPermission()) { 

        if (this.mConnected) { 

            this.mUsbCtrl.sendMsg(message, charset, this.mDevice); 

        } else { 

            this.mCallback.onError(ErrorCode.DEVICE_NOT_CONNECTED); 

        } 

    } 

} 



public synchronized void sendMsg(String msg, String charset, UsbDevice dev) { 

    if (msg.length() != 0) { 

        byte[] send; 

        try { 

            send = msg.getBytes(charset); 

        } catch (UnsupportedEncodingException e) { 

            send = msg.getBytes(); 

        } 

        sendByte(send, dev); 

        sendByte(new byte[]{(byte) 13, (byte) 10, (byte) 0}, dev); 
    } 
}

Solution

  • Most of the time, the method sendMessage() of the PrinterModule has methods of print and moveToNextLine implemented within it. Hence, the character is printed and cursor moves to next line immediately after you call the sendMessage() method.

    You will have to make changes in the sendMessage() method implementation of the PrinterModule. Remove the print and newLine calls from sendMessage() method. Then you can send as many characters as you want in outputStream. When you want to finally print, just send the print command to the Printer.

    UPDATE

    The sendMessage() method calls sendMsg() which ultimately execute the statement sendByte(new byte[]{(byte) 13, (byte) 10, (byte) 0}, dev); . This line is responsible for printing after every sendMsg() call. This byteArray contains command for printing and adding newLine everytime some message is sent to it. Remove this line from here.

    After that keeping sending characters to printer using sendMessage(). When you finally want to print the whole statement, then only send the command PrinterModule.sendByte(new byte[]{(byte) 13, (byte) 10, (byte) 0}, dev);. All your characters would be printed in one line if there is enough space, otherwise it be moved to next line.

    UPDATE 2

    Here is the link for the UsbController. You can directly add this as a new file in your project(no need to add the SDK files). The way to use the UsbController class is here in UsbFragment.