Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon

Getting Started with Android Things

Save for later
  • 16 min read
  • 21 Jul 2017

article-image

In this article by Charbel Nemnom, author of the book Getting Started with Nano Server, we will cover following topics:

  • Internet of things overview
  • Internet of Things components
  • Android Things overview
  • Things support library
  • Android Things board compatibility
  • How to install Android Things on Raspberry
  • Creating the first Android Things project

(For more resources related to this topic, see here.)

Internet of things overview

Internet of things, or briefly IoT, is one of the most promising trends in technology. According to many analysts, Internet of things can be the most disruptive technology in the upcoming decade. It will have a huge impact on our lives and it promises to modify our habits. IoT is and will be in the future a pervasive technology that will span its effects across many sectors:

  • Industry
  • Healthcare
  • Transportation
  • Manufacturing
  • Agriculture
  • Retail
  • Smart cities

All these areas will have benefits using IoT. Before diving into IoT projects, it is important to know what Internet of things means. There are several definitions about Internet of things addressing different aspects considering different areas of application. Anyway, it is important to underline that IoT is much more than a network of smartphones, tablets, and PCs connected to each other. Briefly, IoT is an ecosystem where objects are interconnected and, at the same time, they connect to the internet. Internet of things includes every object that can potentially connect to the internet and exchange data and information. These objects are connected always, anytime, anywhere, and they exchange data.

The concept of connected objects is not new and over the years it has been developed. The level of circuit miniaturization and the increasing power of CPU with a lower consumption makes it possible to imagine a future where there are millions of “things” that talk each other.

