Peter_vdL

The Open Accessory API and USB On The Go

by Peter_vdL Motorola 07-21-2011 03:21 PM - edited 10-11-2011 06:04 PM

Ever since Google announced its Open Accessory APIs for Android at Google I/O, I've been keen to learn more about the implementation.   Google I/O had a supercharged marketing rollout for the Open Accessory API, and they gave all attendees at the announcement talk a cardboard tube containing an Arduino board kit.  The kit contains the hardware to experiment with the Open Accessory API.  The Open Accessory Library lets you (your equipment) talk to your Android phone over USB.   A recent post on the Android blog provides more details, and there's a good analysis of the Google kit by our friends over at FunnyRobotics.

[Note added10/11/2011 - here's another source of ADK info http://labs.arduino.cc/ADK/Index ].

 accessory.jpg

The Open Accessory API Development Kit from Google IO, 2011

 

It's early days, but the most-frequently executed project so far is "stepper-motorize a droidbug controlled by a phone or tablet over USB, and film it dancing".  Which, I have to say, is pretty neat.

 

Basics of the USB Protocol

More than 11 billion USB devices have been sold to date, which means there are roughly 1.5 USB devices for every man, woman, and child on the planet.  (And, at any time, about 1% of them are laying in tangled piles on my desk).  USB - the Universal Serial Bus - in all its forms and iterations, is the greatest success story of external peripheral connection in the history of computers.  Bite on that, Bluetooth!



USB was developed about 16 years ago by PC manufacturers working together. They realized that the old RS232 serial port, first developed by the Romans (OK, I'm not certain about that), needed to be replaced, and relocated to somewhere handier than the dusty back panel of a PC.  USB succeeded brilliantly, and has enjoyed 3 major iterations so far.  USB 3.0 today offers speeds up to an incredible 5 Gbit/sec, which is faster than FireWire (Apple's alternative solution).

 

Every piece of equipment with a USB port is either a "USB host" or a "USB device". 

  • USB hosts are required to supply power to other devices on the bus, and they control all the traffic over the bus.  USB hosts are things like your PC.  There is exactly one host on the bus, and it keeps track of everything else, through a process called "enumerating the bus" (inviting all devices to state their presence and capabilities).
  • USB devices don't do any processing; they just hand over their data when a USB host tells them to.  USB devices includes equipment like cameras, flash drives, mice, and foam missile launchers.   (There are several open source projects to control those USB foam missile launchers!)

The USB protocol requires a USB host at one end of the cable, and a USB device at the other end.  The USB host controls all the USB devices and tells them when to put their data on the bus.  Hosts can't talk to other hosts, and devices can't talk to other devices.  The interaction is always between a USB host and one or more USB devices.  


Is an Android Handset a USB Host or a USB Device?

The question arises whether Android handsets are USB hosts or USB devices? Handsets are like USB hosts because they have the processing power to be a bus master, and you'd like to connect them to your camera or USB keyboard.  But handsets are also like USB devices because you'd like to plug them into your PC to develop your apps.    A piece of USB-connected equipment has to be either a USB Host, or a USB device.  It cannot be both.  Up until recently, that was the end of the story.  We'll come back to that at the end of the article. 

 

The convention throughout the industry is that smartphones are deemed to be USB devices.  That makes it easy to connect them to your PC, but it also means they cannot talk to other USB devices (like the keyboard or flash memory).   And USB devices cannot initiate a connection, so you can't start an Android app and have it control some other USB device.


Open Accessory API

Recognizing that smartphones are overwhelmingly configured as USB devices, Google extended the USB protocol slightly, to put a bit more smartness into the device end, and a bit more enquiry into the host end.  Under Google's Open Accessory API, USB equipment that wants to interact with an Android phone should be implemented as a USB host - with something extra. The something extra is a protocol handshake after the USB cable is plugged in, and identities are exchanged.  The host will send an extra message, asking in effect "BTW, are you a device that supports Android's accessory mode?"

An Android smartphone that supports the Open Accessory API will expect that extra question, and will reply "Yes, I am! LOL!"  When the USB host gets that confirmation, it does some more handshaking, then tells the smartphone which compatible application to start.  That app will use the Accessory API to allow the phone user to interact with the USB host hardware on the other end of the USB cable. But if the USB host hardware doesn't get an answer, the USB host knows that the smartphone does not support the API and so the smartphone won't be controlling the USB host.  Perhaps the USB host can itself offer a less capable alternative interface to the user.

 

In summary, the Open Accessory protocol offers a data path over USB between the phone and the accessory device.  It has a Java API on the phone to detect and interact with connected accessories. The Open Accessory API lets you attach a USB host microcontroller and talk to it from Java on the phone.


Hardware Requirements of the Open Accessory API

Google's approach requires hardware to be upgraded, if it wants to connect to a smartphone over USB.  The equipment has to become a USB host, which requires some processing power.  Not only that, the equipment is obliged to put half an amp of current at 5 Volts on the bus.   This is why the accessory development kit that Google handed out at Google IO includes an Arduino board (for the logic) and a plug-in power supply (for the volts).

 

bike32.jpg
Control your electric bike from your Android phone?  USB hosts can do this!

 

The logic and power requirements focus the Open Accessory API on higher-end, special-purpose peripherals, like exercise equipment or electrically-assisted vehicles. It has a really great future there.  We still need a solution that lets us attach low-end keyboards, printers, and cameras to our smartphones and tablets.  Bluetooth is good for some of these, but not for others (most cameras support USB, with only a few supporting bluetooth).

 

One obvious USB solution would be to build products with two USB ports: one full size USB socket configured as a host, and one micro socket configured as a device.  There are practical difficulties with that approach - battery-powered devices don't have the capacity to provide power to the bus for long; smartphones don't have the space for a full size USB socket - it's duplicative and increases the bill of materials and manufacturing cost.

 

Nonetheless this approach is used (in theory, if not in practice) by some docking stations that support keyboards and flash memory.   More about this in the next section.  If you ever wondered why you have to plug some docks in to the wall power outlet, it's because they support USB device connection by acting as a USB host.  They need to meet the requirement of powering the bus. They have to plug into an electrical outlet to achieve that.  Since they have to plug in, they might as well recharge your phone - this makes a virtue of necessity.

 

USB On The Go Alternative

We still need a solution for low-end devices.  That's where USB On The Go comes in!  USB OTG is a specification that allows capable USB devices (like tablets and smartphones ) to temporarily switch roles, and become a USB host so they can talk to another device!  

 

USB On The Go took a few years to catch on, but recently tablets have boosted its popularity.  When you see Android products advertised as capable of connecting to cameras, keyboards, and mice, it is typically through a docking station or dongle that implements some part of USB On The Go.   Motorola offers such a "USB OTG connector cable" product for the XOOM, calling it a "Camera Kit Adapter", so that people unfamiliar with USB protocols won't get confused. 

 

The HD multimedia dock for ATRIX is an example of a powered dock that supports part of the OTG spec.  The dock provides the power to the bus, that allows the smartphone to act as a USB host.  So you can connect keyboards and USB storage (disks, flash drives) to the ATRIX dock and thus to the ATRIX.  The dock power supply has enough capacity to recharge the phone while also powering the USB bus.


What it all boils down to

That's how practical problems of smartphone connectivity were ingeniously solved for an older bus architecture that was not designed with hybrid (sometimes I'm a device/sometimes I'm a host) products in mind.  

  • The Open Accessory API lets your phone/tablet connect to a USB host built into high-end equipment, and control the equipment from your Android device. 
  • USB On The Go lets your tablet or dock act like a USB host and connect to low-end USB devices like printers and cameras. 

These late adaptions (Open Accessory and OTG) are classic examples of how good engineers can build on a solid foundation.  They also demonstrate how software and hardware changes need to be carefully considered, and kept as unintrusive as possible, to be successful.

 

Peter van der Linden

Android Technology Evangelist, and Fan of Roman Technology

 

PS. many thanks to colleagues Jim Theisen, Greg Tong, and Greg Wilson who provided many suggestions for improving this article.

Comments
by Parin Choganwala(anon) on 09-11-2011 11:56 AM

Does open accessory API works with Motorola Atrrix/Droid phones? I have Arduino ADK board?

 

Thank You,

Parin

 

by Peter_vdL Motorola on 09-11-2011 03:16 PM

I have heard (but not confirmed) that the Nexus One and Nexus S phones support the Open Accessory API. 

 

The ATRIX and Droid phones do not support the Open Accessory API.   On pre-Honeycomb devices, the Open Accessory API is supported by a compatibility library, which has to be compiled as part of the system image by the manufacturer.

 

It's easy to check for the presence/absence of a library, like com.android.future.usb.accessory, on a device.


1.  Create the "Hello World" app

2.  Add a line to the Manifest saying that it uses the library

    <application android:label="TestUSBlib" android:icon="@drawable/icon">
        <uses-library android:name="com.android.future.usb.accessory"/>

3.  Try to install on the device.

 

If the app installs OK, the library is on the device.  If it gives an error message about the library not being found, then it is not on the device.

 

Peter

Android Technology Evangelist

by Parin on 09-12-2011 06:09 AM

Hi Peter,

 

I tried adding com.android.future.usb.accessory library on ATRIX but .apk installation failed with

"Installation error: INSTALL_FAILED_MISSING_SHARED_LIBRARY".

 

so I am trying Amarino right now to enable communication with ATRIX and Arduino board. if you know any other better method

please let me know.

 

Thank you very much for your reply! Appreciate it!

 

Parin

by Arsalan(anon) on 09-13-2011 04:13 AM

Hi Peter,

 

Thank you for the article. A GREAT JOB Well Explained!

 

I am interested in this HD Multimedia Docking Station which provides power to the bus to enable a phone (Which doesn't support USB Host mode) to become USB Host.

 

My interest is to connect my usb device to this docking station and use Open Accessory API (On a Android 2.3.4 Smartphone) to read and write data to this device. I reckon I should be able to use Nexsus S 2.3.4 with this docking station?

 

Do you think this idea will work? Also are there any motorla devices which supports this docking station and has Open Accessory API?

 

Thanks in advance,

 

-Arsalan

by Peter_vdL Motorola on 09-13-2011 02:04 PM
Hi Arsalan, I don't know of any Motorola phones that support the Open Accessory API - that is a very new interface, only announced at Google IO. In general phones that support it will only do so thru a dock (because of the host reqt. to put power on the bus). --- To Parin, First describe what you want to do. Then I can make better suggestions on how to do it. Peter
by Arsalan(anon) on 09-14-2011 01:40 AM

Hi Peter,

 

Thank you for the quick reply.

 

So do you think i will be able to use the USB API provided in 2.3.4 to communicate with my USB Peripheral Device attached to the Docking station?

 

Because when Dock will power the bus will it not become the USB Host it self? and I think the Android phone will then become the USB Accessorry? Please Correct me if I am wrong?

 

The purpose is to communicate to a USB peripheral device acting as a USB Accessory and use the USB Accessory API on 2.3.4.

 

Many Thanks,

 

-Arsalan

by Peter_vdL Motorola 09-14-2011 12:00 PM - edited 09-14-2011 12:00 PM

I'd need more details of the hardware you are using, to answer your question accurately, Arsalan.

 

Generally, the docks and dongles don't have any processing capability - all they do is supply power (in the case of a dock) or convert between USB plugs (in the case of the dongle).  All the work is done in the phone.  Not all phones support USB OTG.  Most don't.  I don't know if Nexus S does.  That would be a good question to pose to the manufacturer of Nexus S or the vendor.

 

    Peter

by Arsalan(anon) on 09-15-2011 01:33 AM

Hi Peter,

 

The hardware is purpose built input device. So if you pass specific messages to the device it will transmitt data. The device can not be used as a plug n play device on the computer. It needs specific drivers to be installed on the windows machine to read and write data to it.

 

Please dont hesitate to ask me any particular questions about the device.

 

I think Nexsus S doesnot support the USB OTG because it doesnt provides power to the bus. Do you reckon we need a Phone with 2.3.4 and USB OTG to work with Open Accessory API?

 

Just for a thought USB OTG phones will power the bus any ways so we will not need a Docking station to provide isnt it? I have a Galaxy S2 and it supports USB OTG and it powers the bus as well. But the main drawback is it lacks open accessory API and is 2.3.3

 

-Arsalan

by Peter_vdL Motorola on 09-15-2011 04:14 PM

Hi Arsalan,

 

Your best bet to experiment with USB now, is to stick to tablets.   Check that the tablet supports the USB OTG protocol.  I know that the XOOM tablet does.  Then you can write apps that control USB devices from the tablet.   You don't need the Arduino kit.  

 

Alternatively, you can use any Android device that supports the Android USB API (either directly (like XOOM), or with the compatability library), and use the Arduino kit to act as the USB host.  The Arduino kit will let you control the stepper motors, LEDs etc, and animate something (like the droid bug examples).

 

I suppose that eventually some manufacturers will ship a Gingerbread-based phone that supports the USB compatability library and the Open Accessory API, but other manufacturers don't brief me on their product plans.

 

Maybe the situation will be regularized in the Ice Cream sandwich release, which merges the tablet code line with the phone code line.  For now, when working with Android USB, you're working with a newly-introduced technology, that is not yet ubiquitous.  Expect a few bumps as you blaze the trail.

by Arsalan on 09-16-2011 01:38 AM

Hi Peter,

 

Thanks for the reply mate!

 

 

On the other hand I was thinking on integrating a USB Host shield with Arduino kit so that my android device interacts with Arduino kit and in turn arduino interacts with the peripheral...

 

A tablet would be prepherable if it is 7 inch with 3.1...

 

Any ways I have orderd a Multimedia HD Dock and Sony Ericsson Live Dock ..... :smileyhappy: I think soon i will be selling that on ebay!

by danist(anon) on 09-30-2011 03:13 PM

Hi Peter, 

 

best article I found on that issue!

 

Even if the atrix (and the docking station) do not support the Open Accessory API: Have you heard if this will be "fixed"/added in the future? Is it technically possible, that a docking station supports both? If so, is there any device + docking station combination which is capable to support the Open Accessory API and the OTG mode at the same time? I would like to connect my arduino board and a keyboard/mouse/stick.  

 

Kind regards and thx in advance. 

 

Daniel 

by danist(anon) on 10-01-2011 12:13 AM
Hi Peter, great article, thanks! Do you know if motorola is planing on adding ADK support to their existing devices (for example Atrix)? Is it technically possible to connect two adk devices, one in host mode an one as client? If it is possible: Does the docking station (atrix) provide the technical basis to support this? Sorry for the tons of questions. Kind regards, Daniel
by Peter_vdL Motorola on 10-06-2011 10:17 AM

Hi Daniel,

 

Good to hear from you, and thanks for the feedback.

 

The beauty of Google's ADK ("Accessory Development Kit") for the Open Accessory API, is that acts as a USB host.  A very flexible, and easily programmable USB host.   This means that USB devices (like all Motorola handsets, including the ATRIX) already support the hardware part of it, by virtue of supporting a connection from any USB host.  So this means that physically and electrically, handsets can talk to the Arduino kit.

 

The piece that's missing is the software library to do the extra handshaking where the host asks "do you support the Open Accessory API?" and the handset replies "Yes!  Which app should I start, to control your hardware?"   Google built that USB library into Honeycomb (so my understanding is that most honeycomb devices are going to have this), and they provided a backward compatibility library for handset manufacturers to compile into their Gingerbread releases.   I have not seen a lot of industry take-up of the USB compatibility library.  It looks like most OEMs are defering their interest to later releases.

 

The question about connecting two ADK breadboards is an interesting one.   After thinking about it, I concluded this cannot successfully be achieved.  The ADK hardware is a USB host - and there is exactly one USB host on the USB bus at any given time.

 

Regards,

 

Peter

Android Technology Evangelist

by danist(anon) on 10-09-2011 10:18 AM

Hi Peter, 

 

thanks for the reply. I guess you are absolutely right about connecting two usb hosts. 

 

So, do you think it will be possible to connect two adk devices, one as host and one as client? For example one arduino board and one keyboard. Or, one arduino board with an usb host controller and one with an USB client controller. 

 

Kind regards, 

 

daniel 

by Peter_vdL Motorola on 10-11-2011 06:00 PM

Hi Daniel,

 

Here's my thinking - to connect two or more USB "thingies" to an Android unit, you will need a dock.

 

If you use a dock, that only supports connecting USB devices (like keyboard, flash reader, etc), not USB hosts.  So I don't think you will be able to connect the Arduino to an Android unit through a dock.    If anyone wants to give it a try and report back on the actual results, I would be delighted to hear them.   It's entirely possible I'm overlooking something!

by Community Manager on 10-12-2011 03:06 AM
Post a Comment
Be sure to enter a unique name. You can't reuse a name that's already in use.
Be sure to enter a unique email address. You can't reuse an email address that's already in use.
reCAPTCHA challenge image
Type the characters you see in the picture above.Type the words you hear.
About MOTODEV Blog
The MOTODEV blog keeps you updated on mobile app development news from MOTODEV and the Android developer community.

Subscribe to our RSS feed Subscribe via RSS

Follow Us:
Fan MOTODEV on Facebook Join the MOTODEV LinkedIn Group MOTODEV on YouTube

motodev profile

motodev G+ Hangout: Migrating ur legacy systems to the cloud & mobile is at @ 12pm PDT today. Join us and bring your questions. #l2cloud 7 days ago · reply · retweet · favorite

motodev profile

motodev RT @sidneyallen: Google + Hangout kicking off in 10 min moto.ly/l2cloud Join StackMob, Salesforce and MOTODEV Team 7 days ago · reply · retweet · favorite

motodev profile

motodev @officemicro Thanks for joining us! 7 days ago · reply · retweet · favorite

motodev profile

motodev Do u have questions about the Google+ Hangout happening right now? Go to Google Moderator and ask a way: moto.ly/askl2cloud 7 days ago · reply · retweet · favorite

Our Blog & Comment Policy
Opinions expressed here and in any corresponding comments are the personal opinions of the original authors, not of Motorola. The content is provided for informational purposes only and is not meant to be an endorsement or representation by Motorola or any other party.

Remember, when you comment, please stay on topic and avoid spam, profanity, and anything else that violates our user guidelines. All comments require approval by Motorola and can be rejected for any reason.

For customer support issues with your Motorola phone go to the Motorola customer support website.