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.
Join the DZone community and get the full member experience.
Join For FreeThe 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:
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");
blueIO.setDirection(Gpio.DIRECTION_OUT_INITIALLY_LOW);
greenIO = manager.openGpio("BCM6");
greenIO.setDirection(Gpio.DIRECTION_OUT_INITIALLY_HIGH);
redIO = manager.openGpio("BCM13");
redIO.setDirection(Gpio.DIRECTION_OUT_INITIALLY_LOW);
redIO.setValue(false);
blueIO.setValue(false);
greenIO.setValue(false);
}
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"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Switch android:text="Red"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/switchRed"
android:layout_marginTop="20dp"/>
<Switch android:text="Green"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/switchGreen"
android:layout_marginTop="20dp"/>
<Switch android:text="Blue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/switchBlue"
android:layout_marginTop="20dp"/>
</LinearLayout>
In the onCreate
method, the app sets the layout:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
..}
And to handle user switch:
Switch switchRed = (Switch) findViewById(R.id.switchRed);
switchRed.setOnCheckedChangeListener(
new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
try {
redIO.setValue(!isChecked);
}
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:
<intent-filter>
<category android:name="android.intent.category.IOT_LAUNCHER"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
Conclusions
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.
Published at DZone with permission of Francesco Azzola, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Trending
-
Writing a Vector Database in a Week in Rust
-
Performance Comparison — Thread Pool vs. Virtual Threads (Project Loom) In Spring Boot Applications
-
How To Approach Java, Databases, and SQL [Video]
-
Part 3 of My OCP Journey: Practical Tips and Examples
Comments