The first time that Internet of things was officially recognized was in 2005. International Communication Union (ITU) in a report titled The Internet of Things (https://www.itu.int/osg/spu/publications/internetofthings/InternetofThings_summar y.pdf), gave the first definition:

“A new dimension has been added to the world of information and communication technologies (ICTs): from anytime, any place connectivity for anyone, we will now have connectivity for anything…. Connections will multiply and create an entirely new dynamic network of networks – an Internet of Things”

In other words, IoT is a network of smart objects (or things) that can receive and send data and we can control it remotely.

Internet of Things components

There are several elements that contribute to creating the IoT ecosystem and it is important to know clearly the role they play in order to have a clear picture about IoT. This will be useful to better understand the projects we will build using Android Things. The basic brick of IoT is a smart object. It is a device that connects to the internet and it is capable of exchanging data. It can be a simple sensor that measures a quantity such as pressure, temperature, and so on or a complex system. Extending this concept, our oven, our coffee machine, our washing machine are examples of smart objects once they connect to the internet. All of these smart objects contribute to developing the internet of things network. Anyway, not only household appliances are examples of smart objects, but cars, buildings, actuators, and so on can be included in the list. We can reference these objects, when connected, using a unique identifier and start talking to it.

At the low level, these devices exchange data using a network layer. The most important and known protocols at the base of Internet of things are:

  • Wi-Fi
  • Bluetooth
  • Zigbee
  • Cellular
  • Network
  • NB-IoT
  • LoRA

From an application point of view, there are several application protocols widely used in the internet of things. Some protocols derive from different contexts (such as the web) others are IoT-specific. To name a few of them, we can remember:

  • HTTP MQTT CoAP AMQP
  • Rest XMPP
  • Stomp

By now, they could be just names or empty boxes, but throughout this article we will explore how to use these protocols with Android Things.

Prototyping boards play an important role in Internet of things and they help to develop the number of connected objects. Using prototyping boards, we can experiment IoT projects and in this article, we will explore how to build and test IoT projects using boards compatible with Android Things. As you may already know, there are several prototyping boards available on the market, each one having specific features. Just to name a few of them, we can list:

  • Arduino (in different flavors) Raspberry Pi (in different flavors) Intel Edison
  • ESP8266
  • NXP

We will focus our attention on Raspberry Pi 3 and Intel Edison because Android Things supports officially these two boards. We will also use other developments boards so that you can understand how to integrate them.

Android Things overview

Android Things is the new operating system developed by Google to build IoT projects. This helps you to develop professional applications using trusted platforms and Android. Yes Android, because Android Things is a modified version of Android and we can reuse our Android knowledge to implement smart Internet of things projects. This OS has great potential because Android developers can smoothly move to IoT and start developing and building projects in a few days. Before diving into Android Things, it is important to have an overview. Android Things OS has the layer structure shown in the following diagram:

This structure is slightly different from Android OS because it is much more compact so that apps for Android Things have fewer layers beneath and they are closer to drivers and peripherals than normal Android apps. Even if Android Things derives from Android, there are some APIs available in Android not supported in Android Things. We will now briefly describe the similarities and the differences. Let us start with the content providers, widely used in Android, and not present in Android Things SDK. Therefore, we should pay attention when we develop an Android Things app. To have more information about these content providers not supported, please refer to the Official Android Things website https://developer.android.com/things/sdk/index.html.

Moreover, like a normal Android app, an Android Things app can have a User Interface (UI), even if this is optional and it depends on the type of application we are developing. A user can interact with the UI to trigger events as they happen in an Android app. From this point of view, as we will see later, the developing process of a UI is the same used in Android. This is an interesting feature because we can develop an IoT UI easily and fast re- using our Android knowledge. It is worth noticing that Android Things fits perfectly in the Google services. Almost all cloud services implemented by Google are available in Android Things with some exceptions. Android Things does not support Google services strictly connected to the mobile world and those that require a user input or authentication. Do not forget that user interface for an Android Things app is optional. To have a detailed list of Google services available in Android Things refer to the Official page (https://developer.android.com/things/sdk/index.html).

An important Android aspect is the permission management. An Android app runs in a sandbox with limited access to the resources. When an app needs to access a specific resource outside the sandbox it has to request the permission. In an Android app, this happens in the Manifest.xml file. This is still true in Android Things and all the permissions requested by the app are granted at installation time. Android 6 (API level 23) has introduced a new way to request a permission. An app can request a permission not only at installation time (using the Manifest.xml file), but at runtime too. Android Things does not support this new feature, so we have to request all the permissions in the Manifest file.

The last thing to notice is the notifications. As we will see later, Android Things UI does not support the notification status bar, so we cannot trigger notifications from our Android Things apps.

To make things simpler, you should remember that all the services related to the user interface or that require a user interface to accomplish the task are not guaranteed to work in Android Things.

Things support library

Things support library is the new library developed by Google to handle the communication with peripherals and drivers. This is a completely new library not present in the Android SDK and this library is one of the most important features. It exposes a set of Java Interface and classes (APIs) that we can use to connect and exchange data with external devices such as sensors, actuators, and so on. This library hides the inner communication details supporting several industry standard protocols as:

GPIO I2C PWM SPI UART

Moreover, this library exposes a set of APIs to create and register new device drivers called user drivers. These drivers are custom components deployed with the Android Things app that extends the Android Things framework. In other words, they are custom libraries that enable an app to communicate with other device types not supported by Android Things natively.

This article will guide you, step by step, to know how to build real-life projects using Android. You will explore the new Android Things APIs and how to use them. In the next sections, you will learn how to install Android Things on Raspberry Pi 3 and Intel Edison.

Android Things board compatibility

Android Things is a new operating system specifically built for IoT. At the time of writing, Android Things supported four different development boards:

  • Raspberry Pi 3 Model B Intel Edison
  • NXP Pico i.MX6UL
  • Intel Joule 570x

In the near future, more boards will be added to the list. Google has already announced that it will support this new board:

NXP Argon i.MX6UL

The article will focus on using the first two boards: Raspberry Pi 3 and Intel Edison. Anyway, you can develop. This is the power of Android Things: it abstracts the underlying hardware providing a common way to interact with peripherals and devices. The paradigm that made Java famous, Write Once and Run Anywhere (WORA), applies to Android Things too. This is a winning feature of Android Things because we can develop an Android Things app without worrying about the underlying board. Anyway, when we develop an IoT app there are some minor aspects we should consider so that our app will be portable to other compatible boards.

How to install Android Things on Raspberry

Raspberry Pi 3 is the latest board developed by Raspberry. It is an upgrade of Raspberry Pi 2 Model B and respect to its predecessor it has some great features:

  • Quad-core ARMv8 Cpu at 1.2Ghz Wireless Lan 802.11n
  • Bluetooth 4.0

Unlock access to the largest independent learning library in Tech for FREE!
Get unlimited access to 7500+ expert-authored eBooks and video courses covering every tech area you can think of.
Renews at £16.99/month. Cancel anytime

The below screenshot shows a Raspberry Pi 3 Model B:

In this section, you will learn how to install Android Things on Raspberry Pi 3 using a Windows PC or a Mac OS X.

Before starting the installation process you must have: Raspberry Pi 3 Model B

  • At least 8Gb SD card
  • A USB cable to connect Raspberry to your PC
  • An HDMI cable to connect Raspberry to a TV/monitor (optional)

If you do not have an HDMI cable you can use a screen mirroring tool. This is useful to know the result of the installation process and when we will develop the Android Things UIs. The installation steps are different if you are using Windows, OS X or Linux.

How to install Android Things using Windows

At the beginning we will cover how to install Android Things on Raspberry Pi 3 using a Windows PC:

  1. Download the Android Things image from this link: https://developer.android.com/things/preview/download.html. Select the right image, in this case, you have to choose the Raspberry Pi image.
  2. Accept the license and wait until the download is completed.
  3. Once the download is complete, extract the zip file.
  4. To install the image on the SD card, there is a great application called Win32 Disk Imager that works perfectly. It is free and you can download it from SourceForge using this link: https://sourceforge.net/projects/win32diskimager/. At the time of writing, the application version is 0.9.5.
  5. After you downloaded it, you have to run the installation executable as Administrator. Now you are ready to burn the image into the SD card.
  6. Insert the SD card into your PC.
  7. Select the image you have unzipped at step 3 and be sure to select the right disk name (your SD). At the end click on Write.

You are done! The image is installed on the SD card and we can now start Raspberry Pi.

How to install Android Things using OS X

If you have a Mac OS X, the steps to install Android Things are slightly different. There are several options to flash this OS to the SD card, you will learn the fastest and easiest one.

These are the steps to follow:

    1. Format your SD card using FAT32. Insert your SD card into your Mac and run Disk Utility. You should see something like it:
    2. Download the Android Things OS image using this link: https://developer.android.com/things/preview/download.html.
    3. Unzip the file you have downloaded.
    4. Insert the SD card into your Mac.
    5. Now it is time to copy the image to the SD card. Open a terminal window and write:
      sudo dd bs=1m if=path_of_your_image.img of=/dev/rdiskn

    6. Where the path_to_your_image is the path to the file with img extension you downloaded at step 2. In order to find out the rdiskn you have to select Preferences and then System Report. The result is shown in the picture below:
    7. The BSD name is the disk name we are looking for. In this case, we have to write:
      sudo dd bs=1m if=path_of_your_image.img of=/dev/disk1

    8. That's all. You have to wait until the image is copied into the SD card. Do not forget that the copying process could take a while. So be patient!

Creating the first Android Things project

Considering that Android Things derives from Android, the development process and the app structure are the same we use in a common Android app. For this reason, the development tool to use for Android Things is Android studio. If you have already used Android studio in the past, reading this paragraph you will discover the main differences between an Android Things app and Android app. Otherwise, if you are new to Android development, this paragraph will guide you step by step to create your first Android Things app.

Android Studio is the official development environment to develop Android Things apps, therefore, before starting, it is necessary you have installed it. If not, go to https://developer.android.com/studio/index.html, download and install it. The development environment must adhere to these prerequisites:

  • SDK tools version 24 or higher.
  • Update the SDK with Android 7 (API level 24) Android Studio 2.2 or higher

If your environment does not meet the previous conditions, you have to update your Android Studio using the Update manager.

Now there are two alternatives to starting a new project:

  • Cloning a template project from Github and import it into Android Studio
  • Create a new Android project in Android Studio

To better understand the main differences between Android and Android Things you should follow option number 2, at least the first time.

Cloning the template project

This is the fastest path because with a few steps you are ready to develop an Android Things app:

    1. go to https://github.com/androidthings/new-project-template and clone the repository. Open a terminal and write:
      git clone https://github.com/androidthings/new-project-template.git

    2. Now you have to import the cloned project into Android Studio.

Create the project manually

This step is a quite longer respect to the previous option but it is useful to know the main differences between these two worlds.

  1. Create a new Android project. Do not forget to set the Minimum SDK to API Level 24:
  2. By now, you should create a project with empty activity. Confirm and create the new project.

There are some steps you have to follow before your Android app project turns into an Android Things app project:

  1. Open Gradle scripts folder and modify build.gradle (app-level) and replace the dependency directive with the following lines:
    dependencies {
    provided 'com.google.android.things:androidthings:0.2-devpreview'
    }
    

  2. Open res folder and remove all the files under it except strings.xml.
  3. Open Manifest.xml and remove android: theme attribute in application tag.
  4. In the Manifest.xmladd the following line inside application tag:
    <uses-library android_name="com.google.android.things"/>

  5. In the layout folder open all the layout files created automatically and remove the references to values.
  6. In the Activity created by default (MainActivity.java) remove this line:
    import android.support.v7.app.AppCompatActivity;

  7. Replace AppCompatActivity with Activity.
  8. Under the folder java remove all the folders except the one with your package name.

That's all. You have now transformed an Android app project into an Android Things app project. Compiling the code you will have no errors. For the next times, you can simply clone the repository holding the project template and start coding.

Differences between Android and Android Things

As you can notice an Android Things project is very similar to an Android project. We have always Activities, layouts, gradle files and so on. At the same time, there are some differences:

  • Android Things does not use multiple layouts to support different screen sizes. So when we develop an Android Things app we create only one layout Android Things does not support themes and styles.
  • Android support libraries are not available in Android Things.

Summary

In this article we have covered, Internet of things overview and components, Android Things overview, Things support library, and Android Things board compatibility, and also learned how to create first Android Things project.

Resources for Article:


Further resources on this subject:

  • Saying Hello to Unity and Android [article]
  • Getting started with Android Development [article]
  • Android Game Development with Unity3D [article]