0

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?

devunwired
  • 62,780
  • 12
  • 127
  • 139
andrewedgar
  • 797
  • 1
  • 12
  • 46
  • Why not just open it as a file? Technically android is linux and in linux everything is a file... so you can just output to a File with that gpio path – MiltoxBeyond Aug 08 '19 at 20:48
  • Thank you for your response. Apologies, but I'm not sure what is meant by "opening it as a file." Do I just create a new file with that path? Where should I put the 0/1 value? – andrewedgar Aug 08 '19 at 20:59

1 Answers1

0

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).

devunwired
  • 62,780
  • 12
  • 127
  • 139