Building IoT app for Android Things in 3 Steps

This post describes how to build an Android IoT app for Android Things. 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.

The goals of this post are:

Build a simple RGB led controller using Android IoT app

Build an Android IoT app that uses a UI developed using Android API

We will use to this purpose Raspberry Pi 3 as IoT prototyping board. Even if you can use other development boards compatible with Android Things.

Getting started Android IoT app (Step 1)

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 IoT application controls a simple RGB led (common anode). This RGB led is connected to Raspberry using 220Ω resistor, one for each color. The schematic diagram is shown below:

The RGB led is a common anode led, so Raspberry Pi 3 powers the pin anode. The RGB pins, that control the led color, are connected 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 IoT application using Android studio. The first step is configuring the Android IoT project using build.gradle:

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

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

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. 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 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.

Android IoT app UI (Step 3)

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:

Conclusions

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 to 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.

About The Author

I'm an electronic engineer with over 15 years of
experience in computer programming. I'm the author of the book "Android things projects". I'm a MVB at DZone and DZone Guide Author. I have contributed to the IoT guide. I'm the technical reviewer of the book Internet of Things with Arduino Blueprints(published by PacktPub). I love creating IoT projects using Arduino, Raspberry Pi,
Android, and other platforms. I am interested in the convergence
between IoT and the mobile applications. I'm SCEA, SCWCD, and SCJP
certified.
Follow on: Twitter | Linkedin

Mukesh Suthar

while Deploying have you created 2 different app i.e 1 for RPI3 and other for Android? if so, then how they are communicating. Have you implemented firebase?
it would be great if you can provide the source code.

Sign up for our Newsletter

Enter your email and stay on top of things,

Get SwA updates delivered to your inbox. Article, tips, source code

×

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.