简体   繁体   中英

Android show no output from Inputstream reader and Procees return exit code 1

i am trying to run a process and to read it's data from InputStreamReader, but it fails in a weird way.

The executable is "ip neigh show"

When trying to run the command from connected device via adb shell the command executes OK and also displays data correctly.

But when trying to execute it from kotlin code it exit with exit code 1 and InputStreamReader show empty data also.

This is how i am trying it:

val p = Runtime.getRuntime().exec("ip neigh show")
InputStreamReader(p!!.inputStream).forEachLine fori@{ line ->
                    val teDhenat = line.split("\\s+".toRegex())
                    if (teDhenat[0] == ip) {
                        if (teDhenat.size < 4) {
                            return@fori
                        } else {
                            uGjet = true
                            macAddress = teDhenat[4]
                            return@fori
                        }
                    }
                }

The problem seems to happen in that line: val p = Runtime.getRuntime().exec("ip neigh show") but i don't understand why.

Also tried with val p = Runtime.getRuntime().exec("/system/bin/ip neigh show") and it's still the same.

Also i have tried using ProcessBuilder() and it doesn't work too.

The Compile and Target SDK is 31 The phone is Xiaomi running Android 11 (SDK 30)

PS: Also i am using same logic for other executables and they work very fine like "ping", "top" etc...

Since Android 11 (SDK 30):

In addition, non-privileged apps can't access the device's MAC address; only network interfaces with an IP address are visible. This impacts the getifaddrs() and NetworkInterface.getHardwareAddress() methods, as well as sending RTM_GETLINK Netlink messages.

The following is a list of the ways that apps are affected by this change:

NetworkInterface.getHardwareAddress() returns null for every interface.

Apps cannot use the bind() function on NETLINK_ROUTE sockets.

The ip command does not return information about interfaces.

Apps cannot send RTM_GETLINK messages. Note that most developers should use the higher-level APIs of ConnectivityManager rather than lower-level APIs like NetworkInterface, getifaddrs(), or Netlink sockets. For example, an app that needs up-to-date information on the current routes can get this information by listening for network changes using ConnectivityManager.registerNetworkCallback() and calling the network's associated LinkProperties.getRoutes().

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM