简体   繁体   English

通过 IP 获取设备 MAC 地址

[英]Gettind a device MAC address by IP

Well, in my app I use some code to discover all reachable devices in Local Network.好吧,在我的应用程序中,我使用一些代码来发现本地网络中所有可访问的设备。 Now I want to get device's mac address using local ip address现在我想使用本地 IP 地址获取设备的 mac 地址

I want something like this: getMacAddress("192.168.1.23")我想要这样的东西: getMacAddress("192.168.1.23")

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

I have found this for getting my own mac address, what about the rest of the ips in the LAN?我发现这个是为了获取我自己的 mac 地址,那么 LAN 中的其余 ips 呢?

 WifiManager manager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
 WifiInfo info = manager.getConnectionInfo();
 String address = info.getMacAddress();

/proc/net/arp is the local ARP cache, which at least has cached entries (which may be offline). /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;
}

alternatively, one could scan the whole local network segment -或者,可以扫描整个本地网段 -

or retrieve the ARP cache from a local network router.或从本地网络路由器检索ARP缓存。

Ref: How can I get the number of devices connected through the phones access-point? Ref: 如何获取通过手机接入点连接的设备数量?

you can count connected devices on access point and it's get hardware mac address on below link on android: http://www.flattermann.net/2011/02/android-howto-find-the-hardware-mac-address-of-a-remote-host/您可以计算接入点上连接的设备,并在以下 android 链接上获取硬件 mac 地址: http : //www.flattermann.net/2011/02/android-howto-find-the-hardware-mac-address-of-远程主机/

code from above link:上面链接中的代码:

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

or if u have problem on code try below code:或者如果您在代码上遇到问题,请尝试以下代码:

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

That function works only to get your own MAC, and even then it has severe limitations on modern Android.该功能仅适用于获取您自己的 MAC,即使如此,它对现代 Android 也有严重的限制。 What you want isn't a feature of Android.您想要的不是 Android 的功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM