【问题标题】:java android Q read ip from hotspot /proc/net/arp: open failed: EACCES (Permission denied)java android Q从热点/proc/net/arp读取ip:打开失败:EACCES(权限被拒绝)
【发布时间】:2020-10-19 16:17:58
【问题描述】:

我想读取仅在 android Q 上连接到我的热点的 ip 我有一个问题: 这是日志:

java.io.FileNotFoundException: /proc/net/arp: open failed: EACCES (Permission denied)

这是行不通的:

 br = new BufferedReader(new FileReader("/proc/net/arp"));

我添加到清单中:

android:requestLegacyExternalStorage="true"

我还添加了所有权限:

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) {
            }
        }
    }

【问题讨论】:

  • 你找到解决办法了吗?
  • @Manzotin 是的,我做到了
  • 介意分享一下吗?
  • @Manzotin 是的,我分享

标签: java android


【解决方案1】:

Android 10 及更高版本不允许访问“/proc/net/arp”。更多信息请参考here

【讨论】:

    【解决方案2】:
     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) {
                    }
                }
    

    【讨论】:

    • 第7行IP_CMD的值是多少?
    • @Manzotin private static final String IP_CMD = "ip neighbor";
    • 在我的 Android 11 中,我收到了这个错误:无法绑定 netlink 套接字:权限被拒绝
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-23
    • 2021-07-02
    • 2016-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多