Book Image

Android Things Quick Start Guide

By : Raul Portales
5 (1)
Book Image

Android Things Quick Start Guide

5 (1)
By: Raul Portales

Overview of this book

Android Things is the IoT platform made by Google, based on Android. It allows us to build smart devices in a simple and convenient way, leveraging on the Android ecosystem tools and libraries, while letting Google take care of security updates. This book takes you through the basics of IoT and smart devices. It will help you to interact with common IoT device components and learn the underlying protocols. For a simple setup, we will be using Rainbow HAT so that we don't need to do any wiring. In the first chapter, you will learn about the Android Things platform, the design concepts behind it, and how it relates to other IoT frameworks. We will look at the Developer Kits and learn how to install Android Things on them by creating a simple project. Later, we will explore the real power of Android Things, learning how to make a UI, designing and communicating with companion apps in different ways, showcasing a few libraries. We will demonstrate libraries and you will see how powerful the Android Things operating system is.
Table of Contents (10 chapters)

Setting up a developer kit

To configure our Android Things developer kit, we will use the setup utility script that can be downloaded from the Android Things Console. It is a simple wrapper of a few tools and simplifies the process of getting our board ready.

The setup utility will help us to install Android Things on the developer kit and also to configure Wi-Fi.

You can download the setup utility from the Android Things Console

The download contains executables for Linux, macOS, and Windows. If you are going to flash a Raspberry Pi, make sure to run it with administrator permissions (sudo on macOS/Linux) because writing the SD card requires root permissions.

Installing Android Things

To proceed with the installation of Android Things, we just have to execute the script for our operating system:

$ sudo ./android-things-setup-utility-macos

If you are flashing a Raspberry Pi, you will need to run the script with sudo.

The script is quite self-explanatory. We just have to follow the steps and select what we want to do. The trickiest part is to configure Wi-Fi, but we can do that later any time in several ways. It looks like this:

Android Things Setup Utility (version 1.0.19)
============================
This tool will help you install Android Things on your board and set up Wi-Fi.
What do you want to do?

1 - Install Android Things and optionally set up Wi-Fi
2 - Set up Wi-Fi on an existing Android Things device

We select Install Android Things. We will be asked about the hardware since there are differences in the process. For this example we will be using a Raspberry Pi because it is a bit more complex. The setup for the iMX7D is simpler, since you do not need to extract the SD card or connect to the network via a cable.

What hardware are you using?
1 - Raspberry Pi 3
2 - NXP Pico i.MX7D

The script will download the required tools and will ask us if we want to use a default image or a custom one.

To get started on Android Things, a default image is the best option.

For our purposes, and for development in general, a default image is the best option. The custom image is used when you are later in the process of building your device and want to test that everything works as expected:

Raspberry Pi 3
Do you want to use the default image or a custom image?
1 - Default image: Used for development purposes. No access to the Android
Things Console features such as metrics, crash reports, and OTA updates.
2 - Custom image: Upload your custom image for full device development and
management with all Android Things Console features.

The script will then download the required image and flash it into the device.

If you are using an NXP board, this is done via the USB type C cable. Please note that the script is a bit confusing in terms of whether you have to connect to your laptop directly or via a USB hub. In any case, make sure the port can properly power the device. This is likely to fail if you use a non-powered USB hub.

When setting up an iMX7D, make sure the USB port can power the device properly.
Connect your device to this computer:
The USB cable should plug into your board's USB-C port. If your computer also has USB-C ports like the more recent MacBooks, you will need to use a USB hub.
Otherwise the board won't power on correctly.

In the case of a Raspberry Pi, you need to extract the SD card and insert it in your computer so that it can be flashed. This is the step that requires root permissions.

Downloading Etcher-cli, a tool to flash your SD card...
20.5 MB/20.5 MB
Unzipping Etcher-cli...

Plug the SD card into your computer. Press [Enter] when ready
[...]
iot_rpi3.img was successfully written to Apple SDXC Reader Media (/dev/disk3)
Checksum: 4d176685

And with that, our board has Android Things installed. Now, we have the option to configure Wi-Fi, which we are going to do in the next section.

If you have successfully installed Android Things on your SD card, you can now put the SD card into the Raspberry Pi and power it up. Otherwise you can abort and run the tool again.

This last message is a bit confusing. It asks us whether we have successfully installed Android Things. It refers to the need for root permissions to write on the SD card. If you forgot to run the script with sudo, it will fail and you'll need to start over again.

Configuring Wi-Fi using the script

