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)
2. WHAT IS AN OPEN ACCESSORY?
A device that can become a
USB Host while connected
to an Android phone or
tablet.
3. WHAT DOES THE USB HOST DO?
It keeps track of plugged in devices
and controls data traffic. It also
supplies devices with power.
Data transfer is 2-way but the USB
protocol is asymmetrical; there can
be multiple devices but only one
host.
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 it
on its head and instead: Android
becomes the device/accessory.
phone tablet
mouse game controller
keyboard webcam
5. ANDROID DEVELOPMENT KIT (ADK)
The ADK is a specialized USB Host
running ADK firmware that can
communicate with Android apps that
implement Open Accessory APIs.
The Arduino Mega ADK is the most
well-known version of an ADK.
6. ANDROID DEVELOPMENT KIT (ADK)
But other ADKs exist too!
Board schematics and source
code is open source. (Creative
Commons and Apache 2.0 licenses.)
8. FIRMWARE CODE
Sample ADK “firmware” (translation:
an Arduino sketch) is downloadable
at developer.android.com.
But if you don’t have the
Accessory Demo Shield
they gave out at Google
IO 2011, it won’t work. :(
9. SIMPLIFIED-VERSION OF FIRMWARE
Stripped down version:
iheartrobotics.com/2011/07/
arduino-mega-adk-setup-
notes.html
(Simply turns on/off built-in LED on pin 13 of the
Arduino when told to by the app running on the
Android device.)
10. CORE OF FIRMWARE
#include <Max3421e.h>
#include <Usb.h>
#include <AndroidAccessory.h>
AndroidAccessory acc("Google, Inc.",
"DemoKit",
"DemoKit Arduino Board",
"1.0",
"http://www.android.com",
"0000000012345678");
...
Include any required classes and create a new instance of an
AndroidAccessory , 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 CODE
Sample DemoKit app is
downloadable at
developer.android.com.
You can more or less hijack the UI
screens and control widgets while
learning how to interface with the
Arduino.
13. ANDROID MANIFEST
Check that the device has USB capabilities so it
can’t be installed or show up in the Android
Market:
Phones with 2.3.4+
<uses-library android:name=
"com.android.future.usb.accessory" />
Tablets with 3.1+
<uses-feature android:name=
"android.hardware.usb.accessory" />
14. ANDROID MANIFEST
Set 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 MANIFEST
Add an intent filter so the popup dialog box will
show up when plugged into the Arduino:
<intent-filter>
<action android:name=
"android.hardware.usb.action.
USB_ACCESSORY_ATTACHED" />
</intent-filter>
16. ANDROID MANIFEST
And let the app know what USB devices it
should be allowed to talk to:
<meta-data android:name=
"android.hardware.usb.action.
USB_ACCESSORY_ATTACHED“
android:resource=
"@xml/accessory_filter" />
17. ANDROID RESOURCE XML
res/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",
"http://www.android.com",
"0000000012345678");
18. DATA SENDING & RECEIVING
Open up a single FileInputStream and a single
FileOutputStream to send and receive arbitrary data. The
encoding 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 & RECEIVING
DemoKit example chose to send and receive via byte arrays.
In RelayController.java:
mActivity.sendCommand(
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 DemoKitActivity.java:
public 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 & RECEIVING
Meanwhile, on the Arduino…
if (acc.isConnected()) {
int len = acc.read(msg, sizeof(msg), 1);
If (data[0] == 0x3) { //a relay?
if (data[1] == 0x0) { // Relay1 button?
digitalWrite(LED1, data[2] ? HIGH : LOW);}
}
}
22. PROJECT IDEAS
What can you make with
the Open Accessory APIs
and an Arduino?
1+1=3
Things already
available Super
on your phone Electronics awesome stuff!
components
you can add
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.