简体   繁体   English

java android Q 从热点 /proc/net/arp 读取 ip:打开失败:EACCES(权限被拒绝)

[英]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) {
            }
        }
    }

Android 10 onwards accessing "/proc/net/arp" is not allowed.从 Android 10 开始,不允许访问“/proc/net/arp”。 For more info refer here有关更多信息,请参阅此处

 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.

相关问题 BitmapFactory:无法解码流:java.io.FileNotFoundException:打开失败:Android Q 上的 EACCES(权限被拒绝) - BitmapFactory: Unable to decode stream: java.io.FileNotFoundException:open failed: EACCES (Permission denied) on Android Q 打开失败:AsyncTask中来自IoBridge.java的EACCES(权限被拒绝) - open failed: EACCES (Permission denied) from IoBridge.java in AsyncTask Android打开失败:EACCES(权限被拒绝) - Android open failed: EACCES (Permission denied) Android 6.0 打开失败:EACCES(权限被拒绝) - Android 6.0 open failed: EACCES (Permission denied) Android 11 打开失败:EACCES(权限被拒绝) - Android 11 open failed: EACCES (Permission denied) Android-打开失败:/ mnt中的EACCES(权限被拒绝) - Android - open failed: EACCES (Permission denied) in /mnt 打开失败:EACCES(权限被拒绝)? - Open failed: EACCES (Permission denied)? Android打开失败:尝试读取数据/数据中的文件时EACCES(权限被拒绝) - Android open failed: EACCES (Permission denied) when try to read a file in data/data 从/ proc / net / arp(Android)检索IP和MAC地址 - Retrieve IP and MAC addresses from /proc/net/arp (Android) java.net.SocketException:套接字失败:EACCES(权限被拒绝) - java.net.SocketException: socket failed: EACCES (Permission denied)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM