69. Converting a color to a ByteArray
private fun colorToByteArray(color: Int): ByteArray {
val red = Color.red(color)
val green = Color.green(color)
val blue = Color.blue(color)
return byteArrayOf(0x56, red, green, blue, 0x00, 0xf0, 0xaa)
}
70. Send the color data to the lightbulb
private fun writeCharacteristic(gatt: BluetoothGatt, color: Int) {
val service = gatt.getService(SERVICE_UUID) // 0xFFE5
val characteristic = service.getCharacteristic(CHARACTERISTIC_UUID) // 0xFFE9
characteristic.value = colorToByteArray(color)
gatt.writeCharacteristic(characteristic)
}
78. Android Things GPIO API
viewModel.firestoreLiveData.observe({ lifecycle }) { device ->
when (device) {
is Lightbulb -> lightbulb.onStateChanged(device)
is Fan -> relay.value = device.isOn
}
}
90. Takeaway
• Actions on Google for chatbots & smart home apps. For
companies & individuals
• Android Things simplifies building secured & advanced IoT
devices
• Create your own devices, or hack existing ones for
interoperability
• Easier than it seems
91. Additional information
• Source code for all the projects:
https://github.com/Nilhcem/home-automation-talk-resources
• Home automation with the Google Assistant:
http://nilhcem.com/android-things/google-assistant-smart-home
• Actions on Google Smart Home documentation:
https://developers.google.com/actions/smarthome/
• Reverse engineering BLE devices:
http://nilhcem.com/iot/reverse-engineering-simple-bluetooth-devices
• Reverse engineering IR devices:
http://nilhcem.com/iot/reverse-engineering-ir-rgb-bulb