简体   繁体   中英

Print in Android using a printer with specific IP address

My query is I have a printer connected to the ethernet and I am able to print to that printer using the IP address of the printer but the issue is the printer stops the moment the lines given for printing ends and hence the paper is stuck in the printer.

My Code:

try {
    Socket sock = new Socket("192.168.0.131", 9100);
    PrintWriter oStream = new PrintWriter(sock.getOutputStream());
    oStream.println("HI,test from Android Device");
    oStream.println("\n\n\n");
    oStream.close();
    sock.close();
} catch (UnknownHostException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}

Just add a \\f at the end of the line.

oStream.println("\n\n\n\f");

It's for form feed / new page

I am not able to print with your code after making socket connection with printer.

try {
      Socket sock = new Socket(ipAddress, 9100);
      PrintWriter oStream = new PrintWriter(sock.getOutputStream());
      oStream.println("HI,test from Android Device");
      oStream.println("\n\n\n");
      oStream.close();
      sock.close();
} catch (UnknownHostException e) {
           e.printStackTrace();
} catch (IOException e) {
           e.printStackTrace();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM