GPIO
Control the general-purpose pins. Outputs are driven with a broadcast; reads come back synchronously from a device file.
The pin is selected by a two-character string code, not a pin number argument. This is the least obvious part of the API, so read the encoding below before you write anything.
Set a pin as output (and drive it)
public static void setGpioOut(Context context)
The pin and level are chosen by the "value" the broadcast carries. The code is
two characters: first digit = GPIO index, second digit = level.
value | Meaning |
|---|---|
"00" | GPIO0 → output LOW |
"01" | GPIO0 → output HIGH |
"10" | GPIO1 → output LOW |
"11" | GPIO1 → output HIGH |
"20" | GPIO2 → output LOW |
"21" | GPIO2 → output HIGH |
The setGpioOut(context) helper drives GPIO0 low. To target a different pin or
drive it high, send the broadcast directly with the code you need:
Intent intent = new Intent("android.intent.item.gpio.a0.set");
intent.putExtra("value", "11"); // GPIO1 → HIGH
context.sendBroadcast(intent);
The parameterised helper form is being finalised — until then, use the direct broadcast above for per-pin control.
Switch a pin to input
public static void setGpioInput(Context context)
Same encoding, but the level digit is unused — only the GPIO index matters.
value | Meaning |
|---|---|
"00" | GPIO0 → input |
"10" | GPIO1 → input |
"20" | GPIO2 → input |
For a pin other than GPIO0, send the direction broadcast directly:
Intent intent = new Intent("android.intent.item.gpio.a0.direction");
intent.putExtra("value", "10"); // GPIO1 → input
context.sendBroadcast(intent);
Read a pin
Unlike everything else in this API, the getters return a value immediately.
public static String getGpio0() // "0", "1", or "getGpio0 failed!"
public static String getGpio1()
public static String getGpio2()
String level = ItempApiPub.getGpio0();
if ("1".equals(level)) {
// pin is high
}