14

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?

Adel Khayata
  • 2,717
  • 10
  • 28
  • 46
Fallenlight
  • 141
  • 1
  • 1
  • 3

7 Answers7

20

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();
    } 
ReachmeDroid
  • 959
  • 1
  • 14
  • 17
1

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);
}
GiftZwergrapper
  • 2,602
  • 2
  • 20
  • 40
1

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.

AC2MO
  • 1,627
  • 13
  • 15
  • Test printer is EPSON TM-T88III (Ethernet). I need to check if it supports LPR/LDP. – Fallenlight Jun 10 '11 at 15:15
  • https://www-304.ibm.com/support/docview.wss?uid=nas1aeb8bc2140732fb2862569c10078efbb&wv=1 It shows that printer supports LPR. – Fallenlight Jun 10 '11 at 15:19
  • 1
    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](http://code.google.com/p/jprinterface/source/browse/trunk/src/sos/net/print/LPR.java?r=2) 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. – AC2MO Jun 10 '11 at 15:22
  • Thanks Gregory, let me try it. I will inform you about the results. Now at least i have something to start with. – Fallenlight Jun 10 '11 at 15:42
  • Did you figure out something? – Umair A. Jul 15 '11 at 14:28
  • 1
    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. – Fallenlight Aug 29 '11 at 17:23
  • Hi guys, Even I am looking for print sdks, is this code is beneficial in android, explain how? – Anchal Radhwani Dec 30 '15 at 05:42
0

Star has an Android SDK which has port discovery. It'll find any of their wifi receipt printers on your network. http://starmicronics.com/support/SDKDocumentation.aspx

LtH
  • 692
  • 7
  • 18
0

Try to use PrintManager: https://developer.android.com/training/printing/custom-docs

  private void doPrint() {
    // Get a PrintManager instance
    PrintManager printManager = (PrintManager) getActivity()
            .getSystemService(Context.PRINT_SERVICE);

    // Set job name, which will be displayed in the print queue
    String jobName = getActivity().getString(R.string.app_name) + " Document";

    // Start a print job, passing in a PrintDocumentAdapter implementation
    // to handle the generation of a print document
    printManager.print(jobName, new MyPrintDocumentAdapter(getActivity()),
            null); //
}
0

My solution. I used Epson TM series. I think the port is 9100 for default. In Manifest add:

<uses-permission android:name="android.permission.INTERNET"/>

in the activity use a Thread otherwise u you can the android.os.NetworkOnMainThreadException error.

Thread thread = new Thread(new Runnable() {

            @Override
            public void run() {
               try  {
                  Socket sock = new Socket("192.168.1.168", 9100);
                  PrintWriter oStream = new PrintWriter(sock.getOutputStream());
                  oStream.println("Hi, test from Android Device");
                  oStream.println("\n");
                  oStream.close();
                  sock.close();
                } catch (UnknownHostException e)
                  {
                   e.printStackTrace();
                  }
                  catch (IOException e)
                  {
                    e.printStackTrace();
                  }
                    }});

If not enough, add in manifest these:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
Boris Karloff
  • 1,190
  • 12
  • 20
-2

Well, you cant connect any devices directly as you will need the driver installed. there are 3rd party apps like Google Cloud print that works seamlessly with Android though.

  • 1
    **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. – Vassilis Jul 28 '17 at 18:49