It’s been awhile since something really big has happened to Android, but with Android Things we’re going to start seeing Android in a lot of places it’s never been before. Android Things is an Android operating system for the Internet of Things (IoT). It provides an SDK to help interface with IoT devices, and let’s us program using standard Android components like Activities. In this post we’re going to setup a Raspberry Pi with Android Things and turn it into a Bitcoin price tracker.

Setting Up the Hardware

Items you’ll need for this project:

A small computer, like a Raspberry Pi

A project kit, which includes:

– Breadboard

– Alphanumeric display

– Jumper wires

To get started with Android Things you’ll need to have a small computer like a Raspberry Pi or Intel Edison. Check out this link to see what hardware is supported and where to buy a dev kit; I ended up going with the Raspberry Pi 3 and Project Kit both from Adafruit.

Once you’ve got your kit, follow these steps from the Android developer site to get the Android Things OS onto the SD card (this is referred to as “flashing the image”). And if you run into an issue unzipping the image archive, like I did, this StackExchange post might be the answer.

Connecting to the Device

With the image flashed, it’s time to connect the device to the internet, and although we’re really just looking for Wi-Fi, we need to start with a wired connection. So make the 3 connections shown in Google’s diagram, and then you should see something like this on your display:

So using my screen as an example, you would then connect to your device by using ‘adb connect 192.168.0.16’.

Once you’ve successfully connected to your device via ethernet, the next step is to set up its Wi-Fi connection:

$ adb shell am startservice \ -n com.google.wifisetup/. WifiSetupService \ -a WifiSetupService.Connect \ -e ssid < Network_SSID > \ -e passphrase < Network_Passcode >

And once the Wi-Fi connection is setup, take note of the Wi-Fi IP and try to connect to your device over Wi-Fi: adb connect <Wi-Fi IP>.

At this point, if you’re feeling cramped for space, you can feel free to unplug everything and move the device to a better work area; it’ll remember the network settings and you don’t need the ethernet or HDMI cords any more.

Another thing to mention is that when the device is turned off and on it might not keep the same IP address. So you may need to look into your router settings to see what IP address it was given (or just start guessing IP addresses). When I plugged my Raspberry Pi in, after being unplugged for a couple days, the IP address was 192.168.0.14.

Building the Bitcoin Price Tracker

Now that we’ve got a Raspberry Pi running Android Things and we can remotely connect to it, it’s time to make our first project: a Bitcoin price tracker. But before we write any code we’ll need to connect the Raspberry Pi to our alphanumeric display so we have a place to show the price. Also, at this point, you’ll probably want to have a pin diagram handy to help figure out which pins are which on your Raspberry Pi.

Connecting the Display

So first things first, let’s plug the display into the breadboard:

Next, we need to make connections to each of the 5 pins on the top of our display: +, +, -, D, and C. Let’s start with the two + connections. The + sign indicates that this is where we should connect the power. So we’ll want to start by using an F/M jumper wire to jump the power from the Pi to the + row of our breadboard. Then we’ll use two M/M jumper wires to connect the + row of our breadboard with the two + pins of our display:



* I’m using 3.3v, but you could also use the 5v option to have a slightly brighter display



Moving on to the – pin, the – sign tells us that this pin should be connected to the ground. So let’s use an F/M jumper wire to connect our Pi’s ground to the – row of the breadboard. Then let’s connect the ground, which is now the – row, to the display, and it should look something like this:

Finally, we just need to connect the D and C pins to our Pi. These pins stand for ‘Data’ and ‘Clock’, and they’re used to help our display communicate with our Pi by using the I2C protocol. Luckily, we don’t need to know much about I2C to use it. We just need to know that ‘Data’ should connect to the SDA pin, and ‘Clock’ should connect to the SCL pin. So let’s consult a pin diagram, and then use two F/M jumper wires to connect SDA to D and SCL to C:

Making the Android Project

Now that we’ve got everything all wired up, it’s time to write some code! Inside Android Studio, start by creating a new project; I’ll name mine, ‘BitcoinTracker’. Then choose a min SDK version of at least 24, choose the ‘Empty Activity’ template, and since we won’t be needing a layout be sure to uncheck the ‘Generate Layout File’ box.

Inside the project, we need to add the Android Things library. To do this, head over to the app’s build.gradle file and add the Android Things dependency:

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

Then, jump over to the manifest and add a library entry inside the application tags. You’ll also need to make some changes to your intent filters and add the internet permission:

Awesome, we’re all setup with Android Things! But before we start coding we’ll need to add a driver for our display. Without a driver we’d need to write a ton of low-level code before we could display anything. Luckily, Android Things already has a bunch of drivers to help us deal with all kinds of common hardware components like our alphanumeric display. So inside the app’s build.gradle file let’s add:

compile ' com.google.android.things.contrib:driver-ht16k33:0.1 '

Then build the app, and back in MainActivity let’s start by declaring a field for our AlphanumericDisplay. Then let’s set up our display in the onCreate method:

* I2C1 comes from the pin diagram

Next, we need to be sure that we’re closing our display when we’re finished with it:

And now, all that’s left is to call alphanumericDisplay.show while passing in the current Bitcoin price.

Getting the Bitcoin Price

To help us get the price, we’ll be using the GDAX API. Specifically, we’ll be using this URL which always returns the current Bitcoin price: https://api.gdax.com/products/BTC-USD/ticker.

To help us handle our HTTP requests we’ll be using the OkHttp library. You can add it to the project as a dependency by using:

compile ' com.squareup.okhttp3:okhttp:3.5.0 '

Then, back in MainActivity, we need to declare a couple new fields. We’ll need one for our OkHttpClient, and one for our Request:

Then we need to create a function to retrieve the Bitcoin price and update the display:

* Callback is from okhttp3

And we’ll also need a method to periodically call the previous function to update the price every 30 seconds:

Finally, to get things started, we just need to call this method after we enable the display in onCreate:

... alphanumericDisplay.setEnabled( true ); runForever(); ...

Then, so long as you’re connected to your device, you should be able to choose it in the device dialog and run the app!

I hope you liked this post! And I hope you’re as excited about Android Things as I am. It’s a really cool platform, and with the recent release of the Google Home there’s a lot of potential here!

Also, if you’re looking to learn more about Android development, then I suggest you start right here. We’ll take you from novice to Android developer one step at a time and explain everything along the way. On the other hand, if you’re looking for something a bit more advanced then I strongly encourage you to check out our Kotlin course. It covers everything you need to know about Kotlin, and it does so by building a Solitaire app!

Helpful Links:

– Hardware 101

– Building Your First Device

– Peripheral Drivers

– Java Source Code

– Kotlin Source Code (uses Fuel instead of OkHttp)