50
Android Things Egor Andreevici @EgorAnd

Android Things

Embed Size (px)

Citation preview

Android ThingsEgor Andreevici@EgorAnd

https://android-developers.googleblog.com/2016/12/announcing-googles-new-internet-of-things-platform-with-weave-and-android-things.html

● Build connected devices using familiar tools, such as Android SDK and

Android Studio

● Google Play Services & Google Cloud

● Flashable image + SDK (Developer Preview)

Android Things

Android Things Platform - https://developer.android.com/things/sdk/index.html

● Peripheral I/O API○ GPIO, PWM, I2C, SPI, UART

● User Driver API○ Inject hardware events into the framework

Things Support Library

● Missing core packages○ e.g. ContactsContract, MediaStore, Settings etc.

● Displays are optional

● Subset of Google Play Services available

● No runtime permissions

● No notifications

Behavior Changes

Hardware: Turnkey Solutions

● Certified development boards

● SoMs (System-on-Modules)○ SoC, RAM, Flash Storage, WiFi, Bluetooth etc.

● Board Support Package (BSP) managed by Google

Hardware

Supported boards - https://developer.android.com/things/hardware/developer-kits.html

Intel® Edison NXP Pico i.MX6UL Raspberry Pi 3

Coming soon - https://developer.android.com/things/hardware/developer-kits.html

Intel® Joule™ 570x NXP Argon i.MX6UL

Rainbow HAT - https://developer.android.com/things/hardware/developer-kits.html

● Hardware Attached on Top○ Seven multicolor LEDs

○ Four 14-segment alphanumeric displays

○ Three capacitive touch buttons

○ Blue, green and red LEDs

○ Temperature and pressure sensor

○ Piezo buzzer

Rainbow HAT

Raspberry Pi 3 + Rainbow HAT:Getting Started

● 8 Gb or larger SD card

● HDMI display + cable

● Ethernet cable

● SD card reader

Flashing the Image: Requirements

● Download and unzip the latest preview image○ https://developer.android.com/things/preview/download.html

● Write the image to the SD card○ Instructions for Linux, Mac and Windows

● Insert the SD card into the Raspberry Pi

Flashing the Image

Android Things Launcher

Connecting via ADB

$ adb connect <ip-address>

connected to <ip-address>:5555

or

$ adb connect Android.local

if host platform supports Multicast DNS

Connecting WiFi

$ adb shell am startservice \

-n com.google.wifisetup/.WifiSetupService \

-a WifiSetupService.Connect \

-e ssid <Network_SSID> \

-e passphrase <Network_Passcode>

Demo App

https://blog.egorand.me/making-rainbow-hat-work-with-the-android-things-2/

● Handle touch button clicks

● Change text on the alphanumeric display

● Play a melody using the buzzer

● Blink RGB leds along with the melody

Rainbow HAT Demo

https://www.youtube.com/watch?v=TErot2KaY6w

Declaring Dependencies

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

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

app/build.gradle

Declaring Dependencies

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

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

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

app/build.gradle

Creating Manifest

AndroidManifest.xml

<application ...>

<uses-library android:name="com.google.android.things"/>

</application>

Creating an Activity

RainbowHATDemoActivity.kt

class RainbowHATDemoActivity : Activity() {

…}

Declaring the Activity in Manifest

AndroidManifest.xml

<activity android:name=".RainbowHATDemoActivity"> <!-- Launch activity from Android Studio --> <intent-filter> <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter> …<activity>

Declaring the Activity in Manifest

AndroidManifest.xml

<activity android:name=".RainbowHATDemoActivity"> … <!-- 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>

Instantiating driver classes

RainbowHATDemoActivity.kt

override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState)

buttons = Buttons() display = Display() buzzer = Buzzer() leds = Leds()

...}

Creating the Buttons class

Buttons.kt

class Buttons(private val buttonDrivers: List<ButtonInputDriver> = listOf( registerButtonDriver(BUTTON_A_GPIO_PIN, KeyEvent.KEYCODE_A), registerButtonDriver(BUTTON_B_GPIO_PIN, KeyEvent.KEYCODE_B), registerButtonDriver(BUTTON_C_GPIO_PIN, KeyEvent.KEYCODE_C))) { …}

Creating the Buttons class

