We believe there is something unique at every business that will ignite the fuse of innovation.

Android Things

If you have not yet heard about it, Android Things is Google’s take on bringing the Android OS to embedded devices. Android Things is currently on release Developer Preview 4, and already supports six different devices. In addition to the Android Things Support Library as well as a majority of the core Android framework, apps built for these embedded devices can also access a subset of Google APIs for Android, including a subset of Firebase components. Firebase integration with Android Things apps opens up a lot of potential for communication between embedded devices and phones/tablets. To explore this potential I built out a “Home Lighting System”, more specifically a breadboard with 3 LEDs, controlled by a Raspberry Pi. The 3 LEDs are controlled by an Android Things app that is synced to a Firebase Realtime Database and also a companion phone/tablet app that is able to control turning the lights off and on by updating values in the same Firebase database. All of the code for this blog is available on Github if you wish to follow along. The code is broken into two modules, the Android Things app and the companion app for phones/tablets.

Step 0: Setup Android Things

To create an Android Things app you first need to flash Android Things onto your chosen device. Google provides great steps for doing just this, which can be found under the Get Started link for your chosen device. Once you’ve setup Android Things on your device and connect to it over ADB you can get started on building an Android Things app.

Step 1: Wiring the "Home Lighting System"

Before we start development for our lighting system app, we need to get the lights themselves wired up. Our lighting system is comprised of three LEDs (one red, one blue, and one green) that are connected to a Raspberry Pi. Each LED is connected to a separate GPIO pin on the Raspberry Pi, allowing the Android Things app to control the LEDs through the Peripheral I/O GPIO API (more on this later). Below is the wiring for the breadboard and Raspberry Pi.

Wiring setup

Step 2: Creating the Android Things app

The next step is create an Android Things application to sync to the state of our Firebase database and then control the state of the LEDs through the GPIO APIs. If you’re using the Android Studio 3.0 preview, you can easily create an Android Things project through File > New Project and selecting Android Things as your target device -- this will automatically setup your dependencies for an Android Things app, otherwise you can follow the detailed steps from Google. Adding Firebase to an Android Things app is the same as adding it to any other Android app, detailed steps can be found here. Also, the Firebase database rules must be configured to allow for reading and writing from the Android Things app, detailed steps can be found here (see “Configure database rules”).

Once the project is set up, we can start creating the app. We need an easy way to interact with our LEDs -- to connect to them, turn them on/off, and to close our connection when the app is closed. To facilitate this I wrote a very basic driver class, SimpleLed, that will handle opening the GPIO pins for the LEDs, and handles setting the GPIO values to turn the LEDs on and off. The GPIO pins are opened and controlled through the Android Things PeripheralManagerService, and we open the specified GPIO pin for each LED. Once the GPIO pin has been opened, we can control turning the LED on and off by using the setValue(boolean) method. Now, all that is needed is to create three SimpleLed objects with our GPIO pins for our respective LEDs, and we then have control over turning them on and off. If the we create the SimpleLeds in our Activity’s onCreate(), then we must be sure to close() them in our Activity’s onDestroy(). Now we just have to observe changes on our Firebase database for the on/off value for each of our LEDs. To accomplish this, I created the LedValueEventListener that takes in a SimpleLed and sets the on/off value to the Boolean value that is being observed from the DataSnapshot. Attach each LED to the appropriate Firebase reference with a LedValueEventListener, and we can now control if the LED is turned on or off based on our Firebase database. The JSON structure for the Firebase database used in the sample can be found below.

  "lighting_system" : {
    "blue_on" : false,
    "green_on" : false,
    "red_on" : false

With a SimpleLed and LedValueEventListener associated with each of our GPIO pins on the breadboard, we can now control our Home Lighting System by updating the on values to true/false through the Firebase console. However, that is quite a cumbersome way to control a lighting system, it would be much better to be able to control the lights from a phone or a tablet!

Step 3: Creating the Companion App

The companion app for our Home Lighting System is rather simple. It has three Switches to control turning each individual light on and off, as well as one button to turn all the lights on and one button to turn all the lights off. Similar to our LedValueEventListener used in the Android Things app, there is a SwitchValueEventListener that updates the checked value of a provided Switch based off the Boolean value from a DataSnapshot. The companion app will also need to be added to our Firebase instance so that we can interact with the database. Once everything is hooked up we can control our lights from anywhere we have a data connection.

Demo gif


Android Things is an exciting new addition to the Android ecosystem and allows easy integration with embedded devices and easy interaction with Android phones and Google APIs, like Firebase. While this sample is a rather simple implementation of the possibilities for an Android Things app, it serves as a showcase for the vast array of possibilities when using these technologies.

About the Author

Alex Townsend

Alex Townsend is a Senior Consultant at CapTech, based in Washington, DC. Alex has a passion for Android application development and architecture, focusing on bringing the latest and greatest technologies and patterns to his work.