Updated date:

Getting Started With Android Things Using Raspberry Pi

Davinder Singh is a Polymath. He loves reading, learning and writing technical cookbooks about new technologies.

Google LLC [Public domain], via Wikimedia Commons

Google LLC [Public domain], via Wikimedia Commons

What Is Android Things?

Android Things is an Operating system (OS) developed by Google for embedded devices such as low powered microcontrollers. The operating system is a special piece of system software that manages the hardware and handles all the dirty work for you so that you can focus on more important things.

Why You Should Try It?

Android Things unlike other embedded operating systems, is powered by Google's Android operating system which runs on millions of devices. And hence comes with security, stability and API support from Google. If you have never worked in the field of IoT before but you have plenty of experience developing Android applications then you can get started right away. Android Things also comes with a low-level hardware API which gives you full control of your hardware like you used to have before. And lastly, because it is fun and you can do lots of awesome things with it with very little effort.

Before Continuing

  • I am assuming that you have Raspberry Pi or compatible board
  • A computer (mac, PC or Linux) with stable internet connection
  • Micro-USB cable
  • Ethernet cable
  • MicroSD card reader
  • 8 GB or larger microSD card
  • HDMI cable and HDMI-enabled display

Raspberry Pi 3 starter kit

Download and Install Android Studio

Android studio is an official integrated development environment used for developing android applications. We will use this to write programs for android things and burn them to the board (burning is a metaphor for installing). Here you will find all the instructions and links required to set up Android Studio on your computer.

Setting up Android Things

You have two options to install Android Things operating system. Either you can use the prebuilt package provided by Google or you can create your custom package. In this article, we will use a custom image for our raspberry pi.

  1. Head to the Google Developer console by clicking here.
  2. Sign in using your Gmail credentials.
You will be greeted by following dialog, Accept the license and continue,

You will be greeted by following dialog, Accept the license and continue,

Creating a New Android Things OS

