How to integrate Android Things with Firebase: Firebase IoT tutorial – Practical guide

This practical guide about Firebase IoT, describes how to integrate Android Things with Firebase. To accomplish this integration, we will build a project that controls an RGB LED remotely in real time. In more details, through this Firebase IoT tutorial, we will describe all the steps necessary to integrate Android Things with Firebase. The final goal is controlling the RGB LED connected to Android Things using Firebase console, so that changing one or more values in the Firebase console will be reflected in RGB LED color change. This project demonstrates, moreover, how it is possible to exchange real-time data between Android Things and Firebase.

As you may already know Android Things is a branch of Android OS therefore Android Things supports Firebase out of the box.  Moreover, using this project, we can explain how to use Firebase in IoT projects.

In this tutorial, we assume you are already familiar with Android Things OS and you have already installed it. If this is the first time you use Android Things you can read how to install the Android Things OS on your board. This Android Things project can be further extended so that we can integrate Android Things with Firebase exchanging different types of information.  There are several scenarios where the synchronization between Android Things and Firebase plays an important role so it is important to know how to do it.

Before diving into this Android Things Firebase integration project it is useful to clarify which components we will use:

  • Raspberry Pi 3 (or an Android Things compatible board)
  • a common anode RGB LED

Moreover, you need a Firebase account so that you can test this Android Things IoT project.


How to integrate Android Things with Firebase Project overview

Now we know the components we will use in this project, it is useful to have an overview of this synchronization project so that you can have a clear overview. The picture below describes how the project works:

Integrate Android Things with Firebase

Once we have connected Android Things to Firebase whenever we change a value in the Firebase database, the new value triggers an event so that the synchronization between Android Things and Firebase database takes place transferring the new value to the Android Things board that in turn set the RGB LED color.

How to setup Firebase project

In this section, we will describe how to configure a Firebase project so that we can integrate it with Android Things IoT app. The first step is creating a free account. Once you have your free account you can create a new Firebase project as shown below:


androidthings firebase


The next step is configuring your app:


android things firebase project


During this configuration process, you have to follow the Firebase instructions. Finally, before interacting with Firebase it is necessary to modify the security aspects:


firebase rules


Finally, we can configure our database that will hold the LED colors:


androidthings firebase add item



At app level, the build.gradle looks like:

[xml] ……
dependencies {
provided ‘<latest available>’
compile ‘<latest available>’
compile ‘<latest available>’
apply plugin: ‘’


How to integrate Android Things with Firebase IoT

After you have followed the instruction during the Firebase project setup you are ready to start coding the Android Things IoT app. All the Firebase dependencies and configuration are now ready and we can dive into the details about integrating Android Things with Firebase. As described previously, in this Firebase IoT step by step guide, the Android Things app has to listen to the Firebase value changes and react to controlling the RGB LED.

The RGB LED has 3 pins one for each color and we connect them to the Raspberry GPIO pins. According to the value in the Firebase database, the Android Things app turns on or off each GPIO pins controlling, in this way, the LED color. This app does not have a UI because the Android Things is controlled remotely.

In order to create this Android IoT app, it is necessary to create a new Android Things project using Android Studio. If you are new and this is the first time you approach the Android Things you can follow this tutorial describing how to get started with Android Things.

In the previous paragraph, we have created a table in the Firebase database containing three different fields representing the LED colors. In order to hold the Firebase new values, we have to create a simple POJO class that represents the table:

[java] public class RGBColor {

private int red;
private int green;
private int blue;

public RGBColor() {}

public int getRed() {
return red;

public void setRed(int red) { = red;

public int getGreen() {
return green;

public void setGreen(int green) { = green;

public int getBlue() {
return blue;

public void setBlue(int blue) { = blue;

We will use this simple class to control the GPIO pins.

Now, we can focus our attention on the main activity that will handle all the synchronization aspects, so that changes in Firebase will be reflected in Android Things. To this purpose, you can use the template Activity already configured in the project. In onCreate(), we have to initialize the connection with Firebase and get the reference to the PeripheralManagerService:

[java] @Override
protected void onCreate(Bundle savedInstanceState) {
Log.d(TAG, “onCreate”);
databaseRef = FirebaseDatabase.getInstance().getReference();
pms = new PeripheralManagerService();

where databaseRef is an instance of DatabaseReference. Moreover, the app adds a listener to the databaseRef to receive notification when values change. Moreover, in this method we initialize the pins that the app uses to control the LED:

[java] private void initPin() {
try {
redPin = pms.openGpio(“BCM26”);
greenPin = pms.openGpio(“BCM6”);
bluePin = pms.openGpio(“BCM5”);
catch (IOException ioe) {
Log.e(TAG, “Unable to open pins”);

It is important to notice, that in the code above, the Android Things IoT app references the pin names directly. This is not a best practice if you want that your app
is portable on different boards. Anyway, for this tutorial, we can use the pin direct access because we are mainly focused on how to integrate Android Thing with Firebase.

Synchronizing Android Things with Firebase

Once the “hardware” part is ready and the pins are initialized, it is necessary to receive notification when Firebase values change. To this purpose, it is necessary to define a listener:

[sociallocker] [java] private ValueEventListener veListener = new ValueEventListener() {
public void onDataChange(DataSnapshot dataSnapshot) {
RGBColor color = dataSnapshot.getValue(RGBColor.class);

// update pins
updatePin(redPin, color.getRed());
updatePin(greenPin, color.getGreen());
updatePin(bluePin, color.getBlue());

public void onCancelled(DatabaseError databaseError) {}
[/java] [/sociallocker]

The code above is really simple, the onDataChange method is invoked when one or more values in the database are changed. The class RGBColor, described previously, holds the new values. The last step is updating the pin setting the right color:

[java] private void updatePin(Gpio pin, int value) {
try {
pin.setValue( value > 0 ? false : true);
} catch (IOException e) {

In this example, we suppose that if the value is more than zero then the PIN is on otherwise if off. Notice that we are using a common cathode LED, for this reason, we turn off the pin.

Now you can play using the Firebase console changing the RGB values and notice that the LED change color.


At the end of this article, you gained the knowledge about how to integrate Android Things with Firebase so that we can control in real time the Android Things board remotely. Once you have learned how to synchronize Android Things with Firebase, you can further extend this project and implement new features to control other kinds of peripherals. During this tutorial, you have explored how to use Firebase in IoT projects and how to configure Firebase so that we can synchronize it with an IoT device like an Android Things compatible boards.

    1. Anonymous March 21, 2019

    Add Your Comment