[英]java android Q read ip from hotspot /proc/net/arp: open failed: EACCES (Permission denied)
I want to read ip which are connect to my hotspot only on android QI have a problem: this is a log:我想阅读仅在 android 上连接到我的热点的 ip QI 有一个问题:这是一个日志:
java.io.FileNotFoundException: /proc/net/arp: open failed: EACCES (Permission denied)
and here is what not work:这是不起作用的:
br = new BufferedReader(new FileReader("/proc/net/arp"));
I add to manifest:我添加到清单:
android:requestLegacyExternalStorage="true"
add all permission are granded also I add:添加所有权限我还添加:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (Settings.System.canWrite(ListOfTerminalsActivity.this)) {
} else {
Intent intent = new Intent(android.provider.Settings.ACTION_MANAGE_WRITE_SETTINGS);
intent.setData(Uri.parse("package:" + getPackageName()));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
try {
startActivity(intent);
} catch (ActivityNotFoundException ignored) {
}
}
}
Pattern macPattern = Pattern.compile("..:..:..:..:..:..");
BufferedReader br = null;
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
Process ipProc = Runtime.getRuntime().exec(IP_CMD);
ipProc.waitFor();
if (ipProc.exitValue() != 0) {
throw new Exception("Unable to access ARP entries");
}
br = new BufferedReader(new InputStreamReader(ipProc.getInputStream(), "UTF-8"));
String line;
while ((line = br.readLine()) != null) {
String[] neighborLine = line.split("\\s+");
if (neighborLine.length <= 4) {
continue;
}
String ip = neighborLine[0];
final String hwAddr = neighborLine[4];
InetAddress addr = InetAddress.getByName(ip);
if (addr.isLinkLocalAddress() || addr.isLoopbackAddress()) {
continue;
}
String macAddress = neighborLine[4];
String state = neighborLine[neighborLine.length - 1];
if (!NEIGHBOR_FAILED.equals(state) && !NEIGHBOR_INCOMPLETE.equals(state)) {
boolean isReachable = false;
try {
isReachable = InetAddress.getByName(ip).isReachable(5000);
} catch (IOException e) {
e.printStackTrace();
}
if (isReachable) {
result.add(new WifiClient(ip, hwAddr));
}
}
}
} else {
br = new BufferedReader(new FileReader("/proc/net/arp"));
String line;
while ((line = br.readLine()) != null) {
String[] parts = line.split(" +");
if (parts.length < 6) {
continue;
}
final String ipAddr = parts[0];
final String hwAddr = parts[3];
if (!ipAddr.equalsIgnoreCase("IP")) {
boolean isReachable = false;
try {
isReachable = InetAddress.getByName(ipAddr).isReachable(5000);
} catch (IOException e) {
e.printStackTrace();
}
if (isReachable) {
result.add(new WifiClient(ipAddr, hwAddr));
}
}
}
}
} catch (Exception e) {
} finally {
try {
if (br != null) {
br.close();
}
} catch (IOException e) {
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.