Skip to content

Most visited

Recently visited

navigation

Integrate Peripheral Drivers

This lesson teaches you to

  1. Integrate Driver Libraries
  2. Register Framework Drivers

Try it out

Get up and running quickly with pre-built drivers from the Peripheral Driver Library. These drivers abstract the low-level communication details associated with many common hardware peripherals.

In this lesson, you will learn to integrate library drivers into your app and bind peripherals to the Android framework through the UserDriverManager.

Initialize the driver library

To import a library driver into your app:

  1. Search the driver index for a driver that matches your peripheral.
  2. Add the driver artifact dependency to your app-level build.gradle file:

    dependencies {
        ...
        compile 'com.google.android.things.contrib:driver-button:0.1'
    }
    
  3. Initialize the driver class with the appropriate Peripheral I/O resources.

  4. When your app no longer needs the resource, close the connection.

    import com.google.android.things.contrib.driver.button.Button;
    import com.google.android.things.contrib.driver.button.ButtonInputDriver;
    ...
    
    public class ButtonActivity extends Activity {
        private static final String TAG = "ButtonActivity";
        private static final String GPIO_PIN_NAME = ...;
    
        private ButtonInputDriver mButtonInputDriver;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            try {
                // Step 3. Initialize button driver with selected GPIO pin
                mButtonInputDriver = new ButtonInputDriver(
                        GPIO_PIN_NAME,
                        Button.LogicState.PRESSED_WHEN_LOW,
                        KeyEvent.KEYCODE_SPACE);
            } catch (IOException e) {
                Log.e(TAG, "Error configuring GPIO pin", e);
            }
        }
    
        @Override
        protected void onDestroy(){
            super.onDestroy();
    
            // Step 5. Close the driver and unregister
            if (mButtonInputDriver != null) {
                try {
                    mButtonInputDriver.close();
                } catch (IOException e) {
                    Log.e(TAG, "Error closing Button driver", e);
                }
            }
        }
    }
    

Bind to the framework

The driver library includes user drivers for supported peripheral types. When these drivers are available, they allow your app to interact with the standard Android framework APIs instead of the Peripheral I/O APIs.

The following code registers a ButtonInputDriver peripheral as a key input with the framework. The driver will generate key events using the provided key code each time the button is pressed or released.

import com.google.android.things.userdriver.InputDriver;
import com.google.android.things.userdriver.UserDriverManager;
...

public class ButtonActivity extends Activity {
    private static final String TAG = "ButtonActivity";
    ...

    private ButtonInputDriver mButtonInputDriver;

    ...

    @Override
    protected void onStart() {
        super.onStart();
        mButtonInputDriver.register();
    }

    @Override
    protected void onStop() {
        super.onStop();
        mButtonInputDriver.unregister();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_ESCAPE) {
            // Handle button pressed event
            return true;
        }

        return super.onKeyDown(keyCode, event);
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_ESCAPE) {
            // Handle button released event
            return true;
        }

        return super.onKeyUp(keyCode, event);
    }
}
This site uses cookies to store your preferences for site-specific language and display options.

Hooray!

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 one-minute survey?
Help us improve Android tools and documentation.