Android Open Accessory APIs


Published on

Slides from SCREENS 2011 in Toronto, Canada. Covers the Android Open Accessory support released for 3.1+ or 2.3.4+ devices, and the Android Development Kit (ADK)

Published in: Technology, Education
  • Be the first to comment

No Downloads
Total Views
On Slideshare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide
  • Example: PC desktopwith pheriphals
  • “accessory mode”
  • Arduino is two pieces – one is hardware and the other is software.This particular board is a variation of the Arduino Mega. A thing that makes this board special is that it has two USB ports on it. Going back to the whole USB Host thing, this board has the ability to be a USB Host which is the 1st most important thing to becoming an Android Development Kit.The 2nd important piece is that the brains of the Arduino needs to run custom firmware that can communicate with an Android app running on the phone or the tablet.
  • Use an Android app that Google created for their developer conference. Turn an LED on/off on the Arduino using the Android app.
  • Google offers a sample project via their developer portal BUT *whawha*, it just doesn’t work with a plain old Arduino Mega. Need to strip out all the unnecessary code.
  • Unmodified DemoKit Android app from Google IO. Working with hardware, especially when it’s new to you, is like wearing two or three hats. You wear one hat when you are prototyping with the electronics and wiring stuff up. Then you switch to another hat to write the Arduino code. And, in this scenario, you switch over to another hat that’s deep in Eclipse which is your Android SDK environment.
  • Version 10 is actually Android 2.3.3 but you must compile with the level 10 Google APIs so it has 2.3.4 capabilities. The phone version of the Open Accessory support is actually a backport from the tablet version. Version 12 is 3.1
  • Also in your manifest, add an intent filter. That will give you the popup that appeared when we plugged in the phone to the Arduino.
  • And lastly, for your manifest, let your app know what USB devices it can support.
  • For this DemoKit app, the list for the meta-data tag is an xml file that declares one usb-accessory node.If the app is not already installed on the device, it will try to find it on the Android Market.
  • The data written could be a simple identifying string or number. Or, if you have a lot of different parts, you could use a byte array.
  • InDemoKit, it was a byte array with 3 slots. There were quite a few other components so Google set it up with constants identifying the type of interaction. 3 was the number assigned to Relays. And since we were clicking on the first Relay button, it already had a value of 0.When you toggle Relay1 button on, you send over the values 3, 0, and 1. When you toggle Relay1 button off, you send over the values 3, 0, 0.We didn’t use Relay2 but the values would have been 3, 1, 1 or 3, 1, 0.
  • So then on the Arduino side, you parse through the byte array. Here it’s checking if the value at index 0 was 3, meaning that it was a relay as established by the Android app constants.Then the nested if checked if it was the first relay button.Finally, digitalWrite is an Arduino command to turn a pin on or off – either high or low.
  • Create rich Uis that are dynamic. Think about ways that you might want to push outside of the boundaries of that screen. Thing about multi-screen experiences, or experiences which have sort of an ambient visualization quality to it.
  • Photos (time-lapse?) being triggered by real world events.e.g. doggy door with a tilt sensor
  • traffic data, finanical data, twitter data, just so much data available to reinvent.
  • motion or orientation.
  • location-based services. Mobile form factor!
  • mass storage is a nice companion to most of these inputs but this opens up the ability for data logging over time.
  • Ben Heck runs a tech show so his proof of concept was a baby rocker. He drove around, collected real-world information about how the car rocked and then made a proof of concept that played back the recorded data.
  • Here’s a project that doesn’t yet use the Open Accessory APIs but NASA has these spheres that are deployed into space and the Nexus S is the brains on it because it was so easy to prototype.
  • Android is being embedded into many unexpected devices. Android itself is open source so you could roll your own specialized hardware and software products. The form factors of devices are blurring too so the use of the ADK and the Open Accessory APIs are great for prototyping a future scenerio where customized devices will be very cheap to manufacturer, or an off-the-shelf phone will be only $50 or $100.
  • Android Open Accessory APIs

    1. ANDROIDOPEN ACCESSORYAPIsPearl ChenGoogle+: @androidsNsheepSlides:
    2. WHAT IS AN OPEN ACCESSORY?A device that can become aUSB Host while connectedto an Android phone ortablet.
    3. WHAT DOES THE USB HOST DO?It keeps track of plugged in devicesand controls data traffic. It alsosupplies devices with power.Data transfer is 2-way but the USBprotocol is asymmetrical; there canbe multiple devices but only onehost. mouse game controller keyboard webcam
    4. WHERE DOES ANDROID FIT?Not all Android phones can host.(Most tablets do though.)To work around this, Google flips iton its head and instead: Androidbecomes the device/accessory. phone tablet mouse game controller keyboard webcam
    5. ANDROID DEVELOPMENT KIT (ADK)The ADK is a specialized USB Hostrunning ADK firmware that cancommunicate with Android apps thatimplement Open Accessory APIs.The Arduino Mega ADK is the mostwell-known version of an ADK.
    6. ANDROID DEVELOPMENT KIT (ADK)But other ADKs exist too!Board schematics and sourcecode is open source. (CreativeCommons and Apache 2.0 licenses.)
    7. DEMO #1
    8. FIRMWARE CODESample ADK “firmware” (translation:an Arduino sketch) is downloadableat if you don’t have theAccessory Demo Shieldthey gave out at GoogleIO 2011, it won’t work. :(
    9. SIMPLIFIED-VERSION OF FIRMWAREStripped down turns on/off built-in LED on pin 13 of theArduino when told to by the app running on theAndroid device.)
    10. CORE OF FIRMWARE#include <Max3421e.h>#include <Usb.h>#include <AndroidAccessory.h>AndroidAccessory acc("Google, Inc.", "DemoKit", "DemoKit Arduino Board", "1.0", "", "0000000012345678");...Include any required classes and create a new instance of anAndroidAccessory , passing in parameters for:manufacturer, model, description, version, uri, and serial #
    11. CORE OF FIRMWARE...void setup() { ... acc.powerOn();}void loop() { ... if (acc.isConnected()) { //communicate with your Android application here } else { //optional but recommended: //set the accessory to its default state } ...}
    12. ANDROID APP CODESample DemoKit app isdownloadable can more or less hijack the UIscreens and control widgets whilelearning how to interface with theArduino.
    13. ANDROID MANIFESTCheck that the device has USB capabilities so itcan’t be installed or show up in the AndroidMarket:Phones with 2.3.4+<uses-library android:name= "" />Tablets with 3.1+<uses-feature android:name= "android.hardware.usb.accessory" />
    14. ANDROID MANIFESTSet a target device version:Phones with 2.3.4+<uses-sdk android:minSdkVersion="10" />Tablets with 3.1+:<uses-sdk android:minSdkVersion="12" />
    15. ANDROID MANIFESTAdd an intent filter so the popup dialog box willshow up when plugged into the Arduino:<intent-filter> <action android:name= "android.hardware.usb.action. USB_ACCESSORY_ATTACHED" /></intent-filter>
    16. ANDROID MANIFESTAnd let the app know what USB devices itshould be allowed to talk to:<meta-data android:name= "android.hardware.usb.action. USB_ACCESSORY_ATTACHED“ android:resource= "@xml/accessory_filter" />
    17. ANDROID RESOURCE XMLres/xml/accessory_filter.xml contains:<resources> <usb-accessory manufacturer="Google, Inc." model="DemoKit“ version="1.0" /></resources>Remember from the Arduino firmware?AndroidAccessory acc("Google, Inc.", "DemoKit", "DemoKit Arduino Board", "1.0", "", "0000000012345678");
    18. DATA SENDING & RECEIVINGOpen up a single FileInputStream and a singleFileOutputStream to send and receive arbitrary data. Theencoding format is defined by you.public class UsbAccessoryActivity extends Activity { private FileInputStream mInput; private FileOutputStream mOutput; private void openAccessory() { UsbManager manager = UsbManager.getInstance(this); UsbAccessory accessory = UsbManager.getAccessory(getIntent()); ParcelFileDescriptor fd = manager.openAccessory(accessory); if (fd != null) { mInput = new FileInputStream(fd); mOutput = new FileOutputStream(fd); } else { // Oh noes, the accessory didn’t open! } }}
    19. DATA SENDING & RECEIVINGDemoKit example chose to send and receive via byte arrays.In DemoKitActivity.RELAY_COMMAND, //constant: set to be 3 mCommandTarget, //constant: Relay1 button would be 0, // Relay2 btn would be 1 isChecked ? 1 : 0); //variable: on or off? 1 or 0?In void sendCommand(byte command, byte target, int value) { byte[] buffer = new byte[3]; … buffer[0] = command; buffer[1] = target; buffer[2] = (byte) value; …}
    20. DATA SENDING & RECEIVINGMeanwhile, on the Arduino…if (acc.isConnected()) { int len =, sizeof(msg), 1); If (data[0] == 0x3) { //a relay? if (data[1] == 0x0) { // Relay1 button? digitalWrite(LED1, data[2] ? HIGH : LOW);} }}
    21. DEMO #2
    22. PROJECT IDEAS What can you make with the Open Accessory APIs and an Arduino? 1+1=3Things already available Superon your phone Electronics awesome stuff! components you can add
    23. AVAILABLE NEXUS S I/O touch screen
    24. MUSIC BETA: “NOW PLAYING” Chris Juergen
    25. AVAILABLE NEXUS S I/O touch screen camera
    26. GARDEN MONITOR Sam Steele
    27. AVAILABLE NEXUS S I/O touch screen camera wifi/3G
    28. AVAILABLE NEXUS S I/Oaccelerometer touch screen camera wifi/3G
    29. AVAILABLE NEXUS S I/Oaccelerometer touch screen GPS camera wifi/3G
    30. AVAILABLE NEXUS S I/Oaccelerometer touch screen GPS cameramass storage wifi/3G
    31. BABY ROCKER POC Ben Heck
    32. AVAILABLE NEXUS S I/Oaccelerometer touch screen GPS cameramass storage wifi/3Gproximity/light sensors
    33. AVAILABLE NEXUS S I/Oaccelerometer touch screen GPS cameramass storage wifi/3G temperatureproximity/light sensor sensors
    35. AVAILABLE NEXUS S I/Oaccelerometer touch screen GPS cameramass storage wifi/3G temperatureproximity/light sensor sensors microphone
    36. AVAILABLE NEXUS S I/Oaccelerometer touch screen GPS cameramass storage wifi/3G temperatureproximity/light sensor sensors microphone NFC
    37. BLURRING LINESAsus Transformer and Padfone, Nook, Xperia PLAY