簡體   English   中英

使用SNMP OID獲取打印機狀態

[英]Get Printer Status Using SNMP OID

我在Android中使用SNMP。 我想獲得打印機狀態。 我使用的是Snmp4Android.jar和OID 1.3.6.1.2.1.25.3.5.1.1 請參閱此鏈接打印機狀態

SnmpActivity.java在這里

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();
    }

謝謝...

使用net-snmp或其他一些可用的MIB瀏覽器(例如通過iReasoning)。 您可能會發現hrPrinterTable(OID .1.3.6.1.2.1.25.3.5)中沒有數據。

如果有一些行,則將OID更改為1.3.6.1.2.1.25.3.5.1.1.1(最后添加1)。 這是第一行上hrPrinterStatus的正確OID。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM