0

I am working with SNMP in Android. I want to get Printer Status. I am using Snmp4Android.jar and OID 1.3.6.1.2.1.25.3.5.1.1. Please refer this link Printer Status.

SnmpActivity.java here

public class SnmpActivity extends Activity {

private static String ipAddress = "PrinterIP";

private static String port = "Port";

private static String oidValue = "1.3.6.1.2.1.25.3.5.1.1";

public static Snmp snmp;
public static CommunityTarget comtarget;
static PDU pdu;
static OID oid;
static VariableBinding req;
Button b;
private static final String tag = "SNMP CLIENT";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    System.setProperty("java.net.preferIPv4Stack", "true");
    System.setProperty("java.net.preferIPv6Addresses", "false");

    b = (Button) findViewById(R.id.buttonClick);
    b.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            try {
                sendSnmpRequest(oidValue);
            } catch (IOException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    });
}



private void sendSnmpRequest(String oid) throws Exception {

        ResponseEvent response;
        OctetString community1 = new OctetString("public");
        String host = ipAddress + "/" + port;
        Address tHost = new UdpAddress(host);
        TransportMapping transport = new DefaultUdpTransportMapping();
        transport.listen();
        CommunityTarget comtarget = new CommunityTarget();
        comtarget.setCommunity(community1);
        comtarget.setVersion(SnmpConstants.version1);
        comtarget.setAddress(tHost);
        comtarget.setRetries(2);
        comtarget.setTimeout(5000);
        PDU pdu = new PDU();
        pdu.add(new VariableBinding(new OID(oid)));
        pdu.setType(PDU.GET);
        snmp = new Snmp(transport);
        response = snmp.get(pdu, comtarget);
        if (response != null) {
            Log.i(tag, "Got Response from Agent  "
                    + response.getResponse().toString());
            PDU responsePDU = response.getResponse();
            if (responsePDU != null) {
                int errorStatus = responsePDU.getErrorStatus();
                int errorIndex = responsePDU.getErrorIndex();
                String errorStatusText = responsePDU.getErrorStatusText();

                if (errorStatus == PDU.noError) {
                    Log.i(tag,
                            "Snmp Get Response = "
                                    + responsePDU.getVariableBindings());
                    Toast.makeText(
                            getApplicationContext(),
                            "Snmp Get Response = "
                                    + responsePDU.getErrorStatusText(),
                            Toast.LENGTH_LONG).show();

                    System.out
                            .println("--" + responsePDU.getVariableBindings());

                } else {
                    Log.i((String) tag, "Error: Request Failed");
                    Log.i(tag, "Error Status = " + errorStatus);
                    Log.i(tag, "Error Index = " + errorIndex);
                    Log.i(tag, "Error Status Text = " + errorStatusText);
                }

            } else {
                Log.i(tag, "Error: Response PDU is null");
            }
        } else {
            Log.i(tag, "Error: Agent Timeout... ");
        }
        snmp.close();
    }

Thanks...

L.R.
  • 977
  • 6
  • 22
user4232
  • 592
  • 1
  • 12
  • 37
  • What is the output of your code now? – L.R. Apr 22 '13 at 14:59
  • if (errorStatus == PDU.noError) this is not working else part of this if condition is now showing ie, Log.i((String) tag, "Error: Request Failed"); Log.i(tag, "Error Status = " + errorStatus); Log.i(tag, "Error Index = " + errorIndex); Log.i(tag, "Error Status Text = " + errorStatusText); – user4232 Apr 22 '13 at 15:23
  • And what is the error status, index and text? Problems with communication over SNMP can have multiple reasons, most likely firewall blocking udp port 161, device not listening on that port, wrong community string, ... – L.R. Apr 23 '13 at 06:35
  • Error: Request Failed Error Status = 2 Error Index = 1 Error Status Text = No such name this is the out put. Anyway, thanks for your reply.... – user4232 Apr 23 '13 at 07:35

1 Answers1

2

Use net-snmp or some other available MIB browser (e.g. by iReasoning). You may find out that there are no data in the hrPrinterTable (OID .1.3.6.1.2.1.25.3.5).

If there are some rows, then change the OID to 1.3.6.1.2.1.25.3.5.1.1.1 (add 1 at the end). This the proper OID for the hrPrinterStatus on the first row.

L.R.
  • 977
  • 6
  • 22