How to manage GPIO with JAVA (Android Studio) in Android 6.0.1 and above

Hello,

I am developing an Android app with Android Studio, I am trying to get access to GPIO in Android 6.0.1 but I can’t find the correct info from the web. I tested the GPIO Access with the Shell commands: https://bananapi.gitbooks.io/bpi-m64/content/en/how-to-test-gpio-pins-on-android.html , and yes, it’s working ok when I do it with ADB or directly in the Android Shell (tested in BPI with Android Terminal app), BUT I am not able to get the GPIO working with the JAVA Shell “Runtime.getRuntime.exec()”, at least in Android studio.

  • The GPIO can be accessed by calling Android shell commands in JAVA? How can I do it in Android Studio?
  • Another way to manage the GPIO in JAVA (Android Studio)?

Thanks!

Hi again, More about gpio:

I started the gpio test with an old BPI-M2 (Android 4.4) and worked ok with ADB and with an usb cable from a PC and also worked ok with Android Terminal apk directly from BPI M2. I noticed that if the gpio test commands are called from the Android Shell directly (without ADB), “su” is mandatory before doing setenforce.

In anyway, I am not able to get running the gpio Shell commands with JAVA & Android Studio, I think it shouldn’t be very difficult, at least in BPI-M2 because Android 4.4 is stock rooted.

In a BPI-M64, I can manage gpio from ADB with the PC and with an usb cable, but I am not able to manage GPIO with the Android Shell, “su” isn’t implemented. Of course, if Android Shell don’t support the su command, calling Shell commands from JAVA will not work to manage GPIO. In the Android Studio documentation, the unique paragraph that gives info about the GPIOs, is Android Things but looks that BPI needs to be installed Android things, or I can choose Android 6.0+Android Things to create the Project? (I think that things commands are incompatible with an Android 6.0.1 OS based device.

Pls, advanced users and BPI team,

  1. How can I call to gpio Shell commands from JAVA, or how to manage GPIOs in JAVA? or isn’t the right way?
  2. I read that exist a WiringPi library and also a WiringBP, special library for BPI products (including M2 and M64), writed in C. How can I use this libraries and write my own app in Studio or in what another way can I get an Android app that manages GPIOs (alternative to Studio)?
  3. What about Android things and pure Android compatibility? It’s necessary that Android things be installed in the BPI or with a pure Android will be enought?

I am sorry if I am doing simple questions but isn’t exists any documentation in the forum nor in the BPI wiki, Gibhub or Gitbook so any support Will be helpfull.

edit: More libraries: The Pi4J Project (Java API for accessing GPIO, PWM, I2C, SPI, UART)

Pls, support, thanks in advance!! Ana

Sorry to go off topic but you mentioned android on the m2 do you have a copy of the Android 4.4 LCD you could share

Hello friend, of course!

Here is the source: https://dev.banana-pi.org.cn/Source_Code/BPI-M2/ And here how to get the image.iso from the source: https://bananapi.gitbooks.io/bpi-m2/content/en/howtobuildandroid442imageforbpim2.html

Talking again about gpio,

Can anyone say me where can I get real working examples for control the gpios under Android Studio (Could be by Shell commands under JAVA, C/C++ or JAVA) or something usefull to can develop an Android app for this purpose??

Hello, could you please show the log when you use Runtime.getRuntime.exec()?

could you please try these code?

String gpio_number = "50"; /* this gpio is an example gpio pin */
String level = "1"; /* 0 is low level, 1 is high level */
String exportPath = null;
String directionPath = null;
String valuePath = null;
Process process = null; 
DataOutputStream dos = null;


exportPath = "echo " + gpio_number + " > /sys/class/gpio/export";
directionPath = "echo out > " + " /sys/class/gpio/gpio" + gpio_number + "/direction";
valuePath ="echo " + level + " > /sys/class/gpio/gpio" + gpio_number + "/value";

process = Runtime.getRuntime().exec("su");
dos = new DataOutputStream(process.getOutputStream());
dos.writeBytes(exportPath+"\n");
dos.flush();
dos.writeBytes(directionPath+"\n");
dos.flush();
dos.writeBytes(valuePath +"\n");
dos.flush();
dos.close();

I did not try this by myself, but I wish it could help you.

1 Like

Hello Jackzeng, thanks very much for your very good support. I just added the code to Studio, the first time, some commands and syntax aren’t recognized because own programing errors (edited post, deleted wrong info). When I fixed them, voilá!!! :slight_smile: your codes run perfect in BPI-M2, I am able to turn on/off the onboard led.

  • I still need to test in BPI-M64, that’s my target. I suppose that will be less easy than the old M2 (production stoped), I will report when I try it :wink:

This is Jackzengs code modified to turn on the onboard led in BPI-M2:

String gpio_number = “PG10”; String level = “1”; String valuePath = null; Process process = null; DataOutputStream dos = null;

valuePath ="echo " + level + " > /sys/class/gpio_sw/" + gpio_number + "/data";

            process = Runtime.getRuntime().exec("su");
            dos = new DataOutputStream(process.getOutputStream());
            dos.writeBytes(valuePath +"\n");
            dos.flush();
            dos.close();
1 Like

Hello, I am sorry but still I can not get working my M64 with GPIO in Android Studio. I used the same method than I used in the last post, but changing the gpio number to PL8 (pin number 40 of the gpio header). As can be seen in bananapi.gitbooks, theorically, the M64 Android 6.0 supports root and gpio since the V1, but ‘su’ is not working. https://bananapi.gitbooks.io/bpi-m64/content/en/androidsoftware.html

The gpio folder don’t exist, it looks that the gpios are exported to gpio_sw folder and that the gpio folder is deleted. (Can’t be done export)

When I tried doing ‘su’ in Android Shell terminal directly in M64,without PC, through Shell terminal app, appears this error:

su: setgid failed: Operation not permited

Also I tried doing setenforce 0, and I get this error:

setenforce: Couldn’t set enforcing status to ‘0’: Permission denied

I think that it’s because this image isn’t rooted as says the gitbook link. I tried the kingroot and kingoroot apps without any luck, I am looking for supersu, looks that I need to install a custom recovery from the recovery mode in the M64 Android 6.0, but I can’t access to the recovery mode (Or I don’t know accesing, I tried through adb). I am using this command:

adb reboot recovery

Then the M64 reboots but also it freezes in the initial logo, before the bootanimation appears and Android never starts until I press the power off/on. What is the correct procedure? the custom recovery should be in a SD card?

How can I install supersu properly or at least, how to Access to recovery mode?