Android Things architecture
Before we get our hands on into the code, let's explore the overall architecture of Android Things and present a few concepts that we will be using throughout the chapter, such as the best practices to handle the peripheral lifecycle and the concept of user space drivers.
Peripheral lifecycle
Whenever we want to use a peripheral, we need to get access to it before we can do anything with it. This is done using the open
method that all peripheral classes have. Opening a peripheral gives us exclusive control over it and therefore it can not be opened if it is already open by another app. The operating system will throw an Exception
if the peripheral is already in use.
As you can imagine, this also means that we need to release the peripheral once we are done with it. This is done by invoking the close
method.
Note
Unless it is a special case, you should open
all your peripherals inside onCreate
and close
them inside onDestroy
.
In general, you will want to open the peripherals...