Exploring Google’s new IoT platform: Android Things

Android is now everywhere, our phone, our TV, our watch, our cars, everywhere! It’s the smartest OS right now that operates even on the smallest devices with near-low to high configuration. It’s amazing how one (open-sourced) OS can support all the devices ranging from high-end to low-end so smoothly.

And just two days ago, Google just upped its game. Google announced its stepping into a new zone, introducing Android to Internet of Things aka IoT projects. You can now work on your IoT projects with your favourite hardwares (Intel Edison, NXP Pico i.MX6UL or Raspberry Pi 3) and write codes in pure Android to run on it.

This just doesn’t mean writing simple Android codes for Hardware, this means access to most of Google’s APIs directly from and to your Hardware devices.

You can capture images via camera hardware along with Android’s default Camera permissions, submit to Google Cloud Vision API to analyze it, use Firebase database to pass it around, and all with official Android SDK directly from your device. No setting up APIs, no setting up back-end servers, no random Python script collected from internet to run your camera on Raspberry pi! Cool, nah?

In case some of you remember, around May of this year, Google had opened it’s repository for Raspberry Pi 3. A few of us were thinking whether this is going to be a new OS (named FUCHSIA) or any new library or something. And now here it is, Android Things!

P.S. just in case you want to know:

In this blog, I’ll try to summarize the whole official documentation as well as try out a few projects too!


What is Android Things

Android Things is the new support from Google for running Android Code with it’s official SDK on the supported low-cost hardware devices of your choice. As it’s said in their official site, with Android, you can now…

Build connected devices for a wide variety of consumer, retail, and industrial applications

It provides you an official SDK, that you can use to write pure simple Android codes, that’ll run on your hardware devices. Play with as many sensors as you want, connect them to your hardware and read/write data as you wish using the official SDK. It’s that simple! And we’re going to dive in deep with Android Things in this tutorial!

Why to try

What are you and what do you want?

  • Have you worked or willing to work on IoT projects?
  • Are you an Android enthusiast?
  • Do you love exploring new SDKs?
  • Are you ready to get your hands dirty with pure Android coding as well as the complexities of hardware projects?
  • Do you just have a Rapberry Pi lying around you :D ?

If any of these above points attract you, you are ready to try out the new SDK.

I’m a full time Android App Developer, I just feel home working in Android projects! And coming from an Electrical Engineering background, I was very much accustomed to Hardware projects. Seemed like I was ready to play with the newly released official SDK from Google for Android Things.

Earlier while I worked on Microcontrollers as well as Arduino, I had to write codes in C/C++, or while working on fun projects in Raspberry Pi, I had to write and sometimes hack around randomly found Python codes from the internet. There was no officially supported platform to play it all.


Android Things Hardware: what you need to know

There’s a two basic hardware requirements for trying out Android Things. Here’s a list:

  • Physically have access to any of these three officially supported hardware devices: Intel Edison, NXP Pico i.MX6UL or Raspberry Pi 3. Along with it’s required periherals, like HDMI Cable, power cable etc.
  • Sensors, switches, breadboards, LEDs, wires, just regular hardware peripherals and sensors as your wish and your project requires.

If you’re still not confident about your hardware knowledge, be sure to look at the official Hardware 101 page. It’s clearly explained and written in a way so you can kickstart your Hardware skills very fast.

More information can be found here.

This year, my dear wife gifted me a Raspberry Pi 3! I worked on a few fun projects with it, just using as a Media device, streaming video from webcam, just common stuff found at Internet! This seemed like the perfect opportunity to try out different things with it!

Android Things Software: what you need to know

The software part is provided by Google and it consists of only the official Things Support Library. The Things Support Library can be added to any Android project via the following gradle dependency:

dependencies {
...
provided ‘com.google.android.things:androidthings:0.1-devpreview’
}

The total Support Libraries (not just this dependency library) consists of two new APIs to interact with Hardware: Peripheral I/O API to communicate with Input-Output ports of Hardware, and UserDriver API that works withn existing Android Frameworks and eases use of regular Android APIs in Android Things project.

From official doc:

Peripheral I/O API
The Peripheral I/O APIs let your apps communicate with sensors and actuators using industry standard protocols and interfaces. The following interfaces are supported: GPIO, PWM, I2C, SPI, UART.
See the Peripheral I/O API Guides for more information on how to use the APIs.
User Driver API
User drivers extend existing Android framework services and allow apps to inject hardware events into the framework that other apps can access using the standard Android APIs.
See the User Driver API Guides for more information on how to use the APIs.

More information can be found here.

PS. The Things Library is still in it’s developer preview, not yet release publicly! So expect a few hiccups!

Points to remember

If you’ve come upto this point, I expect you’re prepared to get your hands dirty with Android Things. That’s good! There’s just a few key point to note down here:

  • You cannot use all Android APIs. Avoid using common intents as well these APIs: CalendarContract, ContactsContract, DocumentsContract, DownloadManager, MediaStore, Settings, Telephony, UserDictionary, VoicemailContract
  • While Android Things support regular UI Toolkit (that means, you can definitely build a layout for your activity), but it’s not compulsory. Display is optional here, and you’re encouraged to avoid displays as the projects are not meant for that! (The activity class does not extend AppCompatActivity, rather plain old Activity.)
  • There’s no Runtime permissions as it doesn’t have regular GUI to ask your permission. All permission noted at AndroidManifest are granted.
  • And there’s no notifications as well as Notifications API too in Android Things.
  • Unlike your mobile phone, Android Things doesn’t have a Launcher App installed on it. It does not open a launcher app on it’s startup, like vanilla Launcher, or Nova Launcher does on your Android phone. So, you need to tell in your AndroidManifest file which app to load at startup.
