3

Objective-I need to detect my Brother QL-720NW label printer on wifi, in order to print from my application.

I have gone through various similar questions on SO like Get the IP address of printer , How to connect a network printer over Android? ,How to get IP adress of other hosts in same wifi network in android? etc

But none of the above solve my problem completely.

Using this How to get IP address of the device from code? I'm able to get list of all ip addresses on my wifi network .

CODE:

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 "";
    }

How can I detect which Ip address is of my printer from the list of Ip addresses??

Please help.

Community
  • 1
  • 1
Rachita Nanda
  • 4,509
  • 9
  • 42
  • 66

2 Answers2

0

You might want to set it up in the printer if you are wanting to maintain a static ip address. There's a chance after turning it off and back on that it would be assigned a new IP if you are on DHCP. Once it's static you could just code it in the app or put it in a setting. That way you never need to search through the ip addresses.

Glenn
  • 79
  • 1
  • 10
0

Well Rachita, I would add (just before adding to the list) a code to connect via Socket and test on port 9100 looking for a printer. Here is an example. Hope that helps

Community
  • 1
  • 1
ramses
  • 505
  • 4
  • 8