1

I'm creating a java project that shows hostname , ip and mac address of connected devices using java . I use InetAddress but it only shows me the Ip's and hostname of connected computers without MAC and also can't see connected mobiles like Android devices.

I tried almost every solution on StackOverFlow but none of theme seems to work

public class HostDiscovery
{
    private String address;
    private int discoveryTimeout;

    HostDiscovery( String address,  int discoveryTimeout)
    {
       this.address = address;
       this.discoveryTimeout = discoveryTimeout;
    }

    public Future<HostDiscoveryResult> multithreadedHostDicovery(final ExecutorService exService){
        return exService.submit(() -> {
            try
            {
                String HostName = null;
                String MacAddress = null;
                boolean result = InetAddress.getByName(address).isReachable(discoveryTimeout);
                InetAddress ip = InetAddress.getByName(address);
                 if (result)
                {

                    HostName = InetAddress.getByName(address).getHostName();
                    if(HostName.endsWith(".mshome.net")){
                        HostName = HostName.replaceAll(".mshome.net", "");
                    }
                    MacAddress = getMacAddress(ip);
                }
                return new HostDiscoveryResult(address, result, HostName, MacAddress);
            } catch (SocketException ex)
            {
                return new HostDiscoveryResult(address, false, null, null);
            }
        });
    }



   private static String getMacAddress(InetAddress ip) throws UnknownHostException {
        String address = null;
        try {
            NetworkInterface network = NetworkInterface.getByInetAddress(ip);
            byte[] mac = network.getHardwareAddress();
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < mac.length; i++) {
                sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : ""));
            }
            address = sb.toString();

        } catch (SocketException ex) {

            ex.printStackTrace();

        }

        return address;
    }




}
Mebin Joe
  • 2,172
  • 4
  • 16
  • 22
Fahed Selmi
  • 29
  • 1
  • 7
  • To send a broadcast to every device on the local link, you need the [ARP protocol](https://en.wikipedia.org/wiki/Address_Resolution_Protocol), and Java cannot do that natively. See [Is there a way to send ARP request via Java?](https://stackoverflow.com/q/14650089/5221149) – Andreas Apr 10 '19 at 20:57

0 Answers0