Buttons.kt

val BUTTON_A_GPIO_PIN = "BCM21"val BUTTON_B_GPIO_PIN = "BCM20"val BUTTON_C_GPIO_PIN = "BCM16"

Raspberry Pi 3 + Rainbow HAT Pinout - https://pinout.xyz/pinout/rainbow_hat

Creating the Buttons class

Buttons.kt

private fun registerButtonDriver(pin: String, keycode: Int): ButtonInputDriver { val driver = ButtonInputDriver( pin = pin, logicLevel = Button.LogicState.PRESSED_WHEN_LOW, keycode = keycode) driver.register() return driver}

Creating the Display class

Display.kt

class Display(private val display: AlphanumericDisplay = AlphanumericDisplay(DISPLAY_I2C_BUS)) {

init { display.setEnabled(true) display.clear() } …}

Creating the Display class

Display.kt

fun displayMessage(message: String) { display.display(message)}

Processing button events

RainbowHATDemoActivity.kt

val MESSAGES = mapOf( KeyEvent.KEYCODE_A to "AHOY", KeyEvent.KEYCODE_B to "YARR", KeyEvent.KEYCODE_C to "GROG")

Processing button events

RainbowHATDemoActivity.kt

override fun onKeyUp(keyCode: Int, event: KeyEvent?) = when (keyCode) { in MESSAGES.keys -> { display.displayMessage(MESSAGES[keyCode]!!) true } else -> super.onKeyUp(keyCode, event)}

Creating the Buzzer class

Buzzer.kt

class Buzzer(private val speaker: Speaker = Speaker(SPEAKER_PWM_PIN) { …}

Creating the Buzzer class

Buzzer.kt

fun play(frequency: Double) { speaker.play(frequency)}

fun stop() { speaker.stop()}

Creating the Buzzer class

Buzzer.kt

init { stopRunnable = Runnable { stop() }}

fun play(frequency: Double, duration: Double) { speaker.play(frequency) stopHandler.postDelayed(stopRunnable, duration.toLong())}

Creating the Leds class

Leds.kt

class Leds(peripheralManagerService: PeripheralManagerService = PeripheralManagerService()) { …}

Creating the Leds class

Leds.kt

private val leds: List<Gpio>

init { leds = listOf( openGpio(peripheralManagerService, LED_RED_GPIO_PIN), openGpio(peripheralManagerService, LED_GREEN_GPIO_PIN), openGpio(peripheralManagerService, LED_BLUE_GPIO_PIN))}

Creating the Leds class

Leds.kt

private fun openGpio(service: PeripheralManagerService, pin: String): Gpio { val led = service.openGpio(pin) led.setDirection(Gpio.DIRECTION_OUT_INITIALLY_LOW) return led}

Creating the Leds class

Leds.kt

fun setLed(led: Int, on: Boolean) = with(leds[led]) { value = on}

fun toggleLed(led: Int) = with(leds[led]) { value = !value}

Integrating Buzzer and Leds

RainbowHATDemoActivity.kt

private fun playMelodyWithLeds() { playbackRunnable = Runnable { buzzer.play(NOTES[noteIndex].toDouble(), DURATIONS[timeIndex] * 0.8) leds.setLed(Leds.LEDS[ledIndex], on = true) leds.setLed(Leds.LEDS[prevIndex(ledIndex, Leds.LEDS.size)], on = false) if (noteIndex == NOTES.size - 1) { // close } else { playHandler.postDelayed(playbackRunnable, DURATIONS[timeIndex].toLong()) // increment indices } } playHandler.post(playbackRunnable)}

Integrating Buzzer and Leds

RainbowHATDemoActivity.kt

override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState)

...

playMelodyWithLeds()}

Closing resources

RainbowHATDemoActivity.kt

override fun onDestroy() { ... arrayOf(leds, buttons, buzzer, display).forEach(Closeable::close) super.onDestroy()}

● Android Things - https://developer.android.com/things/index.html

● Samples - https://developer.android.com/things/sdk/samples.html

● Driver Library - https://github.com/androidthings/contrib-drivers

Where do we go from here?

What will you build?

Thanks!

Egor AndreeviciSoftware Developer at 1&1

@EgorAnd+EgorAndreevichhttps://blog.egorand.me