LEDs
If you are familiar with how Arduino code is structured, you may be expecting to have a setup
and a loop
methods. Android Things can work like that too. We will start with our methods named using that convention to provide a common ground if you have experience with Arduino. However, Android Things allows us to write code in a much more structured way. In this section we will start with something close to the Arduino way, explain its drawbacks, and show better alternatives, showing the different ways to blink an LED.
Note
Blinking an LED is the IoT equivalent of Hello World
.
Let's get into blinking an LED the Arduino way.
The Arduino way
The simplest way is to have a setup where we initialize the LED, followed by a loop that toggles the state and then waits.
The code for that looks like this:
class MainActivity : Activity() {
private lateinit var led: Gpio
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setup()
while (true...