After clicking continue, you will be greeted to Android Things. Here you will have to create a new product. The product is like a project. For every project, you can create a unique product having unique features to handle the job at hand. Click create and then give your product a unique name (I'll name my product Hello World) and in SOM select Raspberry Pi 3.


Create a product

Create a product

Add a Model

Every product consists of several Models. Models help to unify the whole process of maintaining and updating the product. Android Things has Over The Air (OTA) update systems hence you can push new features and improvements to all the devices on a single go.

Create a new model

Create a new model

Let's Configure the Build

After a model has been created,

  1. Click on it (model). A new window will open.
  2. Click the new button and then select Start from scratch.
  3. Now give a name to your operating system (your operating system exciting).
  4. Keep on clicking on next, leaving the rest of the settings as it is till you reach the partition section.
Configure the build

Configure the build

Configure Partition

Here you are presented with the option to configure the partition of your final product. You get to choose what amount is to be left for application use. 4096 MB (4 GB) is the minimum amount of space that can be allocated. You can configure this by adding the amount your memory card has. For example, if your memory card is 8GB you will have to enter 8 x 1024 = 8192 and so on. Click on build the process should start immediately.

Space partition

Space partition

Flashing Android things on device

1. After the build process click on Download and select development for now. Wait for Google to build your operating system. The download should start shortly. While it is being downloaded head to the Tools side menu and download the operating system flashing tool aka Setup Utility.

Android things tools

Android things tools

2. After download complete. Extract the zip file in a folder. Also, place the android-things zip file downloaded from the console. The directory should contain the following files at the moment

  • Android-Setup utility folder containing various scripts (one of them is for your OS).
  • Raspberry Pi android things zip (mine is of 331 megabytes).

Don't forget to connect raspberry pi to your PC using usb cable before continuing

3. If you are running Linux or Mac os, open bash and run the script by typing the path of the script as follows,“./android-things-setup-utility/android-things-setup-utility-linux” or in case you are running mac os, “./android-things-setup-utility/android-things-setup-utility-mac”. On windows just double click open windows executable file.

4. Enter 1 and hit enter or return key.

Recommended for you

Android Things Setup Utility

Android Things Setup Utility

5. Input 1 to select raspberry pi for, “What hardware are you using?”.

6. Now it will ask for which image you want to install. Input 2 followed by the path to zip the file that you just built from the console (containing your custom build).

7. Setup should start.

Setup Utility installing.

Setup Utility installing.

Testing the Install

Now is the time to test the installation. To test the installation we will write a simple program in Android studio to blink LED connected to one of the port of Raspberry Pi.

  • Disconnect raspberry pi from your PC.
  • Connect the short leg of the LED (the negative leg, called the cathode) to the GND (Pin 20).
  • Connect the long leg of the LED (the positive leg, called the anode) to the GPIO pin (Pin 18).
Raspberry Pi Pin configuration

Raspberry Pi Pin configuration

Let's Write Some Code

After you are done with connections. Connect the raspberry pi with your PC again. And run Android Studio.

  1. Create a new project.
  2. Uncheck use Kotlin box.
  3. Give name to the project, location and a package name (com.helloworld.iot).
  4. Now in choose your project click on Android Things and then select Android Things Empty Activity.
Android Studio –– Android things empty activity

Android Studio –– Android things empty activity

5. Give a name to the activity. and check Launch activity automatically on boot. Click next and wait for android studio to set up things for you.

Android Things –– Give name to your activity

Android Things –– Give name to your activity

6. In the editor window (MainActivity.java), Paste the following code.

Code to Blink LED

public class MainActivity extends Activity {
    private static final String TAG = "MainActivity";
    private static final int INTERVAL_BETWEEN_BLINKS_MS = 1000;
    private static final String LED_PIN_NAME = 18; // GPIO port wired to the LED

    private Handler mHandler = new Handler();

    private Gpio mLedGpio;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Step 1. Create GPIO connection.
        PeripheralManager manager = PeripheralManager.getInstance();
        try {
            mLedGpio = manager.openGpio(LED_PIN_NAME);
            // Step 2. Configure as an output.
            mLedGpio.setDirection(Gpio.DIRECTION_OUT_INITIALLY_LOW);

            // Step 4. Repeat using a handler.
            mHandler.post(mBlinkRunnable);
        } catch (IOException e) {
            Log.e(TAG, "Error on PeripheralIO API", e);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        // Step 4. Remove handler events on close.
        mHandler.removeCallbacks(mBlinkRunnable);

        // Step 5. Close the resource.
        if (mLedGpio != null) {
            try {
                mLedGpio.close();
            } catch (IOException e) {
                Log.e(TAG, "Error on PeripheralIO API", e);
            }
        }
    }

    private Runnable mBlinkRunnable = new Runnable() {
        @Override
        public void run() {
            // Exit if the GPIO is already closed
            if (mLedGpio == null) {
                return;
            }

            try {
                // Step 3. Toggle the LED state
                mLedGpio.setValue(!mLedGpio.getValue());

                // Step 4. Schedule another event after delay.
                mHandler.postDelayed(mBlinkRunnable, INTERVAL_BETWEEN_BLINKS_MS);
            } catch (IOException e) {
                Log.e(TAG, "Error on PeripheralIO API", e);
            }
        }
    };
}

Building the Project

1. Make sure raspberry pi is connected before proceeding.

2. Click on the green arrow on the top and select your device in the run window.

3. Click next.

Note: If you ran into error or device doesn't show up then go to following link or comment below with screenshot.

Wrapping It Up

The LED should start blinking the moment code is uploaded. If it didn't work or you had hard time understanding the article comment below, I'll be happy to help you, Well, that's all for this one. Hope you liked it. Thank you for reading.

This article is accurate and true to the best of the author’s knowledge. Content is for informational or entertainment purposes only and does not substitute for personal counsel or professional advice in business, financial, legal, or technical matters.

© 2019 Dav Vendator

Comments

bhattuc on April 09, 2020:

Highly technical but nicely explained. Thanks. Keep in touch.

Dav Vendator (author) from Amritsar on March 28, 2019:

Android is Open Source Project, Even if Google don't, The user is still free to Port security patches to the Raspberry Pi.

Matt R on March 27, 2019:

Good work. - However I'm worried that google wont support android things anymore for devices like rpi :(

Related Articles