[英]Find IP of device connected to mobile hotspot using MAC address
我试图制作一个应用程序,它可以使用它的 MAC 地址找到连接到我的 android 手机热点的设备的 IP 地址。 我知道这可以使用 ARP 表来完成,但我只是不知道如何。 我正在使用 android 工作室和 java 并且我的设备正在运行 android 10。请帮助。
这是它在AndroidNetworkTools库中的实现方式,对我来说效果很好:
/**
* Returns all the IP/MAC address pairs currently in the ARP cache (/proc/net/arp).
*/
public static HashMap<String, String> getAllIPAndMACAddressesInARPCache() {
HashMap<String, String> macList = new HashMap<>();
for (String line : getLinesInARPCache()) {
String[] splitted = line.split(" +");
if (splitted.length >= 4) {
// Ignore values with invalid MAC addresses
if (splitted[3].matches("..:..:..:..:..:..")
&& !splitted[3].equals("00:00:00:00:00:00")) {
macList.put(splitted[0], splitted[3]);
}
}
}
return macList;
}
/**
* Method to read lines from the ARP Cache
*/
private static ArrayList<String> getLinesInARPCache() {
ArrayList<String> lines = new ArrayList<>();
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader("/proc/net/arp"));
String line;
while ((line = br.readLine()) != null) {
lines.add(line);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (br != null) {
br.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return lines;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.