简体   繁体   中英

Running a terminal command from within an app?

I'm attempting to make a simple app to change a value on my AOSP device's GPIO directory to toggle a peripheral device. I'm trying to do this by running a shell command from within the app, but my code doesn't seem to be doing anything:

   private fun toggle(zeroOrOne: String) {


    try {


        val command = "echo $zeroOrOne > /sys/class/gpio/gpio690/value"

        val process = Runtime.getRuntime().exec(command)

        val reader = BufferedReader(InputStreamReader(process.inputStream))
        val line: String? = ""

        while ((reader.readLine()) != null) {
            println(line)

        }
    } catch (t: Throwable) {
        t.printStackTrace()

    }


    }

Nothing is printing and the peripheral isn't responding. Is there something I'm missing?

In general, Android applications don't have the correct Linux permissions to access system file handles like this. On most systems, read/write access to /sys/class/gpio requires root permissions — something that is not granted to apps.

Since you are building your own device based on AOSP, you would need to modify the access permission levels of these two things to match (for example, you could set the group ownership of /sys/class/gpio to system and make your app part of the system group).

Another option would be to build into your custom system a service/daemon that is able to provide file handles to apps based on Android permissions or some other set of rules (side note, this is essentially how the Peripheral I/O works in Android Things).

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