好吧,在我的应用程序中,我使用一些代码来发现本地网络中所有可访问的设备。现在我想使用本地 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();
3 回答
qq_花开花谢_0
TA贡献1835条经验 获得超7个赞
/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从本地网络路由器检索缓存。
添加回答
举报
0/150
提交
取消