Building Production IoT Devices with Android Things (GDD India ’17)

Building Production IoT Devices with Android Things (GDD India ’17)

[MUSIC PLAYING] WAYNE PIEKARSKI: All right. So, welcome to day two. I hope you’re having
a great time here. So, my name’s Wayne Piekarski. And today, I’m
going to be talking about how to build production
devices for Android Things. And so, Android
Things is designed for making IoT devices. We’ve got a big sandbox
area over in the room that you can go and
check out our demo, so definitely come
and see us later on. And we’re here because
we want to educate you about what we’re
doing, but also we want to find out more about
what kinds of cool projects you want to build. We want to answer your questions
and so forth, so definitely come by and see us. Check out the demos. Talk to us. And that’s what
we’re here for, so if you see me walking
around at lunch, definitely come and say hi. So, if you ever dreamed
of making your own devices then Android Things is for you. And today, I’m going to teach
you how to go about doing that, so you can make your
own exciting ideas. So, what is Android Things? So, Android Things
is an extension of the Android
platform that allows you to build IoT devices. Many of you are
familiar with Android on phones, Android on tablets,
we have Android Wear devices. We have Android in cars. And now we have Android
for booting IoT devices. And so, it’s nice because
you’ve already got your existing developer experience. Many of you in the room here
are already Android developers, and so, you can take advantage
of your existing software stack to support
your development. Also, security is really hard,
and a lot of IoT device makers aren’t doing security properly. And so, it’s really hard to
get it right from the beginning and do it perfectly,
so Android Things is there to support that to
help you build really secure IoT applications. And finally, taking
an IoT device and scaling it to
large production runs is also very
challenging as well. And so, while it’s possible
to make easy prototypes. Android Things helps you to
get that product all the way to market, and we’re going
to go through the steps as to how that’s done. So, with Android
Things, you get best in class multimedia,
connectivity, Wi-Fi, rich user interfaces, and it helps to
accelerate the development of IoT applications. So, if you want
to build anything like this, that’s
definitely something you should be checking out. So, what can you use
Android Things for? There’s many things
you can use it for, but really, it’s ideal for
powerful and intelligent devices. It’s not just about little
things that support switches, and lights, and
whatever, but it’s about really complex
applications. Things like audio
processing, video processing, and doing on-device
machine learning, these are the kinds
of cool things that you can build
with Android Things. And there’s many
different use cases. You’ve got enterprise
like big companies. You’ve got businesses. And then you’ve got
consumer devices that people are
going to use at home. IoT’s going to be a big thing. And if you saw my
talk yesterday, I talked about how we
had desktops, and then laptops, and then phones. IoT is going to be bigger than
even those, so it’s definitely something that you
want to be a part of. And so, Android Things is
really good at applications that need, like, large
amounts of computation. An Android Things
device is basically a phone without a
screen, so this is not a tiny microcontroller. It’s very powerful. And you can communicate
with the Cloud, and there’s all kinds of cool
things you can do with it. So let’s get started. Making hardware– IoT devices
involve some kind of equipment that you’re going
to have to give to a customer of some kind. So, it’s really hard to
make good electronics. And so, the reason
is that when you’re making a board like this–
so this is a little screen shot of a Raspberry Pi here–
the circuit board of it. If you look at the
actual board, itself, where that red
arrow is, you’ll see there are these little
squiggly tracks there. And it turns out that when
you’re designing high speed electronics, you have to do a
really good job about making sure that all the electrons
travel at the speed of light correctly, and that they
will arrive at the same time. And so, doing this
is really tricky. It requires very highly
skilled engineers who know how to design electronics. And, you know, I’m
a software person. I don’t make hardware, so I have
no idea how this stuff is done. And so, we want to
try to make that easy. It requires very
skilled designers to make really nice
electronics, but we’re going to show you how
we help you to do that. Also, when you’re
making electronics, it requires you to
buy large quantities to be able to get good prices. If you go to an electronics
company, and say, I want to make 10
of these devices, they’ll charge you,
like, $1,000 each. But if you want
to buy a million, then you can get the price
down to a couple of dollars. And so, that’s one
of the other catches, it’s not like software that
you can make one or a million copies for free. Hardware, it costs
money to make them, and sometimes there
are fixed costs. And then finally, you’ve
got a lot of lead time. When you make
electronics you have to place the order for the
parts, wait for them to arrive, you have to make
things, assemble it. It takes a long period of
time to get this right. And if you make a mistake, you
can’t just ship an update out like you do for software. So, hardware is
quite tricky to do, but we’ve got some solutions
to making that easier. And finally, software, which we
thought was a solved problem, it turns out that’s tricky too. So, everyone is
building these custom IoT Devices with their own
handmade Linux distributions, but they don’t really have
a strategy for security. They don’t have a strategy for
sending updates out to people. And this is becoming really
critical for IoT devices, because now there are these
vulnerable insecure devices that are out there in the wild
that hackers are breaking into. And it’s becoming a
problem for companies who make these devices,
because, well, your device was used by someone to
break into something. So, you’re potentially
on the hook for that. So, as a device
maker now, there’s more and more incentives
for you to do this right. And so, Android Things
is going to help you to do security, and
updates, and all these things properly, so that you can make
devices that your consumers can feel confident with– knowing that it’s made as
best as can be done right now. And finally, you
don’t really want to have to hire a huge
number of engineers on your team, whose
job is to maintain Linux kernels, and
security updates, and keep up to date with the
latest patches for open SSL or something like that. We take care of
all that for you, and that’s a really
big thing here. So, we support the ability
to make easy prototypes, but we also support the ability
to then take that same design and take it all the
way to production. And that’s that
we’re going to talk about for the rest of this talk. Firstly, the design
of Android Things– it’s not just based on
a single board computer. It’s based on this
concept called a SoM. And if you look at the
bottom on the slide there, you can see there’s
a small little board. It’s about 2 centimeters
by 2 centimeters. It’s very tiny, but it contains
the core computing pieces that are needed– so, the CPU,
the memory, the Wi-Fi chip, and so forth. And you attach this board
to a larger breakout device during development,
the top one there, and that has all the
breakout connectors that are used to attach things to. And that’s for testing. And so, when you’re ready
to move into production, you take the
developer board away, and you make your
own custom board that has just the
components that you need. And this is nice because the
custom board that you make only has the pieces you want. You only pay for
the parts you need, and you don’t need to pay for
this huge development board that you’re not using
any other parts for it. And the nice part
is that the SoMs, the Small Computing Modules,
they’re made by someone else. They’re made in
large quantities, so you can buy them
at good prices. And then the boards
that you need for yourself, you can do a run. And because they use low speed
electronics that’s not as hard as the design I
showed previously– low speed electronics are
a lot cheaper to make, and it’s a lot easier for a
lot of electronics designers to build for you. So, you can focus on that part,
and it makes it easy for you. And then finally, we
have what’s called a BSP or a Board Support Package. And so, the board
support package is software from
Google that makes that SoM and that
board of yours work by running Android Things on it. We provide the kernel, the
libraries, necessary drivers, and everything to make it work. And as long as you stick
to the design of the SoM, then everything
will work for you. And so, it really sort of
helps you to focus on– you make your board,
and then we take care of everything else for you. The other thing
that’s nice about SoMs is that you don’t need to
worry so much about product certification and testing. Traditionally, when
you design a board, you have to get the entire
board certified and tested. There are regulatory agencies. Like in the US it’s called
Underwriter’s Laboratory. They do testing or
FCC certification. And doing all that
certification is very expensive, and it’s very time consuming. However, if your design
is based around the SoM, the SoM has already
been certified, and so, it makes the
certification process for the rest of your hardware
a lot cheaper, quicker, and easier. So, it makes things
easier for you, and you can get your
product to market quicker. So, with the way we’ve
designed Android Things, it’s a little different than the
way people have traditionally used AOSP to build their
own custom Android devices. What we do is we’ve
broken Android Things down into two parts. There’s the part that’s
managed by Google. We compile that. We build it. So, it’s things like the
Android framework, the hardware libraries, the Linux kernel. We build that. We give you
binaries, and they’re distributed as part of
the board support package. And then the second part is
managed by you, as a developer. And so, you build APKs like you
would build in Android Studio. And you write what
are called apps. And you write user drivers. And you put them
onto the device. And that’s the part
that you control. And we’ve separated them,
so that the part that’s managed by Google has
all the right permissions to do everything. But your part that you run as
a developer, that’s actually run with restricted permissions. You have to use the Android APIs
to communicate with everything, which helps to isolate your code
from the rest of the system. So, that if there are
any security problems, it tends to keep them
compartmentalized, so the whole system
won’t get attacked if your app has a problem. So that’s one of the nice
things about this separation is we handle certain parts. You handle certain parts. And you can focus on the parts
that are important to you. So, what happens is that when
you upload an APK that you’ve built, you upload it
to a developer console, and it’s combined with the
firmware image from Google. And we build a single
signed firmware image. And this signed
firmware image is then distributed to the devices
that are your products. We sign and verify the images. So, we sign them on the server. And then we verify them when
the images arrive on the device. And we do this to make sure the
images haven’t been tampered with during transmission
to the user, because we don’t want
a rogue third party changing the firmware on
the way to the device. They are also protected
from corruption, so that if the
images are corrupted during transit, then of course
they’ll be verified that way. But also, when the
device boots, we check to make sure it hasn’t
been tampered with or damaged on the device as well. Because you don’t
want to boot an image that’s been corrupted
or tampered in some way. The next thing is we provide
A/B update rollback protection. When a new update
arrives on the device, we don’t just clobber
over the firmware image. We actually stage it
as a second image. We put everything in place,
then we reboot the device and switch over to
the other image. Now, if for any reason,
when we boot this new image, if it fails, the system
will then detect that, and it will then roll back
to the previous version. And that way, if a
corrupt build is pushed, or if you make a bug
or something that causes the device
not to boot, it will roll back, so that
the device will still be able to get future
updates, and so you won’t brick the device
if somehow you make a mistake with your updates. This is a really nice feature
because it’s something that’s really hard to build yourself. We provide all that for you. And then, finally, we provide
automatic security updates. So, Android has a huge
team of security engineers whose job is to look for
vulnerabilities in Linux kernel, and SSL libraries, and
all the different pieces that make up the Android framework. And you get the benefits of
having all of those engineers working for you. So, we make the updates. We then push them
out to your devices. And so, that you don’t have
to worry about keeping track of all these little things. In the past, if you were
doing bills from AOSP, you would have to merge
the patches in yourself, and then build your own
images, and then push them out. This is very time
consuming– hard to do. And so, we’re really trying to
make that frictionless and easy for you. And we take care of it all. And then, finally, we
provide the OTA mechanism to get the software updates
out to your devices. The images are signed–
built on our server. We then push them out. And it uses a system that
we’ve built for ourselves, and it works very well. It’s very reliable
and very easy to use, so you don’t have
to worry about it. You can focus on building
your applications. So let’s look at some of
the more technical things about how to build
for Android Things. The first part is that– this is, like, a diagram that
shows the traditional Android framework model. There’s all the different
parts that form the system. We’ve kept most of them
as part of Android Things. But what we did was we removed
some of the core pieces that aren’t needed. So, for example, an
Android Things device, it’s not a phone, and it
can send text messages. So, we’ve removed all of
those different pieces like wallpapers
and notifications. You don’t need any of that
stuff on an IoT device. We’ve taken all of that away
because we want to save space– make the system boot quickly. But we’ve provided all of the
rest of the Android framework in bits and pieces that
you’re used to dealing with. That’s all available for
you to use when you’re building your IoT devices. And also, the UI
libraries that you’re familiar with in Android
Studio for, like, laying out nice UIs,
that’s all available too. Android Things supports UI,
but displays are actually optional now. So I always think about
Android Things as like a phone but without the screen on
it, but you can have a screen if you want one. So, different IoT
devices, some of them are embedded into different
things, or some of them have displays. You can choose what
you want, so you don’t have to have
a screen, but you don’t have to have one either. You have the same user
interface libraries that you used to with Android. And so, it’s based on
activities and services just like you’re familiar with
for writing Android apps right now. So, all of the same things
that you understand, the same concepts are
there for you as well. And one little
thing to be aware of is that you still need
to have a main activity, because the main activity
is responsible for handling keyboard, and mouse
events, and so forth. So, you still need something
to be handling that. Because screens
are optional, there are times when, if you need to
request permission for example, we don’t pop up a permission
dialog box on the screen– because it might not be possible
for the user to hit OK on it. Android Things automatically
grants permissions to different things. If you’ve got code that you’ve
ported over from an Android phone, it still works,
and the permission’s automatically granted,
so you don’t have to hit the OK button on it. And then, finally, when you’re
building an Android Things IoT app, you’re not
necessarily going to be using a touch
screen anymore, but you need to think about
what kinds of alternative input device modalities
do you want to use. So, now you have the ability
to build wearable devices. You could have a microphone. You could have a camera. You could have a GPS
sensor or a compass. There’s all kinds of
different input modalities that you could consider as
part of building your device, so don’t just lock yourself
into using a touch screen. You could use any
kind of input device. And so, pretty much all the
options you could think of are available. And the Android SDK through
the framework– so, things like sensors and so forth are
read in exactly the same way as you do on a phone. And then, also,
you get to develop with the power of Android. You’re using the
same Android SDK that everyone uses
for phone development. You’re using Android
Studio, and you get to use any language
support of Android Studio. So, you can use Kotlin
for programming. So, if you’re
excited about Kotlin, you can use it on
Android Things as well. You have Google Play
services with all the parts that it provides– Firebase, and TensorFlow,
and Google Cloud IoT. And you can embed the Google
Assistant into your device. So, all of these things that
Google provides for phones are now available for you
on IoT devices as well. I’ve had a lot of IoT
developers come up to me and said they’re really
excited about being able to put TensorFlow
into devices like this. This is something I haven’t
been able to do before. One of the really cool
things about Android Things is that you can now do
machine learning on device. You don’t need the cloud. You don’t need Wi-Fi. And you can do it on the device. And that’s something a very
low end micro control can’t do, so it’s definitely a big
win for Android Things. And as I said, it
really helps you to– all of these tools and libraries
that you’re familiar with, it’s all available to you. The most important
part of Android Things is adding hardware to it. And so, let’s go through
some examples of the APIs that are available for you
to go about doing this. When you look at the
developer board– this is a Raspberry Pi here. We actually support NXP
devices, which some of you have received a kit that
you can play with at home. So, we support NXP devices. We also support
Raspberry Pis right now. And so, this board
here shows you some of the different
connections that are available. We support connecting
things by USB, so you can plug mice,
keyboards, things like that in. We support speakers,
microphones going in via either the audio
jack or the USB port. There’s an ethernet connector. There’s Wi-Fi. There is different HDMI outputs
and connectors for cameras. We support all of these
things on the module, so whatever you want to
use to connect stuff up, you can use that for
whatever your application is. And every developer kit also
has a breakout header pin. This is the connector pin
diagram for the Raspberry Pi. You can see that there’s all
kinds of connectors, which we’ll talk about in a minute. But every single pin
that’s available on there, we have APIs that allow
you to talk to that. And so, you can add additional
devices to these lines here, and then you can integrate
them in whatever way you want. So, there’s a whole bunch of
different interfacing protocols exposed by that connector there. We support general
purpose I/O. We support pulse width modulation. We support I2C buses,
I2S, SPI, and UIs. So, these are all different
connection technologies that hardware design is used to
interface to different devices. If you’ve done hardware design
before, you’ll have seen these. But these are
important because they are very cheap and high
speed ways of getting things connected
to devices that’s easy to interact with directly. And we’re to go through some
more examples of these later. And then, also, we have support
for what’s called user drivers, where we can add to
the Android framework, and we can take sensor data. We can take input devices. And we can actually
inject those events into the Android framework, so
that they can be read and used by applications using the
same sensor framework. So, if you’ve got a sensor
that connects via I2S, we can inject it into
the sensor framework, so that the code
that uses it just sees a regular Android
sensor just like they would to any other sensor that
would be connected on a phone. So, here are some
examples that show how different kinds of
things would be connected. If we had a motor that
was connected up by PWM, that is something that would
talk directly to your app. If you had a sensor, you can see
that the example I talked about before, it connects via I2C. It, then, has a
user sensor driver. It then puts it through
the sensor manager in the framework. And then if you
had a GPS device, you would connect it via
serial port or a UART. You would then write a
GPS driver around it that generates framework events. And then it goes through
location manager into the app. And actually, an
interesting thing is that Google Play services has
something called Fused Location Provider that a lot of people
like to use because it improves the quality of the GPS data. Because these events are
being injected directly into the framework, it means
that Google Play services can use the Fused Location
Provider to help enhance those as well. It’s just one of the really
cool spinoffs of the user driver framework. So, what kind of hardware
can you interface with? We talked about some concepts
like GPIO and so forth. I’m just going to show
you some quick examples. So, GPIO is basically
binary one and zero signals, and they tend to be, like, 0
to 5 volts or 0 to 3.3 volts. And they have low
and high values. They’re used by
things like buttons when they’re inputting
into your device, but you can also
drive LED, so you can make lights turn on and off. Rotary switches and
little sensor devices, they all use just simple
signals coming in, and Android Things can
read those for you. We support what’s called PWM. The nice thing about
pulse width modulation is you use signals with
varying modulation widths, and you can use it to control
the intensity of an LED, or you can use it to move
a server motor around. We have native support for PWM. And the boards have actual
PWM hardware on board that can generate these
signals very reliably, so they’re useful for motor
controlling and things where accuracy is very important. We have UART support,
which are also called serial ports or RS232. So, you can connect them up. And a lot of devices like
GPS units and so forth, they use UART style input. All the Android
Things devices all have at least one serial
port connector on it, and you can connect more by USB. We also have SPI and I2C. There are companies that
make dedicated little boards with sensors on
them and so forth that talk via these
high speed serial buses. These are useful for times
when RS232 isn’t fast enough. And so, you can connect
all kinds of sensors directly to Android Things,
and we handle those directly. So, that’s sensors. Now, how do we go about
building activities within Android Studio that run
on an Android Things device? Everything in Android Things
is based around an APK just like you would build
for an Android phone. Traditionally, you would plug
your phone into your laptop. You would hit the Run
icon in Android Studio, and it puts the
APK on your device. You unplug it. You plug in your
Android Things device. And it works in
exactly the same way. So, you get to use exactly
the same Android Studio tools. And you just plug it in. It works over ADB. And it pretty much just works. There’s a few little differences
you need to be aware of though. The first thing you do
is in your [INAUDIBLE],, you need to include our
Android Things support library. It has a few extra
things that are not part of the standard
Android framework such as the peripheral
IO API, and it brings those dependencies
in, so that you can use them in your code. The next thing you need to
do is– on the bottom there, you can see we’ve got some
Android manifest XML entries. You need to add an IoT launcher
intent flag and an intent filter. And we do that to tell Android
Things that activity that’s tagged with an IoT
launcher is the one that we want to start up at boot. The device boots up. It finds the first IoT launcher. It starts it up. And then that runs on the
device from that point on. And you should only have
one activity on the device with an IoT launcher. If you have two, then only
one of them will start up. But it’s the way of you
flagging which activity you want to run on
startup, and that’s going to be the one that
sets up all of your devices and gets things going. But at the end of the day,
it’s a regular Android APK. And in many cases, I’ve taken
small toy examples of mine, and I’ve run them on
an Android Things kit. It just works. So, a lot of things that
are used too for my Android, you can just easily do
without changing anything. So, if we want to
add peripheral I/O– we talked about GPIO. So, let’s say we want handle
button presses from a switch that’s connected to the device. We wire the switch up to
the GPIO pin on the device. And this is the code that
we then use to do it. It’s very similar to handling
a button in an Android UI except now we’re handling
real physical buttons. We use what’s called the
peripheral managed service. We create one of those. We, then, call open GPIO
with the name of the pin. We, then, call some methods
that set the directions. We want to do input. It’s very important to
specify the correct direction, because you can’t read on an
output pin and vise versa. We, then, also
specify edge falling. The purpose of edge
falling is to say, I want this code to trigger
whenever the signal drops from high to low but
not from low to high, so it only goes on
the falling edge. You can specify rising
and also both depending on what your application is. Then you register a callback. And you can see we have
an on-GPIO edge method, and that method will be called
whenever the signal drops. And so, that’s it. I mean, you’ve got a couple
of lines of code here. Most of it’s kind of boilerplate
that just print something out when a button is pressed. And then you can do whatever
you want once you’ve received that notification. And then you can do
different things in your code depending on what
you want to do. And all of the peripheral
I/O devices like SPI, I2C, all that, they’re
all implemented with a very similar API as
well, so it’s very easy to use. If any of you’ve done
embedded Linux before, you have to go through
the [INAUDIBLE] file systems looking for
different files to twiddle. This is a lot easier, And it’s
handled by a dedicated service that’s isolated
from your process, so that it helps for
security as well. So, we talked about
drivers and user drivers. We also have a peripheral driver
library that we’ve written. Some of the engineers on my team
as well as people out in public have been writing drivers
for different devices that plug into Android Things. On GitHub, we publish
all of these drivers, so they’re available for you. Many common sensors, and
devices, and so forth, the drivers are
already available, so you don’t even need
to write them yourself. We’ve handled a lot
of these things, reading the data sheets,
implementing the code for it, so you don’t have
to worry about. Definitely check out
that driver library if you want to find
examples of code that makes it easy for
you, so you don’t have to worry about
reading these data sheets. So, in summary,
Android Things really helps to provide
the power of Android to help you make
your applications– help you make them
easier and faster than you could have done before. You don’t have to
learn a new tool chain. You can use all your
existing experience. And it really is something that
you can get started quickly. So, if you’re an
Android developer, you could take one of our kits. You can go home, and
you can have something working in a couple of hours. You can really get
started quickly, so that’s definitely something
I encourage you to check out. The hardware and software
is managed by Google. So, these SoMs, we work closely
with the semiconductor partners to make the SoMs. We help specify what the
specifications will be. And then we support them with
kernels, drivers, libraries all bundled up into
a binary that we then combine with your APK
to ship to devices. And because we
handle all that, you don’t need to worry about
handling all these kernel changes, and patches,
and so forth. We take care of that for you. And then finally, with the
help of our developer console, where you upload your APKs to
and push them to the devices, we handle all of the updates,
and all of the security, and everything for you,
so that you can focus on building your application. There’s a lot of
complex things involved with updating devices and
making everything work. We have some of the most
amazing engineers I’ve ever worked with that talk to me
about security about Android Things, and it’s just amazing
the level of detail and care they have put into the
design of the system. It’s very nice. So, as a developer,
you don’t need to worry about these things. You can focus on making
your app and what makes your product great. So, what’s next? As I mentioned before, we
have a sandbox area outside, across the street there, where
you can check out our demos, and come chat to us, and
definitely take advantage of that while we’re here. And then, also, later on,
you’re going to have questions. We have some
communities online where you can go to ask questions. So, on G+, we have an
IoT developers community. We also have, which is a place where you can post your
projects that you’ve worked on. And that’s a great place to
sort of find other ideas of what people are working on. And you can look at their code
and read the documentation. It is a great place to see
fully built little ideas that you can study. And then also we have the
Android Things documentation itself on And then we have a whole
bunch of samples on GitHub that you can look at,
that show best practices in how to do different things
involved with the Android Things kits. And so, also, I do a lot of
posting on Twitter and Google+, so definitely follow
me on social media. I’m a developer
advocate at Google, where I talk about IoT, the
Google Assistant, and also the Assistant SDK. And so, definitely
follow me there if you want to keep up to date
with all the new things that are happening. We’re always doing new things. Like, for example,
yesterday, we actually launched Android Things
developer preview six. The latest developer
preview has a whole bunch of fixes for previous
issues, but it also adds a lot of things for,
like, making fast UIs possible, and it supports
cameras, and you can run a lot of the Android samples. So, Google Maps now works on it. It has the latest version
of Google Play services. So, definitely check
out developer preview six, which just
launched yesterday. And that has a lot in there
that you should try out. So, that’s it for today. Thank you very much for coming. And I look forward to
seeing you on the sandbox. Thank you. [MUSIC PLAYING]


One thought on “Building Production IoT Devices with Android Things (GDD India ’17)”

Leave a Reply

Your email address will not be published. Required fields are marked *