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)

What is Android Things?

Android Things is the IoT platform made by Google and based on Android. You could have guessed that by its name. It is similar to Android Wear, Android Auto, and Android TV in the way that it is an adaptation of Android to another domain, where most of the concepts of the platform are still valid, but there are significant differences as well.

The platform is intended to design and build IoT devices and bring them to the mass market. The key idea behind it is that you can easily prototype your project and, once it is ready, you can move from your developer kit to a simpler and smaller carrier board with just the SoM (System-on-Module).

SoMs are very handy. They are a step up from SoCs (System-on-Chips). They integrate RAM, flash storage, Wi-Fi, and Bluetooth on a single module. The official boards come with the FCC certifications, so all the process of getting the software onto them is streamlined.

Throughout the process of going to mass market, Google will provide security updates to the platform, so keeping your IoT devices up to date and secure is no longer something you have to worry about.

Google will provide security updates to Android Things.

All the areas of this process are centralized on the Android Things Console, which has a similar function to the Google Play Console, but is more focused on building images that you can then distribute and deploy.

One of the key advantages of Android Things is that it can make use of almost all of the already existing Android libraries and frameworks, which gives it a head start in terms of tooling. From the developer point of view, it also lowers the entry barrier to make IoT devices, since all the knowledge you have of the Android framework, libraries, and tools is fully transferable.

In simple terms, Android Things simplifies and empowers the development of IoT devices.

Internet of Things (IoT) vs. smart devices

Before we go any further, let's try to establish what most people understand by IoT and what smart objects are.

An IoT device is meant to use the internet as something integral for its behavior. Examples include remote controlled lights, any appliance that you can interact with over an API (such as lights, ovens, thermostats, coffee makers, and so on) even if it is only for reading data, such as an umbrella that glows when there is a rain forecast, or writing it, such as a simple weather station. For all those systems, the internet is an integral part of the concept.

On the other hand, a smart object is something that is designed to work in isolation. This can still include many complex components, such as machine learning and computer vision. A self-driving robot that navigates a maze or smart curtains that open when there is sunlight are both quite autonomous. A device that you show a photo of a dog to and tells you the breed also falls into this category (and yes, that is one of the official demos of Android Things). All these examples are smart, but they do not need the internet, so I like to call them just smart devices and, yes, we can build those with Android Things too.

Android Things vs. other platforms

Probably the most commonly asked question is: how does Android Things position itself with Arduino?

They are completely different platforms. Android Things is a full operating system running on an SoM, while Arduino runs on microcontrollers. From a capabilities point of view, they are in different orders of magnitude.

Everything you can do on Arduino, you can do with Android Things, but the opposite is not true; there are many things you can do on Android Things that you can't even get close to with Arduino, and we will cover some of them in the final chapter.

Lastly, there is the comparison of SDK and tools. Android Studio is one of the best IDEs overall. The Arduino IDE is very simple and limited. Not to mention how much easier it is to manage project dependencies on Android Studio compared to Arduino libraries.

However, not everything is better on Android Things. There are a few areas where Arduino has the upper hand, namely, power consumption and analog input/output.

The power consumption of Arduino is very low; you can run it on batteries or even solar power for extended periods of time. That is definitely not possible with the current developer kits for Android Things; even a large battery pack will get depleted in just a matter of days.

There is room for everything on IoT. I think Arduino is best at having sensor data collection on the field, but then a central hub running Android Things can do aggregation, cloud upload, and even use machine learning to interpret the readings.

The other platform I usually get asked to compare Android Things with is a Raspberry Pi running Linux and programming it on Python. In this case the hardware is exactly the same, but there are two main advantages of Android Things.

Firstly, if you plan to eventually release and sell your IoT project, Android Things offers a developer console and even a means to mass produce devices, as well as automatic security updates.

Secondly, even with all the libraries that are available on Python, there are more and better examples of using Android. Building an IoT app is a lot closer to a mobile app than it is to a desktop app, so all your prior Android knowledge is easily transferable to Android Things.

Android Things shines when using all the tools that it has available; be it services such as Firebase for cloud storage or cloud messaging and TensorFlow for image classifiers, or libraries such as Retrofit to create API clients and NanoHTTPD to create a server, or just SDK classes, such as Thread, Alarm, Timer, and so on.

Emulators and testing

Since Android Things is meant to be used with custom hardware, it is useless to have an emulator of just the dev kit. Also, given the wide variety of hardware that we can connect, it is almost impossible to emulate it; we would need a whole electronics set to be able to draw our project and then run it. Those are the reasons why Android Things does not have an emulator.

There is no emulator for Android Things.

In my experience, the lack of an emulator is not a problem. On the one hand, it is very satisfying to see the hardware working, and, on the other, many problems that you'll face will not be solved with an emulator, problems such as deciding how to arrange the components inside the chassis of a robot car or figuring out how many turns of a stepper motor are needed to activate a candy dispenser.

In my experience, the most common source of bugs when working with electronics is the actual wiring of the circuits. A loose cable or a misplaced connection are often the reason why something is not working and an emulator won't solve that. Besides, setting the connections on a program is usually more time-consuming than actually wiring them.

Finally, if you really want to use something like an emulator to test your project as you go along, my suggestion is to rely on testing using a mocking framework, such as Mockito or EasyMock, and either run instrumentation tests on the device or use Robolectric to run them on your computer.

You can test Android Things projects using Mockito and Robolectric.

While testing on Android Things is beyond the scope of this guide, and I don't recommend investing in it for just prototyping and tinkering; if you are planning on going to the mass market with any IoT project, testing should definitely be on your agenda.

Android Studio

One of the big advantages of Android Things is that it relies on the excellent set of tools that Google has been building for Android over the years. Among those tools, Android Studio is probably the most impressive.

It is not only that you do not need to get used to a new IDE. Android Studio is already an excellent tool.

You have dependency management with gradle, multi-module and library projects, support for flavors, and so on. And that is just basic gradle. The IDE itself has excellent wizards, refactoring tools, search and autocompilation, profiling, and much more. Plus, do not forget Kotlin support.

Android Studio has come a long way since the early days of Android with Eclipse, and Android Things has all those tools at its disposal from day one.