How to build an Android IoT app using Android Things: Step by step tutorial

This post describes how to build an Android IoT app using Android Things with step by step tutorial.  The goals of this Android Things tutorial are:

  • Build a simple RGB Led controller using Android Things app
  • Build an Android Things app that uses a UI developed using Android API

As you may already know, recently Google has released a new IoT operating system called Android Things. Android Things is OS derived from Android and the interesting part is that we can re-use our Android knowledge to develop Android IoT app. Before diving into Android IoT app, it is important you know what is Android Things and how it works.

In this Android Things tutorial, that explains how to build an Android IoT app using Android Things, we will use Raspberry Pi 3 as IoT development and prototyping board, even if you can use other development boards compatible with Android Things.

If you are eager to know the final result, the video below shows the Android IoT app in action:

This Android IoT app helps you to familiarize with new Android Things APIs. Moreover, this IoT app is useful to have an overview of how to develop an Android IoT app UI. Once you have learned how to build an Android IoT project using Android Things, you can expand it further and add new features. Moreover, Android Things supports not only Leds, but other kinds of peripherals (as motors, servo and so on). This Android IoT project is the starting point to know Android Things and learn how to capitalize your Android experience and apply it to a different field related to the Internet of Things (IoT).

Getting started Android IoT app: Connecting RGB Led to Android Things

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

Android IoT app using Android Things step by step guide

The RGB Led is a common anode led, so Raspberry Pi 3 powers the pin anode. The RGB pins, that control the Led color, connects to Raspberry pins:

  • Pin 29
  • Pin 31
  • Pin 33

These pin numbers are important because later we will use them in the Android IoT app. Check twice the connections before powering Raspberry on.

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

[xml]dependencies {
  provided '<latest version>'

Android Things uses an Activity as we are used in Android. So let’s create a class called RGBThingActivity and in onCreate method, where we handle Pin communication.

How to use Android Thing to control GPIO Pins: PeripheralManagerService

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 GPIO Pins, we turn on or turn off the three color components (Red, Green, and Blue).

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. In the beginning, the Android IoT app initializes the service and then set the pin values:

[java]try {
  // Be aware that in the latest version of Android Things SDK you have to use
  // another way to get the reference (getInstance)
  PeripheralManagerService manager = PeripheralManagerService.getInstance();
  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 new important new aspects. First of all, we have to select the right pins. If we are used to Raspberry, we know that each pin has a corresponding number. In the same way, Android Things uses the same addressing model, anyway the pins are named in a different way. Using the Raspberry pin reference you can know each pin address. The above source code uses these address names. For example, to use the Pin BCM5 (or Pin 29), the code is:

[java] blueIO = manager.openGpio("BCM5");[/java]

In the beginning, we turn all the pin to low (or off), so the Led is off.  Changing the pins values: low to high or high to low we have different led colors.

How to develop an Android IoT app UI

Another interesting feature provided by Android Things is the  UI interface. We can develop a UI interface for Android IoT app in the same way we develop Android UI. As in an Android app, the UI is in XML format. In this example, we want to control the RGB led using three switchings:

<?xml version="1.0" encoding="utf-8"?>
  <LinearLayout xmlns:android="" 
     <Switch android:text="Red" android:layout_width="wrap_content" 
     <Switch android:text="Green" 
    <Switch android:text="Blue" 

In onCreate method, the app sets the layout as we are used in Android app:

protected void onCreate(Bundle savedInstanceState) {

and to handle user switch:

[java] Switch switchRed = (Switch) findViewById(;
  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 other pins.  The final result  is below:

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

[xml]<uses-library android:name=""/>[/xml]

and then 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 know a bit better how to use Android Things. The interesting aspect is that using a few new APIs Android developers can be ready for the next technological revolution called IoT. Moreover, the development process is the same used in Android app.
Using a few code lines, an Android developer can build an Android IoT app. At the end of this tutorial, you hopefully gained the knowledge about how to develop an Android IoT app using Android Things. Furthermore, you learned how to develop an Android IoT app with UI to interact with a user. Where can you go from here? Well, there are several options and this is the first step. It is possible to build an Android IoT user interface to control other kinds of peripherals or you can use different Android Things pins (not only GPIO) to send other kinds of signals (like PWM).

    1. Mukesh Suthar January 12, 2017
      • Francesco Azzola January 12, 2017

    Add Your Comment