Keath Milligan

On software development, quality, security and broken racecars

Implement a RESTful interface with Android Things and Raspberry Pi

Android Things (formerly “Brillo”) is a streamlined version of Android designed for small form-factor and IoT devices. Here is a simple example of a Things app that provides a RESTful web interface to control the state of an LED on a Raspberry Pi GPIO port.

What you will need:

  • A Raspberry Pi 3 Model B
  • A breadboard, ribbon cable, 330 ohm resistors, LEDs, jumpers
  • An 8GB or larger Micro SD card
  • Ethernet cable
  • HDMI cable
  • Android Studio with API 25 or higher tools installed
  • cURL

There are many Raspberry Pi 3 kits available that come with everything you need to get started. Check the Android Things Raspberry Pi hardware page for more information on getting the Things preview image loaded onto an SD card and booting up.

You will also need access to “adb” from the command line.

Setup the Hardware

Unplug your Raspberry Pi and attach the ribbon cable and breadboard. Connect a 330 ohm resistor between BCM 21(see pinout) and the long leg (anode/+) of an LED. Use a jumper to connect the short leg (cathode/-) of the LED to ground. For example:

Double-check your connections and power up the device. Connect the Android debugger with:

The IP address should be displayed at the bottom of the Android Things boot screen.

Create the App

The complete source code for this example is available on Github.

Open Android Studio and create a new project.

  • You can leave “Phone and Tablet” checked since Android Studio doesn’t yet have an option for an Android Things device.
  • Select “API: 24 Android 7.0 (Nougat)” as the minimum SDK.
  • Choose an empty activity to start with.
  • Uncheck “Backwards Compatibility” on the “Customize Activity” page since we don’t need to worry about running on on older devices.

Add the Required Libraries

Download and unpack the Android version of the Restlet Framework. Copy the following jars from the lib directory into your application’s app/libs folder:

  • org.restlet.jar
  • org.restlet.ext.nio.jar
  • org.restlet.ext.json.jar

Now, in Android Studio, edit the “Module: app” build.gradle file and add the necessary libraries for Restlet and Android Things to the “dependencies” section as follows:


Update AndroidManifest.xml

Open the app/manifests/AndroidManifest.xml file and add the following to the “application” section:

Modify the main activity as follows:

This sets up your application as the default activity for the device.

Checkpoint: Run the App

At this point, you should be able to launch the app on the Raspberry Pi and see a “Hello, World!” message. Make sure adb is connected (see above) and click the Run icon in Android Studio.

Add the LED Control Model

Create a new class called LEDModel. This class will provide access to the GPIO pin that controls the state of the LED:

For more info on controlling the GPIO outputs of your device, refer to the Android Things SDK documentation.

Add the Restlet Resource

Create a new class called LEDResource that extends org.restlet.resource.ServerResource. This class provides a RESTful representation of the LED state:

Add the Service

The HTTP server will be run as a background service on a separate thread from the user interface activity. Add a new class called RESTfulService that extends IntentService:

This service provides intent actions that the main activity can use to start and stop the server.

Integrate the Service

Finally, edit the MainActivity class to start and stop the service:


Launch the app. You should see a message in the logcat output that says Starting the internal [HTTP/1.1] server on port 8080.

Open a command-line window and use curl to request the state of the LED:

Where “<ipaddress>” is the IP address of your device. This should return the current state (false) in JSON format:

To turn the LED on:

The LED should now be lit. To turn it off again:


The complete source code for this example is available on Github.


1 Comment

  1. Nice and enough simple app, this helps me a lot.

    I had to add one permission to androidmanifest.xml (see the line starting: uses-permision android…)

    <manifest xmlns:android=””
    <uses-permission android:name=”android.permission.INTERNET” />


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

© 2019 Keath Milligan

Theme by Anders NorenUp ↑