java android Q read ip from hotspot /proc/net/arp: open failed: EACCES (Permission denied)

Viewed 1633

I want to read ip which are connect to my hotspot only on android Q I have a problem : this is a log :

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

Android 10 onwards accessing "/proc/net/arp" is not allowed. 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) {
                }
            }
Related