好吧,当我的手机作为热点工作时,我遇到了一种情况,我需要检测所有连接到我手机的设备并找到它们的 MAC 地址。我写了这样的东西:
public void getListOfConnectedDevice() {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
BufferedReader br = null;
boolean isFirstLine = true;
try {
br = new BufferedReader(new FileReader("/proc/net/arp"));
String line;
while ((line = br.readLine()) != null) {
if (isFirstLine) {
isFirstLine = false;
continue;
}
String[] splitted = line.split(" +");
if (splitted != null && splitted.length >= 4) {
String ipAddress = splitted[0];
String macAddress = splitted[3];
boolean isReachable = InetAddress.getByName(
splitted[0]).isReachable(300);// this is network call so we cant do that on UI thread, so i take background thread.
Log.d(TAG, "ip: " + splitted[0]);
Log.d(TAG, "isReachable: " + isReachable);
if (isReachable) {
Log.d("Device Information", ipAddress + " : "
+ macAddress);
macAddresses.add(macAddress); //My List<String>
}
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
});
thread.start();
}
但boolean isReachable = InetAddress.getByName(
splitted[0]).isReachable(300);
如果设备连接或断开连接,则仅返回 false。而且我找不到任何信息来找到解决方案。或者有没有其他解决方案?(对于未植根的手机)。