【问题标题】:Find printer IP address from a list of ip addresses of network devices从网络设备的 IP 地址列表中查找打印机 IP 地址
【发布时间】:2014-05-25 10:44:23
【问题描述】:

目标-我需要在 wifi 上检测到我的 Brother QL-720NW 标签打印机,以便从我的应用程序进行打印。

我在 SO 上遇到过各种类似的问题,例如 Get the IP address of printerHow to connect a network printer over Android?How to get IP adress of other hosts in same wifi network in android?

但是以上都没有完全解决我的问题。

使用这个 How to get IP address of the device from code? 我可以获取我的 wifi 网络上所有 IP 地址的列表。

代码:

String myIpAdd= getIPAddress(true);
ArrayList<InetAddress>  inetAddresses=getConnectedDevices(myIpAdd);

public ArrayList<InetAddress> getConnectedDevices(String YourPhoneIPAddress) {
        ArrayList<InetAddress> ret = new ArrayList<InetAddress>();

        LoopCurrentIP = 0;

        String IPAddress = "";
        String[] myIPArray = YourPhoneIPAddress.split("\\.");
        InetAddress currentPingAddr;

        for (int i = 0; i <= 255; i++) {
            try {

                // build the next IP address
                currentPingAddr = InetAddress.getByName(myIPArray[0] + "." +
                        myIPArray[1] + "." +
                        myIPArray[2] + "." +
                        Integer.toString(LoopCurrentIP));

                // 50ms Timeout for the "ping"
                if (currentPingAddr.isReachable(50)) {

                    ret.add(currentPingAddr);
                }
            } catch (UnknownHostException ex) {
                ex.printStackTrace();
            } catch (IOException ex) {
                ex.printStackTrace();
            }

            LoopCurrentIP++;
        }
        return ret;
    }

     /**
     * Get IP address from first non-localhost interface
     * @param ipv4  true=return ipv4, false=return ipv6
     * @return  address or empty string
     */
    public static String getIPAddress(boolean useIPv4) {
        try {
            List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
            for (NetworkInterface intf : interfaces) {
                List<InetAddress> addrs = Collections.list(intf.getInetAddresses());
                for (InetAddress addr : addrs) {
                    if (!addr.isLoopbackAddress()) {
                        String sAddr = addr.getHostAddress().toUpperCase();
                        boolean isIPv4 = InetAddressUtils.isIPv4Address(sAddr); 
                        if (useIPv4) {
                            if (isIPv4) 
                                return sAddr;
                        } else {
                            if (!isIPv4) {
                                int delim = sAddr.indexOf('%'); // drop ip6 port suffix
                                return delim<0 ? sAddr : sAddr.substring(0, delim);
                            }
                        }
                    }
                }
            }
        } catch (Exception ex) { 
            ex.printStackTrace();
        } // for now eat exceptions
        return "";
    }

如何从 IP 地址列表中检测我的打印机的 IP 地址??

请帮忙。

【问题讨论】:

    标签: android networking printing android-wifi android-networking


    【解决方案1】:

    如果您想保持静态 IP 地址,您可能需要在打印机中进行设置。如果您在 DHCP 上关闭并重新打开它,则有可能会为其分配一个新 IP。一旦它是静态的,您就可以在应用程序中对其进行编码或将其放入设置中。这样您就无需搜索 IP 地址。

    【讨论】:

      【解决方案2】:

      Rachita,我将添加(就在添加到列表之前)一个代码以通过 Socket 连接并在端口 9100 上进行测试以寻找打印机。 Here is an example。希望有帮助

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-02-20
        • 1970-01-01
        • 1970-01-01
        • 2018-05-19
        • 2018-07-04
        相关资源
        最近更新 更多