• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Print to USB or pre-selected network printer from embedded android

My product is an industrial measurement instrument that uses embedded Android. The instrument needs to print results to a pre-selected network printer or to a USB printer. The instrument operator cannot be burdened with the standard Android printer interface, and Cloud printing is not acceptable. I would think this situation is fairly common in products with embedded Android.

I have code which can find the available printers on the network and return the IP address and port numbers, and I can write plain ANSI text to the printer. However, Unicode characters do not print correctly. A few other non-ANSI characters also print (some European letter variants). I believe this is because of the default "symbol set".

My expectation is that I will use PCL or IPP to control the printer. All text starting with "@PCL" is printed as plain text. All text started with <ESC> is not printed, but I don't have any reason to believe that such commands are being processed.

Searching the 'web, I see this question has been asked a few times, but not well answered.

I am wondering whether there is something wrong with my Socket/InputStream/BufferedReader usage.

Socket socket = new Socket(printer.getIpAddr(), 9100);

InputStream inputStream = socket.getInputStream();

DataOutputStream oStream = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));
write(oStream, UEL + "@PJL" + "\r\n");
write(oStream, "@PJL COMMENT some comments" + CRLF);
write(oStream, "@PJL ECHO RRE" + CRLF);
write(oStream, UEL + "\r\n");
oStream.flush();

BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
int count = bufferedInputStream.available();
 
Have you tried converting your String to UTF-8 encoding before transmission?

So you might do something like this:

Code:
String original = new String("A" + "\u00ea" + "\u00f1" + "\u00fc" + "C");
byte[] utf8Bytes = original.getBytes("UTF8");

Then write your utf8Bytes to the socket output stream.

See here:

https://docs.oracle.com/javase/tutorial/i18n/text/string.html
 
Have you tried converting your String to UTF-8 encoding before transmission?

So you might do something like this:

Code:
String original = new String("A" + "\u00ea" + "\u00f1" + "\u00fc" + "C");
byte[] utf8Bytes = original.getBytes("UTF8");

Then write your utf8Bytes to the socket output stream.

See here:

https://docs.oracle.com/javase/tutorial/i18n/text/string.html

Yes, the local write() method does that. (My note to that effect was accidentally left off the post - oops)
 
Back
Top Bottom