Over a million developers have joined DZone.

Android Things bot: How to Build an IoT Bot to Execute Tasks

DZone's Guide to

Android Things bot: How to Build an IoT Bot to Execute Tasks

Learn to integrate two emerging technologies, Android Things and bots, in a hands-on IoT project that creates a bot, executes IoT tasks, and handles different commands.

· IoT Zone
Free Resource

Discover why Bluetooth mesh is the next evolution of IoT solutions. Download the mesh overview.

In this tutorial, we will cover how to implement an Android Things bot to execute tasks. This is an interesting topic because it mixes two emerging technologies as Internet of Things (IoT) and bots. As you may know, Android Things is the new Google OS built for the Internet of Things. This OS helps you develop professional IoT projects without knowing much about embedded systems. Android Things is built on Android so you can reuse your Android expertise and get ready to develop IoT projects.

Moreover, you already what know a bot is. Maybe you've heard a lot about this topic. Anyway, if you are a new to the concept, a bot is an application that executes automated tasks. In other words, it is a piece of software we develop to performs some specific task. These tasks can range from executing queries online to search for something, or answer to some command and retrieve the weather information.

In this article, we approach the bot topic implementing a different service that uses a bot to control remote peripherals. This IoT project integrates IoT and bots, thus providing a service that can be extended and reused in real-life IoT projects.

IoT Bot Project Overview

The idea that stands behind this Android Things bot project is controlling a remote peripheral device using a bot to execute tasks. In this project, the Android Things bot will control two different LEDs parsing incoming commands and turn them on or off.

As stated before, this project can be further extended and applied to real-life use cases. This project uses Telegram as a bot platform that supports third-party apps. To interact with the bot, we use a chat where we send messages to the bot that are commands.

To build this IoT bot, we need to follow these steps:

  • Create and configure a bot in Telegram.
  • Develop an Android Things bot app that interacts with the bot using an API.
  • Transform the command coming in as messages to a “hardware” command to control remote peripherals.

We will develop this project using Android Things because it is a new OS and it offers great features. Moreover, we can use existing Android libraries in Android Things and there are several Telegram bot, Java-based libraries ready to use.

How to Create a Bot

The first step is to create a bot in Telegram. To do this, we can follow the steps described in this guide. Also, to build our bot we will need to interact with the BotFather:

Image title

We start the conversation with BotFather using the /start command and the /newbot command, thus telling to the bot we want to create a new bot. It is necessary to provide some information, such as:

  • The name of the bot you want to create.
  • The username for the bot (it must end in bot).

After these simple steps, the bot is ready. For this IoT bot project, we have created a bot called AndroidThingsBot:

Android Things bot: Use Telegram in IoT

At the end of this process, the telegram bot provides the HTTP API token that we will use in our Android Things IoT bot.

That’s all; our bot is ready to receive commands. The next step is implementing the Android Things bot that handles incoming commands and interacts with the peripherals.

Develop an Android Things Bot to Execute IoT Tasks

iot bot message

As the first step, it is necessary to create a new Android Things project in Android Studio. This tutorial assumes you are familiar with Android Things. If you want to learn how to use Android Things in an IoT project, you can read my book published by Packt titled “Android Things Projects.”

Once the project is configured, we can add the dependencies. This IoT project uses an open-source library to interact with Telegram. Add the following lines to the build.gradle:

dependencies {
    compile 'com.github.pengrad:java-telegram-bot-api:3.0.1'
    compile 'com.squareup.okhttp3:okhttp:3.8.1'

That’s all! We are ready to interact with the bot. Our target is receiving the messages from AndroidThingsBot and handling them. Next, we have to initialize the class that handles the communication details using the Telegram API:

TelegramBot bot;
protected void onCreate(Bundle savedInstanceState) {
  Log.d(TAG, "onCreate");
  bot = TelegramBotAdapter.build(BOT_TOKEN);

The BOT_TOKEN is the token we have received during the creation step in the previous paragraph.

There are several strategies we can exploit to receive messages. This library provides a listener to listen to incoming messages:

// start listening for incoming messages
GetUpdates getUpdates = new GetUpdates()
  bot.setUpdatesListener(new UpdatesListener() {
    public int process(List<Update> updates) {
     for (Update update : updates) {
        Message msg = update.message();
       // Bot logic Here
     return UpdatesListener.CONFIRMED_UPDATES_ALL;

In this way, as soon as the bot receives a message, the Android Things app gets notified and handles the command. Finally, the last step, where our Android Things app interacts with the peripherals.

How to Handle Commands

In this section, we cover how our Android Things app controls the peripherals, in this case, they are simple LEDs. Of course, you can expand this project and use other kinds of peripherals. The schematic is shown below:

android things sketch bot

The connections are very simple, the two LEDs are connected to the Raspberry Pi using 220Ω resistors to limit the current flowing through the LEDs themselves. Moreover, the LEDS are managed using two GPIO pins in the Raspberry Pi:

  • Pin 13 -> BCM 27
  • Pin 15 -> BCM 22

For more information, take a look at Android Things Raspberry Pinout.

Once the connections are ready, we can focus our attention on developing the source code. As you may already know, the power of Android Things is abstracting the hardware where it works, providing a common way to interact with components and peripherals.

Let us initialize the Peripheral service used to manage the pins. To accomplish this, add the following lines to the Android Activity:

private PeripheralManagerService pmService = new PeripheralManagerService();

Then, we must initialize the pins:

private void initPins() {
  try {
   bluePin = pmService.openGpio("BCM22");
   yellowPin = pmService.openGpio("BCM27");
  catch(IOException ioe) {

Time to invoke the MainActivity class. Moreover, it is useful to add another method to manage the pins so that the app can turn the LEDs on or off:

private void setPin(Gpio pin, String state) {
 try {
 catch (IOException ioe) {

Finally, the app has to handle the incoming commands from the bot and manage the LEDs, therefore, it is necessary to modify the method that handles the incoming messages:

bot.setUpdatesListener(new UpdatesListener() {
   public int process(List<Update> updates) {
     for (Update update : updates) {
       Message msg = update.message();
       if (msg != null) {
         String txt = msg.text();
         if (txt.trim().startsWith("LED")) {
           Log.d(TAG, "LED COMMAND");
           String[] data = txt.split(" ");
           if (data.length < 3) {
             Log.d(TAG, "Command format error");
             return UpdatesListener.CONFIRMED_UPDATES_ALL;
           String value = data[2];
           if (data[1].equalsIgnoreCase("b")) {
             Log.d(TAG, "Blue pin ["+value+"]");
             setPin(bluePin, value);
           else if (data[1].equalsIgnoreCase("y")) {
             setPin(yellowPin, value);
   return UpdatesListener.CONFIRMED_UPDATES_ALL;

Now you can test your bot. The commands that the bot handles have the format:

LED y|b on|off


In this post, we have covered an interesting topic that integrates two emerging technologies: Android Things and bots. This Android Things IoT bot can be further developed so that it can handle more commands or it can interact with other kinds of peripherals. This is a prototype project, but the idea that stands behind it can be exploited and applied to other fields.

Take a deep dive into Bluetooth mesh. Read the tech overview and discover new IoT innovations.

iot apps ,iot application ,andriod ,bots ,iot

Published at DZone with permission of Francesco Azzola, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}