<application
android:label="@string/app_name">
<activity android:name=".HomeActivity">
<!-- 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>
  • Not all Google APIs are supported, only the relevant ones. Here’s the official list from Google.

Peek here to know a bit more.


Getting our hands dirty

Okay, it’s time to start our first project. As I personally own a Raspberry Pi 3, I’ll be writing steps with it. I’m pretty sure the other devices would have steps similar to that. In the prologue, we will just make our Raspberry Pi 3 ready to be working!

  • Download latest System Image from here. It’s going to be flashed in your RPi’s memory card. I downloaded the RPi’s one, it’s around 250MB.
  • Extract the zip to get system iso file.
  • Enter your SD card (8GB or higher, Class 10 preferred, will work as our RPi’s hard drive) in PC. Format it using SDFormatter and flash the iso file on the SD card using Win32DiskImager.
  • After flashing done, insert the mem card into RPi, connect HDMI cable with it to display and connect a LAN connection to it. (For some reason, I couldn’t get it to work with WiFi. But hopefully, I will solve that soon and update it here)
  • It should start with showing some logs and finally display the Android Things wallpaper.
Yeppiiee!!
  • It should display it’s IP address on the screen, as we connected our LAN connection to it. Open a command prompt or use the Terminal in Android studio. Use the command:
adb connect <ip-address>
  • This should connect the RPi as your debug device. Type it to check if the device is connected:
adb devices

First project: Showing logs

In the first prologue, we checked if RPi can be connected for testing. Now, we’ll run a project that just prints logs.

  • Download sample starter project template from here.
  • Update your SDK tools to version 24 or higher.
  • Update your SDK with Android 7.0 (API 24) or higher.
  • Open in Android Studio, let gradle sync.
  • As you can see, it already has dependency added:
dependencies {
...
provided 'com.google.android.things:androidthings:0.1-devpreview'
}
  • It also has the necessary AndroidManifest.xml changes:
<application ...>
<uses-library android:name="com.google.android.things"/>
...
</application>
  • It also has the intent-filter tags as specified earlier in AndroidManifest.xml.
  • In case you’re writing your own project from scratch, remember to add all of these.
  • Run the project. It should prompt you with a device, unknown IoT RPi or something similar.
  • After successful run, it show show life-cycle events in Logcat in Android Studio. Also, the project should start with a blank page on the connected RPi.

Second project: Blinking LED

Blinking LED project is the “Hello World” project of the Hardware world! So, we needed to know if the RPi can execute a command as simple as blinking LED with Android. The core logic is simple:

  • Connect the circuit as shown in the diagram. The output PIN is BCM6 of RPi, which can be seen here.
Poorly Photoshopped Ckt Diagram… :(
  • Download the project from here.
  • In the Blink app of the project, you will see, we just get a reference to the output PIN, change the value continuously with runnable:
PeripheralManagerService service = new PeripheralManagerService(); 
try { 
String pinName = "BCM6";
mLedGpio = service.openGpio(pinName);
mLedGpio.setDirection(Gpio.DIRECTION_OUT_INITIALLY_LOW);

// Post a Runnable that continuously switch the state of the
// GPIO, blinking the corresponding LED

mHandler.post(mBlinkRunnable);
} catch (IOException e) {
e.printstacktrace();
}
private Runnable mBlinkRunnable = new Runnable() { 
    @Override public void run() { 
// Exit Runnable if the GPIO is already closed
if (mLedGpio == null) { return; }
        try { 
// Toggle the GPIO state
mLedGpio.setValue(!mLedGpio.getValue());
Log.d(TAG, “State set to “ + mLedGpio.getValue());

// Reschedule the same runnable in
// {#INTERVAL_BETWEEN_BLINKS_MS} milliseconds
mHandler.postDelayed(mBlinkRunnable,
INTERVAL_BETWEEN_BLINKS_MS);
} catch (IOException e) {
Log.e(TAG, “Error on PeripheralIO API”, e);
}
}
};
  • Run the app named “Blink”.
  • Voila!

Third Project: Doorbell

I started the project on the officially provided project Doorbell.

The Android Doorbell sample demonstrates how to create a “smart” doorbell. The sample captures a doorbell button press from a user, obtains an image of the user via a camera peripheral, processes the image data using Google’s Cloud Vision API, and uploads the image and event data to a cloud database where it can be viewed by a companion app.

But somehow my Camera permission always gets denied, so I can’t access the camera. (I know, I said all the permissions are granted as the project is built!) I’m working on this issue, and will update this article once I solve that!

UPDATE: Mistake on my part, I just rebooted the project again and everything was working fine! I finally got to capture videos via camera hardware attached to RPi. But as I didn’t want to use Google Cloud Vision API, I reduced the code to just show the output of the camera on screen with on-screen GUI clicks.


Conclusion

Trying out Android Things was fun! Earlier whenever I thought of IoT projects, I needed to prepare for long nights of C/C++ coding, or Python coding! Not that anything is wrong with that, just that I’m much more comfortable in Android rather than C/C++. Hopefully I’m not the only one! Get a Raspberry Pi, try out Android Things, it’s actually fun!


This ends the blog on Android Things, Google’s new IoT platform. Hope you liked it as much as I did!

Feel free to comment, like, recommend, share! And follow My Online Cafe to get more updates on Tech Stuff!