According to "How Network Printing Works" it really depends on the type of printer and the protocol which it supports. If you know the ip and the port used by you printer and if your printer supports SNMP (just to pick a protocol) you can use the SNMP protocoll to query your printer for information. There is the Java lib SNMP4j which can help you achive this. I would suggest to not use it unless the printer, the ip and the port will never (!) change for your setup. This is because you can run into several problems
- How to discover an unknown printer ?
- How to discover the port used by the printer ?
- How to discover the protocoll used by the printer ?
Lets assume the questions above wouldn't be much of a problem and lets assume every printer would support SNMP. How to get informations out of it ? Besides using the mentioned java lib, you can use snmpget
in linux from an terminal. The syntax is as follows:
snmpget -v1 -c public host-ip OID
The OID is an object identifier for every property of you printer reaching from pagecount to toner-cardridge information. If you don't add an OID you'll get the whole list of available OID's. The crux of the matter is although all OID's are standardized the usage of the OID's differ from brand to brand and from printer-model to printer-model. For my HP the following works:
snmpget -v1 -c public 192.168.1.10 iso.3.6.1.2.1.43.17.6.1.5.1.2
and returns
iso.3.6.1.2.1.43.17.6.1.5.1.2 = STRING: "Ready"
the use OID returns the status of the printer for my HP. But if I use the same OID on my Canon I'll get
Error in packet
Reason: (noSuchName) There is no such variable name in this MIB.
Failed object: iso.3.6.1.2.1.43.17.6.1.5.1.2
Therefore it is not even SNMP generically applicable, not mentioning the other protocols which are available.
Considering all these information, the easiest way in my opinition is just check if you can establish the connection to the printer on one of the common printer ports via this code
boolean available = false;
try {
String serverAddress = "192.168.1.10";
Socket s = new Socket(serverAddress, 9100);
s.close();
available = true;
} catch (IOException e) {
available = false;
}
System.out.println("printer available: " + available);
Of course this only works if you already know the printer ip.
PrinterIsAcceptingJobs
attribute on thePrintService
? – Oquendo