Lock in $30 Savings on PRO—Offer Ends Soon! ⏳
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Connect your Phone and Home with Firebase and...
Search
James Coggan
December 28, 2017
Programming
0
110
Connect your Phone and Home with Firebase and Android Things
Presentation done at Droidcon Tel Aviv 2017
James Coggan
December 28, 2017
Tweet
Share
More Decks by James Coggan
See All by James Coggan
Build accessible conversational Experiences with Actions on Google
jamescoggan
0
60
How to test your Android Things code in a clean way
jamescoggan
0
88
GDG DevFest Kansas city - Connect your Phone and Home with Firebase and Android Things
jamescoggan
0
65
GDG DevFest Minneapolis - How to write your own custom driver for Android Things
jamescoggan
0
62
Londroind - Thingyfy your home with Android Things and Firebase
jamescoggan
0
100
Other Decks in Programming
See All in Programming
リリース時」テストから「デイリー実行」へ!開発マネージャが取り組んだ、レガシー自動テストのモダン化戦略
goataka
0
140
DevFest Android in Korea 2025 - 개발자 커뮤니티를 통해 얻는 가치
wisemuji
0
170
実は歴史的なアップデートだと思う AWS Interconnect - multicloud
maroon1st
0
250
JETLS.jl ─ A New Language Server for Julia
abap34
2
440
Deno Tunnel を使ってみた話
kamekyame
0
220
Context is King? 〜Verifiability時代とコンテキスト設計 / Beyond "Context is King"
rkaga
10
1.4k
堅牢なフロントエンドテスト基盤を構築するために行った取り組み
shogo4131
8
2.5k
Navigating Dependency Injection with Metro
l2hyunwoo
1
170
これならできる!個人開発のすゝめ
tinykitten
PRO
0
120
生成AI時代を勝ち抜くエンジニア組織マネジメント
coconala_engineer
0
390
AI時代を生き抜く 新卒エンジニアの生きる道
coconala_engineer
1
400
Jetpack XR SDKから紐解くAndroid XR開発と技術選定のヒント / about-androidxr-and-jetpack-xr-sdk
drumath2237
1
180
Featured
See All Featured
Future Trends and Review - Lecture 12 - Web Technologies (1019888BNR)
signer
PRO
0
3.1k
The AI Search Optimization Roadmap by Aleyda Solis
aleyda
1
5k
Navigating the moral maze — ethical principles for Al-driven product design
skipperchong
1
200
Navigating Weather and Climate Data
rabernat
0
49
Balancing Empowerment & Direction
lara
5
810
Game over? The fight for quality and originality in the time of robots
wayneb77
1
65
Hiding What from Whom? A Critical Review of the History of Programming languages for Music
tomoyanonymous
0
290
The Director’s Chair: Orchestrating AI for Truly Effective Learning
tmiket
0
62
WENDY [Excerpt]
tessaabrams
8
35k
Marketing to machines
jonoalderson
1
4.3k
Stop Working from a Prison Cell
hatefulcrawdad
273
21k
Paper Plane (Part 1)
katiecoart
PRO
0
1.9k
Transcript
James Coggan, MyDrive Solutions Connect your Phone and Home with
Firebase and Android Things We are hiring! https://www.mydrivesolutions.com/jobs Q&A sli.do #thingstelaviv
A little bit about me.. James Coggan Android tech lead
https://jamescoggan.com @mad_team
None
What is Android Things?
Android + Internet of Things Android Things
The hardware NXP Pico i.MX7D Raspberry Pi 3 NXP Argon
i.MX6UL NXP Pico i.MX6UL
Development kits NXP Pico i.MX7D Raspberry pi kit
Android
Android Things
What Android Things does support
What Android Things doesn’t support
Good to know • Main application started on boot •
Permissions are free and set on reboot • adb connect 192.168.1.111:5555
Why Android Things? • Kotlin :) • Maintained by Google
• OTA updates • Android community • Hardware agnostic • Relatively cheap hardware • Reusable code
Getting started https://partner.android.com/things/console/u/0/#/tools
Getting started
Getting started
Supported I/Os • General Purpose Input/Output (GPIO): Binary • Pulse
Width Modulation (PWM): Servo motors, DC motors • Inter-Integrated Circuit(I2C) • Serial Peripheral Interface (SPI) • Universal Asynchronous Receiver Transmitter (UART)
None
None
Raspberry Pi NXP MX7D
val pioService = PeripheralManagerService() try { val pinName = "BCM6"
ledGpio = pioService.openGpio(pinName) ledGpio?.setDirection(Gpio.DIRECTION_OUT_INITIALLY_LOW) } catch (e: IOException) { e.printStackTrace() }
val pioService = PeripheralManagerService() try { val pinName = "BCM21"
button = ButtonInputDriver( pinName, Button.LogicState.PRESSED_WHEN_LOW, KeyEvent.KEYCODE_SPACE) button?.register() } catch (e: IOException) { e.printStackTrace() }
override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean { if (keyCode
== KeyEvent.KEYCODE_SPACE) { setLedValue(true) return true } return super.onKeyDown(keyCode, event) }
override fun onKeyUp(keyCode: Int, event: KeyEvent): Boolean { if (keyCode
== KeyEvent.KEYCODE_SPACE) { setLedValue(false) return true } return super.onKeyUp(keyCode, event) }
val gpioForLED: String // Pin 31 get() { return when
(Build.DEVICE) { DEVICE_RPI3 -> "BCM6" DEVICE_IMX6UL_PICO -> "GPIO4_IO22" DEVICE_IMX7D_PICO -> "GPIO2_IO02" else -> throw IllegalStateException("Unknown Build.DEVICE " + Build.DEVICE) } }
val gpioForButton: String // Pin 40 get() { return when
(Build.DEVICE) { DEVICE_RPI3 -> "BCM21" DEVICE_IMX6UL_PICO -> "GPIO2_IO03" DEVICE_IMX7D_PICO -> "GPIO6_IO14" else -> throw IllegalStateException("Unknown Build.DEVICE " + Build.DEVICE) } }
val pinName = gpioForLED ledGpio = pioService.openGpio(pinName) ... val pinName
= gpioForButton button = ButtonInputDriver( pinName, Button.LogicState.PRESSED_WHEN_LOW, KeyEvent.KEYCODE_SPACE) ...
None
None
None
Setting up Firebase https://console.firebase.google.com • Create a new Firebase project
• Add a new Android app to the project with your package name: ie: com.jamescoggan.thingspresentation • Enable anonymous login on (temporarily)
• Setup a database // Database { "light" : true
} • Add the rules // Rules - don’t expose your data for the world { "rules": { ".write": "auth != null", ".read": "auth != null", } }
// Base build.gradle for all modules buildscript { ... dependencies
{ classpath "com.google.gms:google-services:$googleServicesClassVersion" } }
// mobile & things build.gradle dependencies { ... implementation "com.google.firebase:firebase-core:$playServicesVersion"
implementation "com.google.firebase:firebase-database:$playServicesVersion" implementation "com.google.firebase:firebase-auth:$playServicesVersion" implementation "android.arch.lifecycle:extensions:$androidArchComponentsVersion" } apply plugin: "com.google.gms.google-services"
// MainActivity.kt (Both modules - mobile and things) override fun
onCreate(savedInstanceState: Bundle?) { FirebaseApp.initializeApp(this) // Move me to the Application class val firebaseAuth = FirebaseAuth.getInstance() val databaseReference = FirebaseDatabase.getInstance().reference }
// MainActivity.kt override fun onCreate(savedInstanceState: Bundle?) { ... val databaseReference
= FirebaseDatabase.getInstance().reference firebaseAuth.signInAnonymously() .addOnCompleteListener { task -> if (task.isSuccessful) { observeLightsData() } else { Timber.e(task.exception, "FirebaseAuth:failed") } } }
// Light.kt class FirebaseTables { companion object { val LIGHTS_BASE
= "light" } } data class Light(val light : Boolean)
class LightLiveData(val firebase: DatabaseReference) : LiveData<Light>() { private val valueEventListener
= object : ValueEventListener { override fun onDataChange(snapshot: DataSnapshot) { val newValue = snapshot.getValue(Boolean::class.java) ?: false value = Light(newValue) } override fun onCancelled(error: DatabaseError) { } } override fun onActive() { firebase.child(LIGHTS_BASE).addValueEventListener(valueEventListener) } override fun onInactive() { firebase.child(LIGHTS_BASE).removeEventListener(valueEventListener) } }
// Things Activity private val lightsDataObserver = Observer<Light> { lightState
-> Timber.d("LightState changed: ${lightState?.isOn}") led.setValue(lightState?.isOn ?: false) } override fun onCreate(savedInstanceState: Bundle?) { ... firebaseAuth.signInAnonymously() .addOnCompleteListener { task -> if (task.isSuccessful) { lightsLiveData.observe(this, lightsDataObserver) } else { Timber.e(task.exception, "FirebaseAuth:failed")
// Mobile Activity override fun onCreate(savedInstanceState: Bundle?) { ... toggleButton.setOnCheckedChangeListener({
_, state: Boolean -> databaseReference.child("light").setValue(state) }) }
None
None
None
Nearby • Pub/Sub • Send messages, files or stream data
• No need for server • Peer to peer • Wifi or BLE
Nearby
googleApiClient = GoogleApiClient.Builder(this) .addApi(Nearby.CONNECTIONS_API) .addConnectionCallbacks(object : GoogleApiClient.ConnectionCallbacks { override fun
onConnected(connectionHint: Bundle?) { startService() } override fun onConnectionSuspended(cause: Int) { failed() } }) .build()
private fun startService() { // Android Things Nearby.Connections.startAdvertising( googleApiClient, "appName",
"serviceId", connectionLifecycleCallback, AdvertisingOptions(Strategy.P2P_STAR)) .setResultCallback { result -> when { result.status.isSuccess -> Timber.d("startAdvertising:onResult: SUCCESS") else -> Timber.w("STATE_READY")
private fun startService() { // Phone Nearby.Connections.startDiscovery( googleApiClient, "serviceId", endpointDiscoveryCallback,
DiscoveryOptions(Strategy.P2P_STAR)) .setResultCallback { result -> when { result.status.isSuccess -> Timber.d("startDiscovery:SUCCESS") else -> { Timber.w("startDiscovery:FAILURE ${result.statusMessage}")
private val connectionLifecycleCallback = object : ConnectionLifecycleCallback() { override fun
onConnectionResult(endpointId: String?, result: ConnectionResolution?) { Timber.d("connectionResult from " + endpointId, result) sendDataPayload() } }
// Phone private fun sendDataPayload(email : String, password: String) {
val credentials = Credentials(email, password) val adapter = moshi.adapter(Credentials::class.java) val json = adapter.toJson(credentials) Nearby.Connections.sendPayload( googleApiClient, currentEndpointId, Payload.fromBytes(json.toByteArray()) ) }
private val payloadCallback = object : PayloadCallback() { override fun
onPayloadReceived(endpointId: String?, payload: Payload?) { val adapter = moshi.adapter(Credentials::class.java) val credentials = adapter.fromJson(jsonString) credentials?.let { saveCredentials(credentials) } } }
firebaseAuth.signInWithEmailAndPassword(email,password) .addOnFailureListener { exception -> exception.printStackTrace() } .addOnSuccessListener { loadData()
} }
Success!!
https://github.com/jamescoggan/thingspresentationn • Common code in shared module • Nearby •
RainbowHat: ◦ Sensors ◦ Led ◦ Button • Philips Hue (in progress)
Q&A sli.do #thingstelaviv
Feedback: https://goo.gl/gwPxpY Thank you! https://jamescoggan.com @mad_team