private String getIP(String mac) {
String ip = null;
try {
BufferedReader br = new BufferedReader(new FileReader("/proc/net/arp"));
String line = "";
while((line = br.readLine()) != null) {
String[] tokens = line.split("\\s+");
// The ARP table has the form:
// IP address HW type Flags HW address Mask Device
// 192.168.178.21 0x1 0x2 00:1a:2b:3c:4d:5e * tiwlan0
if(tokens.length >= 4 && tokens[3].equalsIgnoreCase(mac)) {
ip = tokens[0];
break;
}
}
br.close();
}
catch(Exception e) { Log.e(TAG, e.toString()); }
return ip;
}
但要注意:除非你已经用你的电脑建立了联系(和您将需要它的IP地址或名称),ARP表是空的。
我想你想反过来这样做,建立与PC只知道它的MAC地址的连接。那就不那么简单了。您可以尝试ping本地网络上的每个人(Runtime.getRuntime().exec("ping -b 192.168.178.255");
)以填充ARP表。
或者,也许你可以从你的路由器获得所有客户端IP地址列表?
在PC上有一个叫做'arp'的命令。我想知道你能在Android上找到什么。 –
这个答案可能会帮助你http://superuser.com/questions/29640/inverse-arp-lookup –
arping是要走的路。请注意,某些设备可能会阻止arp包。 – rsan