Search code examples
androidserversocketbonjourprintersipp-protocol

Bonjour(IPP) vs Jetdirect-Socket Printer


I am trying to use my android phone as a printer. I am using ServerSocket to receive the document to be printed. If I add my phone as IP printer by providing IP address and port and select Generic Postscript Printer, I am able to receive the file in ps format correctly. I don't want to add my phone as printer as IP printer. So Now I am using NsdManager to register my device as printer. It gets recognized as Bonjour printer automatically and I can successfully add as printer. But now every time I print a document from by computer I get this data in the socket's input stream.

POST / HTTP/1.1 Content-Length: 673 Content-Type: application/ipp Host:   
Android-2.local:9200 User-Agent: CUPS/2.1.0 (Darwin 15.2.0; x86_64)   
IPP/2.0 Expect: 100-continue Gattributes-charsetutf-8Hattributes-
natural-languageen-usEprinter-uriipp://Android- 
2.local.:9200/Drequested-attributescompression-supportedDcopies-
supportedDcups-versionDdocument-format-supportedD marker-colorsDmarker-
high-levelsD marker-levelsDmarker-low-levelsDmarker-messageDmarker-
namesDmarker-typesDmedia-col-supportedD$multiple-document-handling-
supportedDoperations-supportedDprint-color-mode-supportedD printer-
alertDprinter-alert-descriptionDprinter-is-accepting-jobsD printer-
mandatory-job-attributesD printer-stateDprinter-state-messageDprinter-  
state-reasons

I read the IPP documentation and I am sending 100 Continue in the response and all the required params like this

clientSocket.setTcpNoDelay(true);
BufferedOutputStream out = new BufferedOutputStream(clientSocket.getOutputStream());
out.write("HTTP/1.1 100\r\n".getBytes("UTF-8"));
out.write("\r\n".getBytes("UTF-8"));
out.write("compression-supported: \"none\"\r\n".getBytes("UTF-8"));
out.write("printer-is-accepting-jobs: \"true\"\r\n".getBytes("UTF-8"));
.....
....
out.flush();

After that if i try to read the input stream for document, it gives null and on my computer i receive message "Printing: Connected to Printer" but if do out.close(); in to close the outputstream for the socket I get the message "unable to get printer status" on my computer. Please help me. Is there any way I just receive the document and not this post request or way to send a correct response and get the document? I am stuck with this for quite a long time now. Any help is highly appreciated.


Solution

  • I was finally able to implement it. Here is an excellent implementation for network printer using Node.js. It explains the details of IPP https://github.com/watson/ipp-printer

    Also this video is good demonstration https://www.youtube.com/watch?v=58Ti8w1yX2w

    And I am using https://github.com/NanoHttpd/nanohttpd to handle the printing requests on my android phone.