Smartphones, tablets, TVs, cars and smartwatches: Android is everywhere enabling users and developers with rich set of applications, libraries and services. Android Things brings such a power to virtually any object, any “thing”: using a low-cost (yet powerful) board, developer can add intelligence and connectivity to home, industries, vehicles and even medical appliances. This presentation introduces practical concepts around the Android Things platform and how to have fun with it.
4. FEEL AT HOME…
Same architecture
Same IDE (Android Studio)
Same programming languages
Same framework
Same app (Activity) lifecycle
Same UI widgets (UI?)
Same application packaging
Same reliable security for apps and firmware
Same passionate community!
6. IN & OUT
Cast
Drive
Firebase Analytics
Firebase Cloud Messaging
Firebase Realtime Database
Firebase Remote Config
Firebase Storage
Fit
Instance ID
Location
Nearby
Places
Mobile Vision
AdMob
Android Pay
Firebase App Indexing
Firebase Authentication
Firebase Dynamic Links
Firebase Invites
Firebase Notifications
Maps
Play Games
Search
Sign-In
CalendarContract
ContactsContract
DocumentsContract
DownloadManager
MediaStore
Settings
Telephony
UserDictionary
VoicemailContract
Basic rule: “is there any UI for the user to interact why the app?”
7. BOARDS FOR PROTOTYPING
Intel Edison Intel Joule NXP Pico i.MX6UL Raspberry Pi3
PRICE $55 > $200 $70 $22
SDK
PRICE
$150 > $300 - $22
CPU Atom DC @500Mhz Atom QC @1.5GHz
NXP i.MX6Ultralite ARM
Cortex A7 @500MHz
Broadcom BCM2837
QC @1.2GHz Cortex A53
RAM 1GB 3-4GB 512MB 1GB
STORAGE 4GB 8-16GB 4GB microSD
DISPLAY NO HDMI NO HDMI
CAMERA NO CSI-2 NO CSI-2
AUDIO USB 2.0 USB 2.0 3.5mm Analog USB 2.0 & 3.5mm Analog
NET WiFi n, BT 4.0 WiFi ac, BT 4.2 Ethernet, WiFi n, BT 4.1 GB Ethernet, WiFi n, BT 4.1
USB USB 2.0 OTG
2x USB 2.0 HOST +
USB 3.0 OTG
USB 2.0 HOST +
USB 2.0 OTG
4x USB 2.0 HOST
GPIO
2x UART, 2x I2C,
SPI 2ch, 14 GPIO
4x UART, 5x I2C,
2x SPI, up to 48 GPIO
8x UART, 4x I2C,
4x SPI, > 20 GPIO
2x UART, 2x I2C,
2x SPI, up to 26 GPIO
8. WHICH BOARD?
• Intel Edison
• Damn small!
• No UI, no Ethernet
• ADB via USB
• Raspberry Pi3
• Damn cheap!
• Ethernet + WiFi
• HDMI + Camera + lot of
extension boards: AI+VR+IoT!
• Different configurations can be tested just swapping the SD
9. ANDROID THINGS vs ANDROID
Current supported boards lack some of “universally available”
features of any Android Device:
• Bluetooth is no supported in DP2
• Latest news: added in DP3, released April 6th!
• There is no RTC: PKI support may be broken unexpectedly
• RPi3 has OS on a removable memory: security risk
• Any “visual alarm” on battery level: must be monitored manually
• There is no GPS onboard: location must be fed with NMEA
stream
• No Power-button (adb shell reboot -p)
12. “Hello Things”
public class MainActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
PeripheralManagerService service = new PeripheralManagerService();
mLedGpio = service.openGpio(“IO13”);
mLedGpio.setDirection(Gpio.DIRECTION_OUT_INITIALLY_LOW);
new Thread(new Runnable() {
public void run() {
for (int i=0;i<10;i++) {
try {
mLedGpio.setValue(i%2 == 0);
Thread.sleep(250);
} catch (Exception e) {
// uh! uh!
}
}
}
}).start();
}
}
longer
pin
330 ohm
13. GPIO:
• Two logical values and two electric states:
• true or false, high or low
• Change association using gpio.setActiveType()
• Gpio.ACTIVE_HIGH
• Gpio.ACTIVE_LOW
GPIO1
2 PWM
3 ANALOG
PeripheralManagerService manager = new PeripheralManagerService();
List<String> portList = manager.getGpioList();
mGpio = manager.openGpio(portList.get(0));
mGpio.setValue(true | false); // write
boolean state = mGpio.getValue(); // read
PERIPHERAL I/O
14. GPIO1
2 PWM
3 ANALOG
GpioCallback mGpioCallback = new GpioCallback() {
@Override
public boolean onGpioEdge(Gpio gpio) {
// Read the active low pin state
mDevice.getValue()
// Continue listening for more interrupts
return true;
}
@Override
public void onGpioError(Gpio gpio, int error) {
//oh nooo
}
};
mGpio.setEdgeTriggerType(EDGE_BOTH);// |EDGE_NONE | EDGE_RISING | EDGE_FALLING
mGpio.registerGpioCallback(mGpioCallback);
PERIPHERAL I/O
16. PERIPHERAL I/O
PeripheralManagerService manager = new PeripheralManagerService();
List<String> portList = manager.getPwmList(); //List of all PWM ports
Pwm mPwm = manager.openPwm(portList.get(0));
open connection
mPwm.setPwmFrequencyHz(50);
mPwm.setPwmDutyCycle(7.5);
mPwm.setEnabled(true); //start pulsing
setup connection
Signal
5V
GND
50HZ , 7.5% -> Neutral position(90°)
50HZ , 3.75% -> Min position (0°)
50HZ , 11.25% -> Max position (180°)
Values for this servo
GPIO1
2 PWM
3 ANALOG
17. Analog Signal:
• Hardware available only on Intel Edison Arduino
Breakout Board
• Not supported yet on Android Things
• For the moment use external analog-to-digital
converter (ADC)
PERIPHERAL I/O
mcp3008
adc0832
GPIO1
2 PWM
3 ANALOG
18. ADC0832
• 2 channel Analog to digital converter
Start communication
Analog input
Power
Clock
Output
Channel
selection
22. • Synchronous, fixed clock speed, half-duplex, master-slave
(SW), low boundrate
• Peripheral specs: addresses for all connected slave,
addresses for all information, MSB only
• Connection:
• Shared clock signal (SCL)
• Shared data line (SDA)
• Common ground reference (GND)
The world of addresses
I2C
PeripheralManagerService manager = new PeripheralManagerService();
I2cDevice mI2C = manager.openI2cDevice(I2C_DEVICE_NAME, I2C_ADDRESS);
SPI
1
2
I2C
3
UART
23. byte value = mI2C.readRegByte(address);
mI2C.writeRegByte(address, value);
byte[] data = new byte[3];
mI2C.readRegBuffer(startAddress, data, data.length);
mI2C.writeRegBuffer (startAddress, data, data.length)
read / write register
read / write buffer
read / write word
short value = mI2C.readRegWord(address); //2 byte Little Endian
mI2C.writeRegWord(address, value);
System Management Bus (SMBus)
S slave address register address S slave address data[N] S
I2C SPI
1
2
I2C
3
UART
24. UART
• The simplest serial, often used for expansion boards or even
home appliances, often for logging
• Description: point to point, asynchronous (no clock), full duplex
• Peripheral specs: boundrate, parity bit, data size, stop bit
• Interface: 3 basic wires TX, RX, GND + 2 optional wires:
request to send (RTS) and clear to send (CTS)
SPI
1
2
I2C
3
UART
25. PeripheralManagerService manager = new PeripheralManagerService();
UartDevice mUart = manager.openUartDevice(UART_DEVICE_NAME);
mUart.setBaudrate(115200);
// 8N1
mUart.setDataSize(8);
mUart.setParity(UartDevice.PARITY_NONE);
mUart.setStopBits(1);
//enable/disable HW flow control
mUart.setHardwareFlowControl(UartDevice.HW_FLOW_CONTROL_AUTO_RTSCTS);
mUart.setHardwareFlowControl(UartDevice.HW_FLOW_CONTROL_NONE);
open connection
set configuration
UART SPI
1
2
I2C
3
UART
26. mUart.write(buffer, buffer.length);
mUart.read(buffer, buffer.length))
send/read data
mUart.registerUartDeviceCallback(new UartDeviceCallback() {
@Override
public boolean onUartDeviceDataAvailable(UartDevice uart) {
byte[] buffer = new byte[20];
int count;
try {
while ((count = uart.read(buffer, buffer.length)) > 0) {
Log.d("TAG", "Read " + count + " bytes from peripheral");
}
} catch (IOException e) { Log.d("TAG", "Unable to access UART device");}
return true; // Continue listening for more interrupts
}
@Override
public void onUartDeviceError(UartDevice uart, int error) {
Log.d("TAG", "UART device error: " + error);
}
});
listening for data
UART SPI
1
2
I2C
3
UART
34. NEVER FORGET TO READ THE RELEASE NOTES!
List<String> portList = mManager.getGpioList();
for (String gpioName: portList) {
Gpio gpio;
if (gpioName.equals("BCM4") ||
gpioName.equals("BCM5") ||
gpioName.equals("BCM6"))
{
gpio = mManager.openGpio(gpioName);
gpio.setDirection(Gpio.DIRECTION_OUT_INITIALLY_LOW);
gpio.setActiveType(Gpio.ACTIVE_HIGH);
gpio.setValue(false);
gpio.close();
}
}
35. BRICKPI3 LOW LEVEL PROTOCOL
REQUEST (NO RESPONSE)
TARGET DEVICE MESSAGE PARAM
0 0x01 1 byte optional
TARGET DEVICE MESSAGE PADDING PADDING CHECK
RESPONSE
PAYLOAD
0 0x00 0x00 0x00 0x00 0xA5
N bytes according
to expected
response length
TARGET DEVICE MESSAGE PARAM
RESPONSE
BUFFER
0 0x01 1 byte optional
2+N bytes according
to expected
response length
REQUEST (WITH RESPONSE)
RESPONSE
36. BRICKPI3 LOW LEVEL PROTOCOL
0x01 // target device
0x14 + portNumber // message “set sensor type” on port
type // sensor type
Example: 0x01 0x14 0x05 // set sensor type NXT TOUCH on port 0
SET SENSOR TYPE
37. BRICKPI3 LOW LEVEL PROTOCOL
0x01 // target device
0x18 + portNumber // message “read sensor data” on port
0x00 0x00 // padding
0x00 … 0x00 // expected response payload length
Example: 0x01 0x18 0x00 0x00 0x00 0x00 0x00 // read sensor on port 0
READ SENSOR DATA REQUEST
0x00 0x00 0x00 // header (?)
0xA5 // fourth byte must be 0x0A5 if message is valid
0x** … 0x** // response payload
Example: 0x00 0x00 0x00 0xA5 0x05 0x00 0x01 // touch sensor is pressed
READ SENSOR DATA RESPONSE
Message is valid
It is a touch sensor
Data sample is valid
Button is pressed
38. BRICKPI3 LOW LEVEL PROTOCOL
public void setSensorType(int port, int type) {
byte[] request = {
0x01; // target device
(byte) (0x14 + port), // message “set sensor type”
(byte) (type) // sensor type
};
mSpi.write(request, request.length);
}
SET SENSOR TYPE
39. BRICKPI3 LIBRARY FOR ANDROID THINGS
BrickPi4 mBrick = BrickPi3.getInstance();
mBrick.open();
mBrick.setSensorType(BrickPi3.SENSOR_PORT.S4, BrickPi3.SENSOR_TYPE.NXT_TOUCH);
if (mBrick.isPressed(BrickPi3.SENSOR_PORT.S4)) {
mBrick.setMotorSpeed(BrickPi3.MOTOR_PORT.A, (byte) 80);
mBrick.setMotorSpeed(BrickPi3.MOTOR_PORT.D, (byte) 80);
}
SET SENSOR TYPE, READ SENSOR DATA AND START MOTORS!
mBrick.setSensorType(BrickPi3.SENSOR_PORT.S1,
BrickPi3.SENSOR_TYPE.EV3_ULTRASONIC_CM);
if (mBrick.getDistanceInCm(BrickPi3.SENSOR_PORT.S1) < 20) {
mBrick.setMotorSpeed(BrickPi3.MOTOR_PORT.A, (byte) -60);
mBrick.setMotorSpeed(BrickPi3.MOTOR_PORT.D, (byte) 60);
// sleep while rover rotates…
}
41. CONCLUSIONS
Android Things is for IoT what Android has been for mobile
devices: power, richness, simplicity, full access to the cloud!
Android Things brings unexpected potential to developers,
makers and enables the entire IoT industry with a powerful
platform ready-to-run.
Looking forward to seeing “powered by “ logo
in our next appliances!
42. RESOURCES
• Android Things
• https://developer.android.com/things/index.html
• BrickPi3 Protocol
• https://www.dexterindustries.com/BrickPi
• LEGO Mindstorms
• https://www.lego.com/mindstorms/
• Link to source code will be added in the public version of
this presentation
43. SPEAKERS
• Giovanni Di Gialluca
• g.digialluca@reply.it
• https://www.linkedin.com/in/giovanni-di-gialluca-9237a776
• Stefano Sanna
• s.sanna@reply.it
• @gerdavax
• https://www.linkedin.com/in/gerdavax