Skip to content

Most visited

Recently visited


Native PIO

In this document

  1. Creating a client
  2. Polling

Related samples

  1. Native PIO sample

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.

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

This site uses cookies to store your preferences for site-specific language and display options.

Get the latest Android developer news and tips that will help you find success on Google Play.

* Required Fields


Follow Google Developers on WeChat

Browse this site in ?

You requested a page in , but your language preference for this site is .

Would you like to change your language preference and browse this site in ? If you want to change your language preference later, use the language menu at the bottom of each page.

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.

Take a short survey?
Help us improve the Android developer experience. (Dec 2017 Android Platform & Tools Survey)