Search code examples
androidencodingzebra-printers

Print data on Zebra printer from Android via bluetooth


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?


Solution

  • 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.