通过 IP 获取设备 MAC 地址

好吧,在我的应用程序中,我使用一些代码来发现本地网络中所有可访问的设备。现在我想使用本地 ip 地址获取设备的 mac 地址


我想要这样的东西: getMacAddress("192.168.1.23")


public static String getMacAddress(String ip)

{

    String macAddress = ...

    return macAddress;

}

我发现这个是为了获取我自己的 mac 地址,那么 LAN 中的其余 ips 呢?


 WifiManager manager = (WifiManager) getSystemService(Context.WIFI_SERVICE);

 WifiInfo info = manager.getConnectionInfo();

 String address = info.getMacAddress();


天涯尽头无女友
浏览 210回答 3
3回答

qq_花开花谢_0

/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从本地网络路由器检索缓存。

蝴蝶不菲

该功能仅适用于获取您自己的 MAC,即使如此,它对现代 Android 也有严重的限制。您想要的不是 Android 的功能。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java