Getting started with Android Things

January 25, 2017
Written by

Getting started with Android Things

Android Things makes it way easier to build IoT applications using the Android Framework and the tools you already know. Let’s have a look at how to flash Android Things to a Raspberry Pi 3 and build our first application.

Our tools

We’ll use the following tools throughout the tutorial:

I have completed this tutorial using a Mac, so the software described will likely not work on Windows or Linux, but you should be able to find equivalents if you’re on a different OS.

Flashing the Android Things image

Extract the files from the zip file you downloaded from the Android Things website and you should end up with a file called iot_rpi3.img. This is an image file and we will flash it to our memory card. If you have the file, feel free to skip the next section and head straight to formatting the memory card..

I don’t have iot_rpi3.img!

When trying to extract the file you may hit a perpetual issue where you’ll end up with a file called androidthings_rpi3_devpreview_4.zip.cpgz. Extracting that file would then create another file called androidthings_rpi3_devpreview_4 1.zip.

Unzipping the file with 7zip via terminal however solves this issue. To install 7zip and extract the file run the following:

$ brew install p7zip
$ 7za x androidthings_rpi3_devpreview_4.zip

Formatting the memory card

Once you’ve extracted the disk image from the zip file, we need to format the memory card and and install the disk image on it. If your memory card already has something on it, and that “something” is another Raspberry Pi image, you may see the message:

The disk you inserted was not readable by this computer.

To solve that issue, open SDFormatter, select the SD Card and click Format.

SD Card Formatter

After the disk is formatted we need to identify where we want to install the image. In your terminal run diskutil and make a note of the disk number assigned to the memory card.

On my machine you can see that the memory card is disk2.

Now that we know which disk number our card has been assigned, it’s time to unmount that disk so we can flash it. In terminal run:

$ diskutil unmountDisk /dev/disk2
Unmount of all volumes on disk2 was successful

To flash the image to disk run the following command in the same directory where the Android Things image was extracted:

$ sudo dd bs=1m if=iot_rpi3.img of=/dev/rdisk2

This process will take anything from 5 to 15 minutes depending on the size and speed of your memory card. Get yourself some coffee, we’re nearly done.

1344700496313878_animate_large.gif

When the process completes, you should get a message pop up saying

The disk you inserted was not readable by this computer.

Great, we’re done with this bit.

Booting up the image

Now that our image has been installed to the memory card, we’ll put it in the Raspberry Pi 3 and boot to check that everything is running as expected.

It’s a good idea to connect an HDMI cable your Pi at least for a bit while we set it up. It will save us from having to guess whether things are running as expected or not.

Android Things first boot

If you see a splash screen that resembles the one above you’re doing great!

giphy.gif

Bringing the “I” back to IoT.

We’ve successfully imaged the memory card and used it to boot the Pi, but notice the splash screen says “No Wifi Connection” and “No Ethernet Connection”.  We need to solve that connecting our Pi to the network.

Power down the Pi momentarily by unplugging its micro-usb cable and connect an ethernet cable. Power it again and you will see that it has an assigned IP address. ?

Android Things first boot

Back in your terminal run the following:

$ adb connect Android.local
connected to Android.local:5555

Android.local is automatically broadcast by the Raspberry Pi, and should resolve to the IP address assigned to your Pi on port 5555. If that doesn’t work for some reason, you can just use the Raspberry Pi’s IP address and port, which in the example above is 192.168.0.23:5555.

However, staying connected to a cable makes our device slightly less portable, so let’s connect our Pi to WiFi.

Making the “I” in IoT wireless

Still in your terminal enter:

$ adb shell am startservice \
    -n com.google.wifisetup/.WifiSetupService \
    -a WifiSetupService.Connect \
    -e ssid <Network_SSID> \
    -e passphrase <Network_Passcode>

Replace <Network_SSID> with your network name and <Network_Passcode> with your network’s password. Ensure that this is a 2.4GHz network because the Raspberry Pi 3 unfortunately does not support 5GHz.

You will see that your Pi now gets an IP address assigned to it soon after you run it. You can now remove the ethernet cable but you will need to restart the Pi before you can connect to it.

Hello, Things!

Finally, let’s test that Android applications can run properly on the Raspberry Pi. To do that we can write and run a simple application. You can write the code yourself or clone this application’s repository here.

Open Android Studio. Create a new project called Hello Things. Android Things runs on API 24 and above and we will start with an empty activity.

new-android-things.gif

In the application level build.gradle add a dependency to Android Things.

dependencies {
    ...
    provided 'com.google.android.things:androidthings:0.4-devpreview'
}

To the application’s manifest add an entry to the shared library and modify the intent filters.


<application
    android:label="@string/app_name">
    <uses-library android:name="com.google.android.things"/>
    <activity android:name=".MainActivity">
        <!-- Launch activity as default from Android Studio -->
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>
            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>

        <!-- Launch activity automatically on boot -->
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>
            <category android:name="android.intent.category.IOT_LAUNCHER"/>
            <category android:name="android.intent.category.DEFAULT"/>
        </intent-filter>
    </activity>
</application>

In hellothings/MainActivity.java, change the onCreate method to list all the available GPIO ports on our Raspberry Pi.


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

    PeripheralManagerService service = new PeripheralManagerService();
    Log.d(TAG, "Available GPIO: " + service.getGpioList());
}

This line of code will return a list of every available General-purpose input/output (GPIO) in our Raspberry Pi. Click run or hit CTRL+R.

01-02 19:40:52.589 1636-1636/rocks.androidthings.hellothings D/MainActivity: Available GPIO: [BCM12, BCM13, BCM16, BCM17, BCM18, BCM19, BCM20, BCM21, BCM22, BCM23, BCM24, BCM25, BCM26, BCM27, BCM4, BCM5, BCM6]

On the screen, you will see that the activity called Hello Things was started.

Android Things application running

Only the beginning

If you, like me, had a Raspberry Pi gathering dust then this is the perfect chance to use your existing Android skills to get into IoT. The Android Things platform is immense and there’s a lot more real life applications we can build.

How about securing your house and getting the Pi to send you an MMS whenever someone walks past a camera? Or how about controlling some of the lights around your house? I would love to see what you come up with. Hit me up on Twitter @marcos_placona or by email on marcos@twilio.com to tell me about it.