Android Things – Capacitive Touch Buttons, GPIO on the Rainbow Hat

This blog will show you how to use the capacitive touchbuttons of the Rainbow hat peripheral. In learning about the capacitive buttons we will use the GPIO protocol which can come in handy for numerous other sensors and peripherals.

First we create an instance of PeripheralManagerService. This is the AndroidThings SDK class that allows us to open connections to different board pins using the different available protocols.

13;
PeripheralManagerService service = new PeripheralManagerService();13;

Using this instance, we want to open a GPIO connection with the pin address of the capacitive A button. This can be found here. When we open this connection we are given back a GPIO bus variable that will now allow us to communicate using the AndroidThings SDK GPIO class.

Opening a connection to a pin is capturing this pin as your resource, and like with all resource management it is good practice to release resources once you have finished with them. Also if we have an open connection to a pin, no other code can open a connection to that same pin until we close our connection.

Since we opened the connection to our pin in the onCreate method, we should use the symmetrically matching pair from the Android Lifecycle to close the connection. Therefore we close the GPIO bus connection in onDestroy using this code.

Once we have a connection to the GPIO bus, we now have to configure our pin for GPIO input from a button.

We set the direction to IN. This means we are telling the GPIO bus that we expect to use this pin as an input. I.e. we expect to receive information from the peripheral on this pin. (This information can be found in documentation or datasheets like this)

We set the active type to LOW using the below. Active type means the “on” state, when the button is pressed this equates to the pin being active. Describing active type as low means that when there is a low, near zero voltage the GPIO bus value will return true. You’ll see this return of true next when we listen for input.

When using a GPIO pin as an input you can register a callback for state changes. You create the callback like below. Implementing the onGpioEdge method. If you only want a single callback you return false from this method, however we want to notified every time the button is pressed so we return true.

The onGpioEdge method is passed the GPIO bus as a parameter, we can use this parameter to determine the current state of the gpio peripheral. When onGpioEdge is called and the GPIO’s getValue method returns false, this means the button has been released.

Let’s now use the callback we have created and register our interest with the Android Things GPIO SDK class.

Add this code to your onStart method to register the callback. We are using the onStart method as we want to be notified of button presses when our Activity is in the foreground only. This is slightly less relevant on AndroidThings as only one application should ever be installed, but it’s still a best practice to future proof your code.

The setEdgeTriggerType method is how we configure what callbacks we want to be notified of. Using the EDGE_BOTH parameter means we will be notified when the button is pressed and also when the button is released. Relating this back to the ActiveType discussion, we will be notified when the peripheral enters the active state and when it exits the active state.

Finally since we registered the callback in the onStart method, we should use the symmetrically matching pair from the Android Lifecycle to unregister. Therefore we unregister our callback in onStop.

Let’s run our app and check the LogCat for the messages showing our button is working.

You now understand how the Android Things GPIO communication with input peripherals works. You can interface with hardware buttons and monitor for state changes knowing when the button is pressed or not. This knowledge also transfers to other GPIO input peripherals such as other types of hardware buttons, motion detectors or light sensors.