Skip to content

Latest commit

 

History

History

button

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 

Button driver for Android Things

This driver supports GPIO buttons.

NOTE: these drivers are not production-ready. They are offered as sample implementations of Android Things user space drivers for common peripherals as part of the Developer Preview release. There is no guarantee of correctness, completeness or robustness.

How to use the driver

Gradle dependency

To use the button driver, simply add the line below to your project's build.gradle, where <version> matches the last version of the driver available on jcenter.

dependencies {
    compile 'com.google.android.things.contrib:driver-button:<version>'
}

Sample usage

import com.google.android.things.contrib.driver.button.Button;

// Access the Button and listen for events:

Button mButton;

try {
    mButton = new Button(gpioPinName,
            // high signal indicates the button is pressed
            // use with a pull-down resistor
            Button.LogicState.PRESSED_WHEN_HIGH
    );
    mButton.setOnButtonEventListener(new OnButtonEventListener() {
        @Override
        public void onButtonEvent(Button button, boolean pressed) {
            // do something awesome
        }
    });
} catch (IOException e) {
    // couldn't configure the button...
}

// Close the Button when finished:

try {
    mButton.close();
} catch (IOException e) {
    // error closing button
}

Alternatively, you can register a ButtonInputDriver with the system and receive KeyEvents through the standard Android APIs:

ButtonInputDriver mInputDriver;

try {
    mInputDriver = new ButtonInputDriver(gpioPinName,
            Button.LogicState.PRESSED_WHEN_HIGH,
            KeyEvent.KEYCODE_A // the keycode to send
    );
    mInputDriver.register();
} catch (IOException e) {
    // error configuring button...
}

// Override key event callbacks in your Activity:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_A) {
        // do something awesome
        return true; // indicate we handled the event
    }
    return super.onKeyDown(keyCode, event);
}

// Unregister and close the input driver when finished:

mInputDriver.unregister();
try {
    mInputDriver.close();
} catch (IOException e) {
    // error closing input driver
}

License

Copyright 2016 Google Inc.

Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.