Build an IoT App Using Android Things in 3 Steps

DZone 's Guide to

Build an IoT App Using Android Things in 3 Steps

If you're an Android developer, you might want to look at the Android Things OS. Using the knowledge you already have, you can create and code connected devices.

· IoT Zone ·
Free Resource

The goals of this post are to:

  • Build a simple RGB LED controller.
  • Build an Android IoT app that uses a UI developed using the Android API.

We will use a Raspberry Pi 3 as our IoT prototyping board for this experiment, but you can use any other development board, so long as it's compatible with Android Things.

If you just want to see the end result, the video below shows the Android IoT app in action:

Getting Started (Step 1)

Usually, an IoT project has two sides: an electric/electronic side and a software side. To keep things simple so that we can focus on the Android IoT app, this IoT application controls a simple RGB LED (common anode). This RGB LED is connected to a Raspberry Pi using a 220Ω resistor, one for each color. The schematic diagram is shown below:

Image title

The RGB LED is a common anode LED, so the Raspberry Pi 3 powers the pin anode. The RGB pins, which control the LED color, are connected to:

  • Pin 29
  • Pin 31
  • Pin 33

These pin numbers are important because, later, we will use them in the Android IoT app. Double check the connections before powering on your Pi.

Now it is time to create the IoT application using Android Studio. The first step is configuring the Android IoT project using build.gradle:

dependencies {
    provided 'com.google.android.things:androidthings:0.1-devpreview'

Android Things uses Activity, just like we're used to in Android. So let’s create a class called RGBThingActivity. In the onCreate method, we handle pin communication.

Android Thing PeripherManagerService (Step 2)

To handle the communication to the RGB LED, we use GPIO pins. GPIO pins use a programmable interface to read the device status or to set the output value (High or Low). Using Raspberry Pi GPIO pins, we turn on or off the three color components (red, green, and blue).

The Android Things SDK provides a service called PeripheralManagerService to abstract the GPIO communication interface. We have to use it every time we want to read or write data. At the beginning, the Android IoT app initializes the service and then sets the pin values:

try {
    PeripheralManagerService manager = new PeripheralManagerService();
    blueIO = manager.openGpio("BCM5");
    greenIO = manager.openGpio("BCM6");
    redIO = manager.openGpio("BCM13");
catch (IOException e) {
    Log.w(TAG, "Unable to access GPIO", e);

This piece of code introduces some important new aspects. First of all, we have to select the right pins. If you are used to Raspberry Pis, you know that each pin has a corresponding number. In the same way, Android Things uses the same addressing model, though the pins are named in a different way. Using the Raspberry pin reference, you can know each pin address. These address names are used in the code above. For example, to use the pin BCM5 (or Pin 29), the code is:

    blueIO = manager.openGpio("BCM5");

At the beginning, we turn all the pins to low (or off), so the LED is off. Changing the pins values from low to high or high to low is how we introduce different LED colors.

Android IoT app UI (Step 3)

Another interesting feature provided by Android Things is the UI interface. We can develop a UI interface for our Android IoT app in the same way we develop any other Android UI. Like with an Android app, the UI is in the XML format. In this example, we want to control the RGB LED using three switches:

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    <Switch android:text="Red" 

    <Switch android:text="Green" 

    <Switch android:text="Blue" 


In the onCreate method, the app sets the layout:

protected void onCreate(Bundle savedInstanceState) {

And to handle user switch:

Switch switchRed = (Switch) findViewById(R.id.switchRed);
new CompoundButton.OnCheckedChangeListener() {
    public void onCheckedChanged(CompoundButton buttonView, 
    boolean isChecked) {
        try {
        catch (IOException e) {
            Log.w(TAG, "Red GPIO Error", e);

You have to repeat the same piece of code for the other pins. The final result is below:

The last aspect we need to consider is the Manifest.xml. To use our app, we have to add this bit inside the application tag:

<uses-library android:name="com.google.android.things"/>

Then, we declare that our Activity is an IoT activity, and it starts at boot:

    <category android:name="android.intent.category.IOT_LAUNCHER"/>
    <category android:name="android.intent.category.DEFAULT"/>


At the end of this post, you hopefully know how to use Android Things a bit better. The interesting aspect is that, using a few new APIs, Android developers can be ready to participate in the next technological revolution — IoT. Moreover, the development process is the same one you're used to when building an Android app. Using a few code lines, an Android developer can build an Android IoT app.

android studio, android things, iot, iot app development, raspberry pi 3, tutorial

Published at DZone with permission of Francesco Azzola , DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}