6

My requirement is to print a file from an android device without using any cloud based service.

I have been able to achieve it using "Raw" print protocol i.e by simply sending the file to printer's IP address at Port 9100. Here is the code snippet for that:

 client = new Socket(ip,port); //Port is 9100
 byte[] mybytearray = new byte[(int) file.length()]; //create a byte array to file
 fileInputStream = new FileInputStream(file);
 bufferedInputStream = new BufferedInputStream(fileInputStream);
 bufferedInputStream.read(mybytearray, 0, mybytearray.length); //read the file
 outputStream = client.getOutputStream();
 outputStream.write(mybytearray, 0, mybytearray.length); //write file to the output stream byte by byte
 outputStream.flush();
 bufferedInputStream.close();
 outputStream.close();

The problem with "Raw" printing protocol is that there is no way to get the status back from the printer.

So, I recently read about IPP and LDR using which we can get the status back from printer.

I have tried to find a way to implement them using android but had no success. I have already went through this answer but had no success in finding my solution.

It will be really helpful if someone can guide me on how to implement IPP or LDR in android.

Thanks in advance!

Community
  • 1
  • 1
Exception
  • 2,273
  • 1
  • 24
  • 42
  • What kind of status do you want to read? IPP offers a generic response-status that is related to the IPP communication itself. You can read attributes from printer-objects or job-objects. You could check whether the job has been submitted okay or even wait until the job-state is 'completed' (=printed). – IPP Nerd May 09 '15 at 12:53
  • Basically I just want a confirmation that job submitted has finished or not i.e whether the printer printed the file or not. In my current scenario mentioned above, I couldn't get any status like that – Exception May 09 '15 at 15:09
  • @Exception i was trying your code to print a pdf file but it is only printing byte code and not the actual file data, could you please help me here – Vishwajit Palankar Sep 19 '16 at 14:18
  • Could you tell me what printer this works with? I am using an inexpensive HP model, and it will only print ANSI characters. – Robert R Evans Nov 01 '16 at 12:59

1 Answers1

1

General usage of IPP:

  1. Once a print job has been submitted the printer returns a job-id
  2. Use the Get-Job-Attributes-Operation in order to get the current job-state
  3. Wait until the attribute job-state equals to 9 (means 'completed')

There are other final job-states you should check for: aborted or canceled

For prototyping you could use the ipptool (native for desktop usage):

# ipptool -t -d job=482 ipp://192.168.2.113/ipp job.ipp
{
OPERATION Get-Job-Attributes
GROUP operation-attributes-tag
  ATTR charset attributes-charset utf-8
  ATTR language attributes-natural-language en
  ATTR uri printer-uri $uri
  ATTR integer job-id $job
}

Update 5/2020

I have published a kotlin implementation of the ipp protocol.

https://github.com/gmuth/ipp-client-kotlin

Once submitted you can wait for the print job to terminate: job.waitForTermination()

IPP Nerd
  • 992
  • 9
  • 25
  • I still don't understand how do I implement it on android and I don't want to use Android-PrintService because I don't think it uses IPP or LPR – Exception May 12 '15 at 04:38
  • There are a number of android print service apps available that claim to support IPP - with no cloud-processing involved. Unfortunately they don't seem to work very well. You can very well bypass the official android print api and connect straight to the printer. The downside is that you have to implement IPP yourself. – IPP Nerd Oct 02 '15 at 10:36
  • @IPPNerd How to use ipp in Android application? Do you have sample for demo? – PhongBM Sep 13 '21 at 04:02
  • I've provided examples how to use the library here: https://github.com/gmuth/ipp-samples – IPP Nerd Sep 13 '21 at 09:31