We already mentioned that you can only deploy your code to a Raspberry Pi using adb over a network connection, so configuring Wi-Fi is quite important. I do use an Ethernet cable most of the time when testing on a Pi, but configuring Wi-Fi comes in handy quite often.

In the case of the iMX7D, this step is even less important, since we can use the board for debugging via the USB type C cable.

In any case, there are several ways to configure Wi-Fi: you can use the script any time to perform just this step, you can do it with the adb command (for which the script is a wrapper) or you can use the system UI for that.

Let's take a quick look at the message from the script:

Would you like to set up Wi-Fi on this device? (y/n)
Please plug your Raspberry Pi to your router with an Ethernet cable, then press [Enter].

So, to be able to configure Wi-Fi on a Raspberry Pi, you need to connect it via Ethernet first; then, the script will connect to it using adb and configure Wi-Fi.

You need an Ethernet cable to configure Wi-Fi on the Raspberry Pi with the setup script.

In the case of the iMX7D, the message is slightly different:

Please ensure antenna is already attached. If it is not, disconnect your board, attach the antenna and reconnect your board to your computer.

When ready, press [Enter]...

This board will not be able to connect to Wi-Fi unless the antenna is connected, but the configuration is done via the USB type C cable:

The Wi-Fi on iMX7D will not work unless the antenna is connected.
Attempting to connect to your Raspberry Pi at Android.local...
Connected to device through Ethernet.
Enter the Wi-Fi network name: PlattySoftHQ
Enter the Wi-Fi network password (leave empty if no password):
Connecting to Wi-Fi network PlattySoftHQ...
Looking for device... This can take up to 3 minutes.
Device found.
Waiting...

What the script does is to connect adb to the device using the name Android.local and then send an adb command to set up the Wi-Fi. If you want to do that by hand, you just have to type the following:

$ adb connect Android.local
connected to Android.local:5555
$ adb shell am startservice \
-n com.google.wifisetup/.WifiSetupService \
-a WifiSetupService.Connect \
-e ssid <Network_SSID> \
-e passphrase <Network_Passcode>

You can see the complete list of parameters in the official documentation at https://developer.android.com/things/hardware/wifi-adb.

The Wi-Fi setup may fail if you have several Android devices on the LAN.

Since the script relies on the device identifying itself as Android.local on the local network, this only works if it is the only device doing that. If you already have another device on the network that uses that name (such as another Android Things or an Android TV) this may fail, so let's look at a back-up plan.

Configuring Wi-Fi using the system UI

Early versions of Android Things did not have any UI besides a screen that displayed the Android Things logo and the current IP. Part of the feedback received was that using Android.local as the means to connect to the board was way too unreliable and, since you need to connect a display to the dev kit to see the IP if that fails, you could also configure it straight away, so that feedback was incorporated to the default app.

On the current version of Android Things, you can configure Wi-Fi the same way you would on an Android device. On a Raspberry Pi, you want to connect an HDMI display, mouse, and keyboard (or a touchscreen, if you have one). The iMX7D should work without any issues with the script, but you can also use the touchscreen that is provided with the dev kit.

The main screen of Android Things is similar to an Android settings screen, but with a far fewer options. Let's pick networks and then enable wireless.

I recommend configuring DHCP to always give the same IP to these boards and to write it down, so you don't have to connect a display every time you start the device to check the IP.
Home screen of Android Things, including information and settings

By clicking on a Wi-Fi, you can just enter a password directly on the device.

Network configuration screen of Android Things

You should take this chance, now that you have a screen connected, and write down the IP for later use.

Connecting to your Android Things

On an iMX7D, you just need to use adb as you would do with an Android-powered smartphone; no extra setup is required.

You do not need to perform any extra steps to connect to an iMX7D.

For Raspberry Pi, you need to connect to adb over the network, so, regardless of whether it is connected via a cable or via Wi-Fi, you can just type the following:

$ adb connect Android.local

If you are lucky that should be enough, but if, as we mentioned before, you have several devices identifying as Android.local, this will fail. In that case, you will need to connect using the IP of the dev kit. The simplest way to know the IP address of a device is to attach a display to it, so hopefully you already have that IP written down.

The simplest way to know the IP address of a dev kit is to attach a display to it.
Once you know the IP (let's use the one for the screenshot: 192.168.42.52), you can connect to the board by typing the following:
$ adb connect 192.168.42.38
connected to 192.168.42.38:5555

And you are ready to deploy your apps to the dev kit. It will appear with a name, such as Google Iot_rpi3, on the Select Deployment Target dialog of Android Studio.

Now that we have our development kit ready to be used, let's create a sample empty project for Android Things.