CesarBalzer / Cordova-Plugin-BTPrinter

A cordova plugin for bluetooth printer for android platform
Apache License 2.0
81 stars 64 forks source link

How to enable the CHINESE CHARACTER ? #17

Open qaoo8 opened 4 years ago

qaoo8 commented 4 years ago

I have a project need to print the Chinese Character, according to your source file below, could you let us know how to enable it? Thank you!

boolean printText(CallbackContext callbackContext, String msg) throws IOException {

    try {
        // CANCEL CHINESE CHARACTER
        // mmOutputStream.write(0x1C);
        // mmOutputStream.write(0x2E);
        //
        // mmOutputStream.write(0x1B);
        // mmOutputStream.write(0x74);
        // mmOutputStream.write(0x10);
        // -------------------------
        // Select character code table (ESC t n) - n = 16(0x10) for WPC1252
        mmOutputStream.write(msg.getBytes());
        // mmOutputStream.write(msg.getBytes("iso-8859-1"));
        // tell the user data were sent
        Log.d(LOG_TAG, "PRINT TEXT SEND -> " + msg);
        callbackContext.success("PRINT TEXT SEND");
        return true;

    } catch (Exception e) {
        String errMsg = e.getMessage();
        Log.e(LOG_TAG, errMsg);
        e.printStackTrace();
        callbackContext.error(errMsg);
    }
    return false;
}
andreszs commented 4 years ago

To print in Chinese, try this:

First set the text encoding to GB2312 or the corresponding chinese encoding supported by your printer:

var strEncoding = "GB2312"; /* official character set of the People's Republic of China */
BTPrinter.setEncoding(function (data) {
    console.log(data);
}, function (err) {
    console.error(err);
}, strEncoding);

Second, send an ESC t command to set the character code table according your printer:

var hexCharCodePOS = "1B74??"; /* where ?? is the character code table in HEX */
BTPrinter.printPOSCommand(function (data) {
    console.log("Success");
}, function (err) {
    console.error(err);
}, hexCharCodePOS);

Refer to your printer's character code tables for the Chinese code. EPSON codes can be found here. Remember to set the character code table in HEX, so to select Page 47 [WPC1253: Greek] you should send 47 in hexadecimal:

hexCharCodePOS = "1B742F"; /* (ESC t in HEX => 1B74) + (WCP1253 => EPSON page 47 => 2F in HEX) */

Try the BTPrinter Plugin Demo app to test your printer with any language.