Print data on Zebra printer from Android via bluetooth

你说的曾经没有我的故事 提交于 2019-12-23 03:14:10

问题


I use Zebra QL320 plus printer. Fonts was loaded from Win7(sys. encoding CP1251). When I send text from Android via bluetooth to printer in russian lng:

! 0 200 200 200 1
ENCODING UTF-8
TEXT 14 0 20 80 Привет мир
PRINT

I have in result something like this:

Привет мир

How I can fix this?


回答1:


What encoding is Russian in? Are you sending this as a String in Java? You have to form up your string with the right encoding. Try debugging the app and getting the bytes from the string you are sending and make sure the bytes are correct

Check out the Sun encoding stuff here




回答2:


I have solved it using in the OutputStream of BluetoothSocket an encodint to ISO-8859-1 for printing Spanish characters.

      outputStream.write(cpclData.getBytes("ISO-8859-1")); 

Maybe you have to use a special russian ISO charset




回答3:


Here is working example:

public void bluetoothSendData(String text){
    bluetooth_adapter.cancelDiscovery();
    if (socket_connected) {
        try {
            OutputStream o_stream = socket.getOutputStream();               
            o_stream.write(decodeText(text, "CP1251"));
            Log.i("emi", "Data was sended.");
        } catch (IOException e) {
            bluetoothCloseConnection();
            Log.i("emi", "Send data error: " + e);
        }
    } else {
        Log.i("emi", "Bluetooth device not connected.");
    }
}

private byte[] decodeText(String text, String encoding) throws CharacterCodingException, UnsupportedEncodingException{
    Charset charset = Charset.forName(encoding);
    CharsetDecoder decoder = charset.newDecoder();
    CharsetEncoder encoder = charset.newEncoder();
    ByteBuffer bbuf = encoder.encode(CharBuffer.wrap(text));
    CharBuffer cbuf = decoder.decode(bbuf);
    String s = cbuf.toString();
    return s.getBytes(encoding);
}

How I understend, this examle will be work in Fonts what was loaded from OS with encoding CP1251.




回答4:


BServico.write(new byte[] { 28, 46 }); //Cancels Chinese character mode

//TEST
for (int i = 0; i < 20; i++) {
    String text = String.format("%d - %s - çüáéíóúñåæø\n", i, Integer.toHexString(i));

    BServico.write(new byte[] { 0x1B, 0x74, (byte)i });

    try {
        BServico.write(text.getBytes("ISO-8859-1"));
    } catch (Exception ex) {
        //
    }
}

Correct Code Page for me BServico.write(new byte[] { 0x1B, 0x74, 0x10 });



来源:https://stackoverflow.com/questions/9732546/print-data-on-zebra-printer-from-android-via-bluetooth

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!