通过 IP 获取设备的 MAC 地址。

4

我的应用程序中使用一些代码来发现本地网络中所有可达设备。现在我想通过本地 IP 地址获取设备的 MAC 地址。

我想要类似这样的功能:getMacAddress("192.168.1.23")

public static String getMacAddress(String ip)
{
    String macAddress = ...
    return macAddress;
}

我找到了获取自己mac地址的方法,那么如何获取局域网中其他ip的mac地址呢?
 WifiManager manager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
 WifiInfo info = manager.getConnectionInfo();
 String address = info.getMacAddress();

获取自己的 MAC 地址的方法在 Android 6 中已经无法使用。该方法始终返回常量值 “02:00:00:00:00:00”。 - cesargastonec
3个回答

4

/proc/net/arp 是本地的 ARP 缓存,至少具有已缓存的条目(可能处于离线状态)。

protected String getMacAddress(String ipAddress) {
    try {
        BufferedReader br = new BufferedReader(new FileReader(new File("/proc/net/arp")));
        String line;
        while((line = br.readLine()) != null) {
            if(line.contains(ipAddress)) {
                /* this string still would need to be sanitized */
                return line;
            }
        }
        System.out.println(output);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

另外,也可以扫描整个本地网络段 -

或者从本地网络路由器检索ARP缓存。


什么是 System.out.println(output);? - Seraphim's
简短回答:它是JAVA内置方法,用于在控制台中打印传递给它的参数。 - JexSrs

4

参考:如何获取通过手机热点连接的设备数量?

在Android上,您可以通过以下链接计算接入点上连接的设备数量,并获取硬件MAC地址:http://www.flattermann.net/2011/02/android-howto-find-the-hardware-mac-address-of-a-remote-host/

以上链接提供的代码:

/**
 * Try to extract a hardware MAC address from a given IP address using the
 * ARP cache (/proc/net/arp).<br>
 * <br>
 * We assume that the file has this structure:<br>
 * <br>
 * IP address       HW type     Flags       HW address            Mask     Device
 * 192.168.18.11    0x1         0x2         00:04:20:06:55:1a     *        eth0
 * 192.168.18.36    0x1         0x2         00:22:43:ab:2a:5b     *        eth0
 *
 * @param ip
 * @return the MAC from the ARP cache
 */

public static String getMacFromArpCache(String ip) {
    if (ip == null)
        return null;
    BufferedReader br = null;
    try {
        br = new BufferedReader(new FileReader("/proc/net/arp"));
        String line;
        while ((line = br.readLine()) != null) {
            String[] splitted = line.split(" +");
            if (splitted != null && splitted.length >= 4 && ip.equals(splitted[0])) {
                // Basic sanity check
                String mac = splitted[3];
                if (mac.matches("..:..:..:..:..:..")) {
                    return mac;
                } else {
                    return null;
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            br.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return null;
}

如果您的代码存在问题,请尝试以下代码:
 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));
                ad = currentPingAddr.toString();   /////////////////
                Log.d("MyApp",ad);                 //////////////

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

                    ret.add(currentPingAddr);
                    ad = currentPingAddr.toString();        /////////////////
                    Log.d("MyApp",ad);                     //////////////
                }
            } catch (UnknownHostException ex) {
            } catch (IOException ex) {
            }

            LoopCurrentIP++;
        }
        return ret;
    }

0

那个函数只能用来获取你自己的MAC地址,即使在现代Android上它也有严重的限制。你想要的不是Android的一个功能。


有很多安卓应用可以做到这一点,例如Fing或其他Play Store应用。那么它们是如何做到的? - JexSrs
你测试过它们的Android版本是什么?从6.0开始,你甚至无法获取自己的MAC地址-它会返回一个硬编码的02:00:00:00:00:00。 - Gabe Sechan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接