How to connect a network printer over Android?

22,705

Solution 1

Any device connected to a network will communicate via their IP and Ports / sockets. The simplest way to connect via telnet or socket and write the data to their socket buffers.

try 
    {
    Socket sock = new Socket("192.168.1.222", 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();
    } 

Solution 2

Just Add This Code After oncreate Method

if (android.os.Build.VERSION.SDK_INT > 9) {
    StrictMode.ThreadPolicy policy = 
        new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);
}

Solution 3

You might be able to use lpdspooler, that is, if the printer supports LPR/LPD. If you can give some more details about the environment (printer, etc), I might be able to give more information.

Share:
22,705
Fallenlight
Author by

Fallenlight

Updated on July 11, 2022

Comments

  • Fallenlight
    Fallenlight almost 2 years

    I want to code an Android app, which will connect to a network printer with a specific IP address, and then make a printing.

    For printing I know that I need to write my own Postscript for specific files types, and connecting to a network is not a problem over WIFI.

    How to connect to the network printer?

  • Fallenlight
    Fallenlight about 13 years
    Test printer is EPSON TM-T88III (Ethernet). I need to check if it supports LPR/LDP.
  • Fallenlight
    Fallenlight about 13 years
    www-304.ibm.com/support/… It shows that printer supports LPR.
  • AC2MO
    AC2MO about 13 years
    I looked into lpdspooler again, and it doesn't look like exactly what you want, but I see your printer does support LPR and RAW. I found this link to the source for a project which sends a job to a printer through LPR in java; I believe it will do exactly what you want.
  • Fallenlight
    Fallenlight about 13 years
    Thanks Gregory, let me try it. I will inform you about the results. Now at least i have something to start with.
  • Umair A.
    Umair A. almost 13 years
    Did you figure out something?
  • Fallenlight
    Fallenlight almost 13 years
    Hi, i tried the link which Gregory provided, it worked as java application without any problem. But whenever i tried the same idea and code in Android, i ended up SocketTimeOut error while writing data file. I tried wakelock and increasing the amount of timeout but it didnt help, i will check with wireshark to see what is wrong, still working on it. It is early to say it but i think it can happen because of permission problems there is nothing i can think of for now, any help is appriciated.
  • Menna-Allah Sami
    Menna-Allah Sami about 9 years
    how to get the port ?
  • Anchal Radhwani
    Anchal Radhwani over 8 years
    Hi guys, Even I am looking for print sdks, is this code is beneficial in android, explain how?
  • Vishwajit Palankar
    Vishwajit Palankar almost 8 years
    it worked for me, i want to print a pdf file, could you help me there
  • Vassilis
    Vassilis almost 7 years
    No, this is the microsoft windows' ed way. In fact, many good quality devices, like printers, modems, sound cards, etc, support standard communication protocols, so you can connect without any drivers installed. See the accepted answer for example.
  • Satyam Gondhale
    Satyam Gondhale almost 5 years
    I have some different Url like Ip, Port like 192.168.100.101:9090/printer/print How to use this Url for Print ?
  • user3219477
    user3219477 almost 5 years
    @luis can you tell me which printer you have used?
  • user3219477
    user3219477 almost 5 years
    @VishwajitPalankar Which printer you have used?
  • Luis
    Luis almost 5 years
    @user3219477 I think it was an HP printer. The important thing for me was that the printer supported PCL (en.m.wikipedia.org/wiki/Printer_Command_Language)
  • Sam Chen
    Sam Chen over 4 years
    Works great! But how can I print in xml format? Please help!