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