Skip to content

Instantly share code, notes, and snippets.

@rosterloh
Created August 18, 2014 13:40
Show Gist options
  • Save rosterloh/c4bd02bed8c5e7bd47c5 to your computer and use it in GitHub Desktop.
Save rosterloh/c4bd02bed8c5e7bd47c5 to your computer and use it in GitHub Desktop.
Android class for GPIO control
public class GPIO {
public String port;
public int pin;
//get direction of gpio
public String getInOut()
{
String command = String.format("cat /sys/class/gpio/%s/direction",this.port);
try {
Process p = Runtime.getRuntime().exec(new String[] {"su", "-c", command});
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
StringBuilder text = new StringBuilder();
String line;
while((line = reader.readLine()) != null){
text.append(line);
text.append("\n");
}
String retour= text.toString();
return retour;
} catch (IOException e) {
return "";
}
}
// get state of gpio for input and output
//test if gpio is configurate
public int getState()
{
String command = String.format("cat /sys/class/gpio/%s/value",this.port);
try {
Process p = Runtime.getRuntime().exec(new String[] {"su", "-c", command});
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
StringBuilder text = new StringBuilder();
String line;
while((line = reader.readLine()) != null){
text.append(line);
text.append("\n");
}
try {
String retour= text.toString();
if(retour.equals("")){
return -1;
} else {
return Integer.parseInt(retour.substring(0, 1));
}
} catch(NumberFormatException nfe) {
return -1;
}
} catch (IOException e) {
return -1;
}
}
//set value of the output
public boolean setState(int value){
String command = String.format("echo %d > /sys/class/gpio/%s/value", value,this.port);
try {
String[] test = new String[] {"su", "-c", command};
Runtime.getRuntime().exec(test);
return true;
} catch (IOException e) {
return false;
}
}
// set direction
public boolean setInOut(String direction){
String command = String.format("echo %s > /sys/class/gpio/%s/direction", direction,this.port);
try {
Runtime.getRuntime().exec(new String[] {"su", "-c", command});
return true;
} catch (IOException e) {
return false;
}
}
//export gpio
public boolean activationPin(){
String command = String.format("echo %d > /sys/class/gpio/export", this.pin);
try {
Runtime.getRuntime().exec(new String[] {"su", "-c", command});
return true;
} catch (IOException e) {
return false;
}
}
// unexport gpio
public boolean desactivationPin(){
String command = String.format("echo %d > /sys/class/gpio/unexport", this.pin);
try {
Runtime.getRuntime().exec(new String[] {"su", "-c", command});
return true;
} catch (IOException e) {
return false;
}
}
//init the pin
public int initPin(String direction){
int retour=0;
boolean ret=true;
// see if gpio is already set
retour=getState();
if (retour==-1) {
// unexport the gpio
ret=desactivationPin();
if(ret==false){ retour=-1; }
//export the gpio
ret=activationPin();
if(ret==false){ retour=-2; }
}
// get If gpio direction is define
String ret2 = getInOut();
if (!ret2.contains(direction))
{
// set the direction (in or out)
ret=setInOut(direction);
if(ret==false){ retour=-3; }
}
return retour;
}
//Constructor
public GPIO( int pin){
this.port = "gpio"+pin;
this.pin = pin;
}
}
@cavenaghi9
Copy link

Is it workable for newest Android version? I tried on Andorid 6.0, and application doesn't have any permission for /sys folder, although I have assigned 777 with this folder.

@usantos
Copy link

usantos commented Feb 7, 2019

Is it workable for newest Android version? I tried on Andorid 6.0, and application doesn't have any permission for /sys folder, although I have assigned 777 with this folder.

Do you is a root?

@gullz
Copy link

gullz commented Mar 29, 2019

Any copyright you think we can include in the class if this is copied? ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment