Native PIO

The native Peripheral I/O (PIO) APIs let you write C/C++ code to control GPIO, PWM, I2C, SPI and UART peripherals that access the same underlying peripheral service as the standard PIO APIs. These APIs utilize the Android Native Development Kit.

Adding the required permission

Add the required permission for this API to your app's manifest file:

<uses-permission android:name="" />

Creating a client

The APeripheralManagerClient class is defined in the Android Things native library. Use this class to create a client to communicate with the manager for each type of interface (for example, GPIO). The client sets up the connection to the interface:

APeripheralManagerClient* client = APeripheralManagerClient_new();
ASSERT(client, "failed to open peripheral manager client");
AGpio* gpio;
int openResult = APeripheralManagerClient_openGpio(client, LED_GPIO, &gpio);
ASSERT(openResult == 0, "failed to open GPIO: %s", LED_GPIO);
int setDirectionResult = AGpio_setDirection(gpio, AGPIO_DIRECTION_OUT_INITIALLY_LOW);
ASSERT(setDirectionResult == 0, "failed to set direction for GPIO: %s", LED_GPIO);

When the app is destroyed, be sure to delete the connection and the client:



While the app is alive, poll for received values (or set output values):

while (!app->destroyRequested) {
    int gpioValue;
    if (AGpio_getValue(gpio, &gpioValue) != 0) {
        LOGE("failed to get value for GPIO: %s", LED_GPIO);
        continue; // retry immediately
    if (AGpio_setValue(gpio, !gpioValue) != 0) {
        LOGE("failed to set value for GPIO: %s", LED_GPIO);
        continue; // retry immediately