Home | History | Annotate | Download | only in usb
      1 page.title=Android Open Accessory Development Kit
      2 @jd:body
      3 
      4   <div id="qv-wrapper">
      5     <div id="qv">
      6       <h2>In this document</h2>
      7 
      8       <ol>
      9         <li><a href="#components">ADK Components</a></li>
     10         <li>
     11 
     12           <a href="#getting-started">Getting Started with the ADK</a>
     13 
     14           <ol>
     15             <li><a href="#installing">Installing the Arduino software and necessary
     16             libraries</a></li>
     17 
     18             <li><a href="#installing-firmware">Installing the firmware to the ADK board</a></li>
     19 
     20             <li><a href="#running-demokit">Running the DemoKit Android application</a></li>
     21 
     22             <li><a href="#monitoring">Monitoring the ADK board</a></li>
     23           </ol>
     24         </li>
     25 
     26         <li>
     27           <a href="#accessory-protocol">Implementing the Android Accessory Protocol</a>
     28 
     29           <ol>
     30             <li><a href="#wait">Wait for and detect connected devices</a></li>
     31 
     32             <li><a href="#determine">Determine the connected device's accessory mode
     33             support</a></li>
     34 
     35             <li><a href="#start">Attempt to start the device in accessory mode</a></li>
     36 
     37             <li><a href="#establish">Establish communication with the device</a></li>
     38           </ol>
     39         </li>
     40 
     41         <li>
     42           <a href="#firmware">How the ADK board implements the Android Accessory Protocol</a>
     43 
     44           <ol>
     45             <li><a href="#wait-adk">Wait for and detect connected devices</a></li>
     46 
     47             <li><a href="#determine-adk">Determine the connected device's accessory mode
     48             support</a></li>
     49 
     50             <li><a href="#start-adk">Attempt to start the device in accessory mode</a></li>
     51 
     52             <li><a href="#establish-adk">Establish communication with the device</a></li>
     53           </ol>
     54         </li>
     55       </ol>
     56 
     57       <h2>Download</h2>
     58       <ol>
     59         <li><a href="https://dl-ssl.google.com/android/adk/adk_release_0512.zip">ADK package</a></li>
     60       </ol>
     61 
     62       <h2>See also</h2>
     63 
     64       <ol>
     65         <li><a href="http://www.youtube.com/watch?v=s7szcpXf2rE">Google I/O Session Video</a></li>
     66         <li><a href="{@docRoot}guide/topics/usb/accessory.html">USB Accessory Dev Guide</a></li>
     67       </ol>
     68 
     69       <h2>Where to buy</h2>
     70 
     71       <ol>
     72 
     73       <li><a href="http://store.arduino.cc/">
     74         Arduino Store</a></li>
     75 
     76       <li><a href="https://store.diydrones.com/ProductDetails.asp?ProductCode=BR-PhoneDrone">
     77         DIY Drones</a></li>
     78 
     79       <li><a href=
     80         "http://mbed.org/order/">
     81         mbed</a></li>
     82 
     83       <li><a href=
     84         "http://www.microchip.com/android">
     85         Microchip</a></li>
     86 
     87       <li><a href="http://shop.moderndevice.com/products/freeduino-usb-host-board">
     88         Modern Device</a></li>
     89 
     90       <li><a href=
     91         "http://www.rt-net.jp/shop/index.php?main_page=product_info&cPath=3_4&products_id=1">
     92         RT Corp</a></li>
     93 
     94       <li><a href="http://www.seeedstudio.com/depot/seeeduino-adk-main-board-p-846.html">
     95         Seeed Studio</a></li>
     96 
     97       <li><a href="http://www.sparkfun.com/products/10748">
     98         SparkFun</a></li>
     99 
    100       <li><a href="http://troido.de/de/shoplsmallgbuy-android-stufflsmallg">
    101         Troido</a></li>
    102 
    103       </ol>
    104     </div>
    105   </div>
    106 
    107   <p>The Android 3.1 platform (also backported to Android 2.3.4) introduces Android Open Accessory
    108   support, which allows external USB hardware (an Android USB accessory) to interact with an
    109   Android-powered device in a special "accessory" mode. When an Android-powered powered device is
    110   in accessory mode, the connected accessory acts as the USB host (powers the bus and enumerates
    111   devices) and the Android-powered device acts as the USB device. Android USB accessories are
    112   specifically designed to attach to Android-powered devices and adhere to a simple protocol
    113   (Android accessory protocol) that allows them to detect Android-powered devices that support
    114   accessory mode. Accessories must also provide 500mA at 5V for charging power. Many previously
    115   released Android-powered devices are only capable of acting as a USB device and cannot initiate
    116   connections with external USB devices. Android Open Accessory support overcomes this limitation
    117   and allows you to build accessories that can interact with an assortment of Android-powered
    118   devices by allowing the accessory to initiate the connection.</p>
    119 
    120   <p class="note"><strong>Note:</strong> Accessory mode is ultimately dependent on the device's
    121   hardware and not all devices will support accessory mode. Devices that support accessory mode can
    122   be filtered using a <code>&lt;uses-feature&gt;</code> element in your corresponding application's
    123   Android manifest. For more information, see the <a href=
    124   "{@docRoot}guide/topics/usb/accessory.html#manifest">USB Accessory</a> Developer Guide.</p>
    125 
    126   <p>The following list of distributers are currently producing Android Open Accessory compatible
    127   development boards:</p>
    128 
    129   <ul>
    130 
    131     <li>The <a href="http://store.arduino.cc/">Arduino Store</a> provides the Arduino Mega ADK
    132     (in <a href="http://store.arduino.cc/eu/index.php?main_page=product_info&cPath=11_12&products_id=144">EU nations</a>
    133     or <a href="http://store.arduino.cc/ww/index.php?main_page=product_info&cPath=11_12&products_id=144">non-EU nations</a>)
    134     that is based on the ATmega2560 and supports the ADK firmware.</li>
    135 
    136     <li><a href="https://store.diydrones.com/ProductDetails.asp?ProductCode=BR-PhoneDrone">DIY
    137     Drones</a> provides an Arduino-compatible board geared towards RC (radio controlled) and UAV
    138     (unmanned aerial vehicle) enthusiasts.</li>
    139 
    140     <li><a href="http://mbed.org/order/">mbed</a> provides a microcontroller and a library
    141     to develop accessories that support the Android accessory protocol. For more information, see
    142     <a href="http://mbed.org/cookbook/mbed-with-Android-ADK">mbed with the Android ADK</a>.
    143     </li>
    144 
    145     <li><a href="http://www.microchip.com/android">Microchip</a> provides a PIC based USB
    146     microcontroller board.</li>
    147 
    148     <li><a href="http://shop.moderndevice.com/products/freeduino-usb-host-board">Modern
    149     Device</a> provides an Arduino-compatible board that supports the ADK firmware.</li>
    150 
    151     <li><a href="http://www.rt-net.jp/shop/index.php?main_page=product_info&cPath=3_4&products_id=1">
    152     RT Corp</a> provides an Arduino-compatible board based on the Android ADK board design.</li>
    153 
    154     <li><a href="http://www.seeedstudio.com/depot/seeeduino-adk-main-board-p-846.html">
    155     Seeed Studio</a> provides an Arduino-compatible board that supports the ADK firmware.</li>
    156 
    157     <li><a href="http://www.sparkfun.com/products/10748">
    158     SparkFun</a>'s IOIO board now has beta support for the ADK firmware.</li>
    159 
    160   </ul>
    161 
    162   <p>We expect more hardware distributers to create a variety of kits, so please stay tuned for
    163   further developments.</p>
    164 
    165   <h2 id="components">ADK Components</h2>
    166   <p>The Android Open Accessory Development Kit (ADK) provides an implementation of an Android USB
    167   accessory that is based on the <a href="http://www.arduino.cc/">Arduino open source electronics
    168   prototyping platform</a>, the accessory's hardware design files, code that implements the
    169   accessory's firmware, and the Android application that interacts with the accessory. The hardware
    170   design files and firmware code are contained in the <a href=
    171   "https://dl-ssl.google.com/android/adk/adk_release_0512.zip">ADK package download</a>.</p>
    172   <p>The main hardware and software components of the ADK include:</p>
    173 
    174   <ul>
    175     <li>A USB micro-controller board that is based on the Arduino Mega2560 and Circuits@Home USB
    176     Host Shield designs (now referred to as the ADK board), which you will later implement as an
    177     Android USB accessory. The ADK board provides input and output pins that you can implement
    178     through the use of attachments called "shields." Custom firmware, written in C++, is installed
    179     on the board to define the board's functionality and interaction with the attached shield and
    180     Android-powered device. The hardware design files for the board are located in
    181     <code>hardware/</code> directory.</li>
    182 
    183     <li>An Android Demo Shield (ADK shield) that affixes atop the ADK board implements the input
    184     and output points on the board. These implementations include a joystick, LED outputs, and
    185     temperature and light sensors. You can create or buy your own shields or wire your own features
    186     to the ADK board to implement custom functionality. The hardware design files for the shield
    187     are located in <code>hardware/</code>.</li>
    188 
    189     <li>A library based on the <a href=
    190     "http://www.circuitsathome.com/arduino_usb_host_shield_projects">Arduino USB Host Shield</a>
    191     library provides the logic for the USB micro-controller board to act as a USB Host. This allows
    192     the board to initiate transactions with USB devices. Describing how to use this entire library
    193     is out of the scope of this document. Where needed, this document points out important
    194     interactions with the library. For more information, see the source code for the Arduino USB
    195     Host Shield library in the <code>firmware/arduino_libs/USB_Host_Shield</code> directory.</li>
    196 
    197     <li>An Arduino sketch, <code>firmware/demokit/demokit.pde</code>, defines the firmware that
    198     runs on the ADK board and is written in C++. The sketch calls the Android accessory protocol
    199     library to interact with the Android-powered device. It also sends data from the ADK board and
    200     shield to the Android application and receives data from the Android application and outputs it
    201     to the ADK board and shield.</li>
    202 
    203     <li>The Android accessory protocol library, which is located in the
    204     <code>firmware/arduino_libs/AndroidAccessory</code> directory. This library defines how to
    205     enumerate the bus, find a connected Android-powered device that supports accessory mode, and
    206     how to setup communication with the device.</li>
    207 
    208     <li>Other third party libraries to support the ADK board's functionality:
    209       <ul>
    210         <li><a href="http://www.arduino.cc/playground/Main/CapSense">CapSense library</a></li>
    211 
    212         <li><a href="http://www.arduino.cc/playground/Learning/I2C">I2C / TWI (Two-Wire Interface)
    213         library</a></li>
    214 
    215         <li><a href="http://www.arduino.cc/playground/ComponentLib/Servo">Servo library</a></li>
    216 
    217         <li><a href="http://www.arduino.cc/playground/Code/Spi">Spi library</a></li>
    218 
    219         <li><a href="http://www.arduino.cc/en/Reference/Wire">Wire library</a></li>
    220 
    221         <li>An Android application, DemoKit, that communicates with the ADK board and shield. The
    222         source for this project is in the <code>app/</code> directory.</li>
    223       </ul>
    224     </li>
    225 
    226   </ul>
    227 
    228   <h2 id="getting-started">Getting Started with the ADK</h2>
    229 
    230   <p>The following sections describe how to install the Arduino software on your computer, use the
    231   Arduino software to install the ADK board's firmware, and install and run the accompanying
    232   Android application for the ADK board. Before you begin, download the following items to set up
    233   your development environment:</p>
    234 
    235   <ul>
    236     <li><a href="http://www.arduino.cc/en/Main/software">Arduino Software</a>: contains libraries
    237     and an IDE for coding and installing firmware to the ADK board.</li>
    238 
    239     <li><a href="http://www.arduino.cc/playground/Main/CapSense">CapSense library</a>: contains the
    240     libraries to sense human capacitance. This is needed for the capacative button that is located
    241     on the ADK shield.</li>
    242 
    243     <li><a href="https://dl-ssl.google.com/android/adk/adk_release_0512.zip">The ADK package</a>: contains the firmware for the ADK board and hardware design
    244     files for the ADK board and shield.</li>
    245   </ul>
    246 
    247   <h3 id="installing">Installing the Arduino software and necessary libraries</h3>
    248 
    249   <p>To install the Arduino software:</p>
    250 
    251   <ol>
    252     <li>
    253       <a href="http://arduino.cc/en/Guide/HomePage">Download and install</a> the Arduino Software
    254       as described on the Arduino website.
    255 
    256       <p class="note"><strong>Note:</strong> If you are on a Mac, install the FTDI USB Serial
    257       Driver that is included in the Arduino package, even though the installation instructions say
    258       otherwise.</p>
    259     </li>
    260 
    261     <li><a href="https://dl-ssl.google.com/android/adk/adk_release_0512.zip">Download</a> and
    262     extract the ADK package to a directory of your choice. You should have an <code>app</code>,
    263     <code>firmware</code>, and <code>hardware</code> directories.</li>
    264 
    265     <li>Extract the CapSense download to a directory of your choice.</li>
    266 
    267     <li>Install the necessary libraries:
    268 
    269       <p>On Windows:</p>
    270 
    271       <ol type="a">
    272         <li>Copy the <code>firmware/arduino_libs/AndroidAccessory</code> and
    273         <code>firmware/arduino_libs/USB_Host_Shield</code> directories (the complete directories,
    274         not just the files within) to the <code>&lt;arduino_installation_root&gt;/libraries/</code>
    275         directory.</li>
    276 
    277         <li>Create a CapSense directory in the
    278         <code>&lt;arduino_installation_root&gt;/libraries/</code> directory</li>
    279 
    280         <li>Copy <code>CapSense.cpp</code> and <code>CapSense.h</code> from the unzipped CapSense
    281         download to the <code>CapSense</code> directory.</li>
    282       </ol>
    283 
    284       <p>On Mac:</p>
    285 
    286       <ol type="a">
    287         <li>Create, if it does not already exist, an <code>Arduino</code>
    288         directory inside your user account's <code>Documents</code> directory, and within
    289         that, a <code>libraries</code> directory.</li>
    290 
    291         <li>Copy the <code>firmware/arduino_libs/AndroidAccessory</code> and
    292         <code>firmware/arduino_libs/USB_Host_Shield</code> directories (the
    293         complete directories, not just the files within) to your
    294         <code>Documents/Arduino/libraries/</code> directory.</li>
    295 
    296         <li>Create a <code>CapSense</code> directory in your
    297         <code>Documents/Arduino/libraries/</code> directory.</li>
    298 
    299         <li>Copy <code>CapSense.cpp</code> and <code>CapSense.h</code> from the unzipped CapSense
    300         download to the <code>CapSense</code> directory.</li>
    301       </ol>
    302 
    303       <p>On Linux (Ubuntu):</p>
    304 
    305       <ol type="a">
    306         <li>Copy the <code>firmware/arduino_libs/AndroidAccessory</code> and
    307         <code>firmware/arduino_libs/USB_Host_Shield</code> directories (the complete directories,
    308         not just the files within) to the <code>&lt;arduino_installation_root&gt;/libraries/</code>
    309         directory.</li>
    310 
    311         <li>Create a <code>CapSense</code> directory in the
    312         <code>&lt;arduino_installation_root&gt;/libraries/</code> directory.</li>
    313 
    314         <li>Copy <code>CapSense.cpp</code> and <code>CapSense.h</code> from the unzipped CapSense
    315         download to the <code>CapSense</code> directory.</li>
    316 
    317         <li>Install the avr-libc library by entering <code>sudo apt-get install avr-libc</code>
    318         from a shell prompt.</li>
    319       </ol>
    320     </li>
    321   </ol>
    322 
    323   <p>You should now have three new directories in the Arduino libraries directory:
    324   <code>AndroidAccessory</code>, <code>USB_Host_Shield</code>, and <code>CapSense</code>.</p>
    325 
    326   <h3 id="installing-firmware">Installing the firmware to the ADK board</h3>
    327 
    328   <p>To install the firmware to the ADK board:</p>
    329 
    330   <ol>
    331     <li>Connect the ADK board to your computer using the micro-USB port, which allows two-way
    332     communication and provides power to the ADK board.</li>
    333 
    334     <li>Launch Arduino.</li>
    335 
    336     <li>Click <strong>Tools &gt; Board &gt; Arduino Mega 2560</strong> to specify the ADK board's
    337     type.</li>
    338 
    339     <li>Select the appropriate USB port:
    340 
    341       <ul>
    342         <li>On Windows: click <strong>Tools &gt; Serial Port &gt; COM#</strong> to specify the port
    343         of communication. The COM port number varies depending on your computer. COM1 is usually
    344         reserved for serial port connections. You most likely want COM2 or COM3.</li>
    345 
    346         <li>On Mac: Click <strong>Tools &gt; Serial Port &gt; dev/tty.usbserial-###</strong> to
    347         specify the port of communication.</li>
    348 
    349         <li>On Linux (Ubuntu): Click <strong>Tools &gt; Serial Port &gt; dev/ttyUSB#</strong> to
    350         specify the port of communication.</li>
    351       </ul>
    352     </li>
    353 
    354     <li>To open the firmware code (a sketch), click <strong>File &gt; Open</strong> and select
    355     <code>firmware/demokit/demokit.pde</code>.</li>
    356 
    357     <li>Click <strong>Sketch &gt; Verify/Compile</strong> to ensure that the sketch has no
    358     errors.</li>
    359 
    360     <li>Select <strong>File &gt; Upload to I/O Board</strong>. When Arduino outputs <strong>Done
    361     uploading.</strong>, the board is ready to communicate with your Android-powered device.</li>
    362   </ol>
    363 
    364   <h3 id="running-demokit">Running the DemoKit Android application</h3>
    365 
    366   <p>The DemoKit Android application runs on your Android-powered device and communicates with the
    367   ADK board. The ADK board receives commands such as lighting up the board's LEDs or sends data
    368   from the board such as joystick movement and temperature readings.</p>
    369 
    370   <p>To install and run the application in Eclipse:</p>
    371 
    372   <ol>
    373     <li><a href="http://code.google.com/android/add-ons/google-apis/installing.html">Install the
    374     Google APIs API Level 10 add-on library</a>, which includes the Open Accessory library for
    375     2.3.4 devices that support accessory mode. This library is also forward compatible with Android
    376     3.1 or newer devices that support accessory mode. If you only care about Android 3.1 or newer
    377     devices, all you need is API Level 12. For more information on deciding which API level to use,
    378     see the <a href="{@docRoot}guide/topics/usb/accessory.html#choosing">USB Accessory</a>
    379     documentation.</li>
    380 
    381     <li>Click <strong>File &gt; New &gt; Project...</strong>, then select <strong>Android &gt;
    382     Android Project</strong></li>
    383 
    384     <li>In the <strong>Project name:</strong> field, type DemoKit.</li>
    385 
    386     <li>Choose <strong>Create project from existing source</strong>, click <strong>Browse</strong>,
    387     select the <code>app</code> directory, click <strong>Open</strong> to close that dialog and then
    388     click <strong>Finish</strong>.</li>
    389 
    390     <li>For Build Target, select <strong>Google APIs</strong> (Platform 2.3.3, API Level 10).
    391 
    392       <p class="note"><strong>Note:</strong> Even though the add-on is labeled as
    393       <strong>2.3.3</strong>, the newest Google API add-on library for API level 10 adds USB Open
    394       Accessory API support for 2.3.4 devices.</p>
    395     </li>
    396 
    397     <li>Click <strong>Finish</strong>.</li>
    398 
    399     <li>Install the application to your device.</li>
    400 
    401     <li>Connect the ADK board (USB-A) to your Android-powered device (micro-USB). Ensure that the
    402     power cable to the accessory is plugged in or that the micro-USB port on the accesory is
    403     connected to your computer for power (this also allows you to <a href="#monitoring">monitor the
    404     ADK board</a>). When connected, accept the prompt that asks for whether or not to open the
    405     DemoKit application to connect to the accessory. If the prompt does not show up, connect and
    406     reconnect the accessory.</li>
    407   </ol>
    408 
    409   <p>You can now interact with the ADK board by moving the color LED or servo sliders (make sure
    410   the servos are connected) or by pressing the relay buttons in the application. On the ADK shield,
    411   you can press the buttons and move the joystick to see their outputs displayed in the
    412   application.</p>
    413 
    414   <h3 id="monitoring">Monitoring the ADK Board</h3>
    415 
    416   <p>The ADK firmware consists of a few files that you should be looking at if you want to build
    417   your own accessory. The files in the <code>firmware/arduino_libs/AndroidAccessory</code>
    418   directory are the most important files and have the logic to detect and connect to
    419   Android-powered devices that support accessory mode. Feel free to add debug statements (Arduino
    420   <code>Serial.print()</code> statements) to the code located in the
    421   <code>arduino_libraries_directory/AndroidAccessory</code> directory and
    422   <code>firmware/demokit/demokit.pde</code> sketch and re-upload the sketch to the ADK board to
    423   discover more about how the firmware works.</p>
    424 
    425   <p>You can view the debug statements in the Arduino Serial Monitor by clicking <strong>Tools &gt;
    426   Serial Monitor</strong> and setting the baud to 115200. The following sections about how
    427   accessories communicate with Android-powered devices describe much of what you should be doing in
    428   your own accessory.</p>
    429 
    430   <h2 id="accessory-protocol">Implementing the Android Accessory Protocol</h2>
    431 
    432   <p>An Android USB accessory must adhere to Android Accessory Protocol, which defines how
    433   an accessory detects and sets up communication with an Android-powered device. In general, an
    434   accessory should carry out the following steps:</p>
    435 
    436   <ol>
    437     <li>Wait for and detect connected devices</li>
    438 
    439     <li>Determine the device's accessory mode support</li>
    440 
    441     <li>Attempt to start the device in accessory mode if needed</li>
    442 
    443     <li>Establish communication with the device if it supports the Android accessory protocol</li>
    444   </ol>
    445 
    446   <p>The following sections go into depth about how to implement these steps.</p>
    447 
    448   <h3 id="wait">Wait for and detect connected devices</h3>
    449 
    450   <p>Your accessory should have logic to continuously check
    451   for connected Android-powered devices. When a device is connected, your accessory should
    452   determine if the device supports accessory mode.</p>
    453 
    454   <h3 id="determine">Determine the device's accessory mode support</h3>
    455 
    456 
    457   <p>When an Android-powered device is connected, it can be in one of three states:</p>
    458 
    459   <ol type="a">
    460     <li>The attached device supports Android accessory mode and is already in accessory mode.</li>
    461 
    462     <li>The attached device supports Android accessory mode, but it is not in accessory mode.</li>
    463 
    464     <li>The attached device does not support Android accessory mode.</li>
    465   </ol>
    466 
    467   <p>During the initial connection, the accessory should check the vendor and product IDs of the
    468   connected device's USB device descriptor. The vendor ID should match Google's ID (0x18D1) and the
    469   product ID should be 0x2D00 or 0x2D01 if the device is already in accessory mode (case A). If so,
    470   the accessory can now <a href="#establish">establish communication with the device</a> through
    471   bulk transfer endpoints with its own communication protocol. There is no need to start the device
    472   in accessory mode.</p>
    473 
    474   <p class="note"><strong>Note:</strong> 0x2D00 is reserved for Android-powered devices that
    475   support accessory mode. 0x2D01 is reserved for devices that support accessory mode as well as the
    476   ADB (Android Debug Bridge) protocol, which exposes a second interface with two bulk endpoints for
    477   ADB. You can use these endpoints for debugging the accessory application if you are simulating
    478   the accessory on a computer. In general, do not use this interface unless your accessory is
    479   implementing a passthrough to ADB on the device.</p>
    480 
    481   <p>If the vendor and product ID do not match, there is no way to distinguish between states b and
    482   c, so the accessory <a href="#start">attempts to start the device in accessory mode</a> to figure
    483   out if the device is supported.</p>
    484 
    485   <h3 id="start">Attempt to start the device in accessory mode</h3>
    486 
    487   <p>If the vendor and product IDs do not correspond to an Android-powered device in accessory
    488   mode, the accessory cannot discern whether the device supports accessory mode and is not in that
    489   state, or if the device does not support accessory mode at all. This is because devices that
    490   support accessory mode but aren't in it initially report the device's manufacturer vendor ID and
    491   product ID, and not the special Android Open Accessory ones. In either case, the accessory should try to start
    492   the device into accessory mode to figure out if the device supports it. The following steps
    493   explain how to do this:</p>
    494 
    495   <ol>
    496     <li>Send a 51 control request ("Get Protocol") to figure out if the device supports the Android
    497     accessory protocol. A non-zero number is returned if the protocol is supported, which
    498     represents the version of the protocol that the device supports (currently, only version 1
    499     exists). This request is a control request on endpoint 0 with the following characteristics:
    500       <pre>
    501 requestType:    USB_DIR_IN | USB_TYPE_VENDOR
    502 request:        51
    503 value:          0
    504 index:          0
    505 data:           protocol version number (16 bits little endian sent from the device to the accessory)
    506 </pre>
    507     </li>
    508 
    509     <li>If the device returns a proper protocol version, send identifying string information to the
    510     device. This information allows the device to figure out an appropriate application for this
    511     accessory and also present the user with a URL if an appropriate application does not exist.
    512     These requests are control requests on endpoint 0 (for each string ID) with the following
    513     characteristics:
    514       <pre>
    515 requestType:    USB_DIR_OUT | USB_TYPE_VENDOR
    516 request:        52
    517 value:          0
    518 index:          string ID
    519 data            zero terminated UTF8 string sent from accessory to device
    520 </pre>
    521 
    522       <p>The following string IDs are supported, with a maximum size of 256 bytes for each string
    523       (must be zero terminated with \0).</p>
    524       <pre>
    525 manufacturer name:  0
    526 model name:         1
    527 description:        2
    528 version:            3
    529 URI:                4
    530 serial number:      5
    531 </pre>
    532     </li>
    533 
    534     <li>When the identifying strings are sent, request the device start up in accessory mode. This
    535     request is a control request on endpoint 0 with the following characteristics:
    536       <pre>
    537 requestType:    USB_DIR_OUT | USB_TYPE_VENDOR
    538 request:        53
    539 value:          0
    540 index:          0
    541 data:           none
    542 </pre>
    543     </li>
    544   </ol>
    545 
    546   <p>After sending the final control request, the connected USB device should re-introduce itself
    547   on the bus in accessory mode and the accessory can re-enumerate the connected devices. The
    548   algorithm jumps back to <a href="#determine">determining the device's accessory mode support</a>
    549   to check for the vendor and product ID. The vendor ID and product ID of the device will be
    550   different if the device successfully switched to accessory mode and will now correspond to
    551   Google's vendor and product IDs instead of the device manufacturer's IDs. The accessory can now
    552   <a href="#establish">establish communication with the device</a>.</p>
    553 
    554   <p>If at any point these steps fail, the device does not support Android accessory mode and the
    555   accessory should wait for the next device to be connected.</p>
    556 
    557   <h3 id="establish">Establish communication with the device</h3>
    558 
    559   <p>If an Android-powered device in accessory mode is detected, the accessory can query the
    560   device's interface and endpoint descriptors to obtain the bulk endpoints to communicate with the
    561   device. An Android-powered device that has a product ID of 0x2D00 has one interface with two bulk
    562   endpoints for input and output communication. A device with product ID of 0x2D01 has two
    563   interfaces with two bulk endpoints each for input and output communication. The first interface
    564   is for standard communication while the second interface is for ADB communication. To communicate
    565   on an interface, all you need to do is find the first bulk input and output endpoints, set the
    566   device's configuration to a value of 1 with a SET_CONFIGURATION (0x09) device request, then
    567   communicate using the endpoints.</p>
    568 
    569   <h2 id="firmware">How the ADK board implements the Android Accessory protocol</h2>
    570 
    571   <p>If you have access to the ADK board and shield, the following sections describe the firmware
    572   code that you installed onto the ADK board. The firmware demonstrates a practical example of how
    573   to implement the Android Accessory protocol. Even if you do not have the ADK board and shield,
    574   reading through how the hardware detects and interacts with devices in accessory mode is still
    575   useful if you want to port the code over for your own accessories.</p>
    576 
    577   <p>The important pieces of the firmware are the
    578   <code>accessory/demokit/demokit/demokit.pde</code> sketch, which is the code that receives and
    579   sends data to the DemoKit application running on the Android-powered device. The code to detect
    580   and set up communication with the Android-powered device is contained in the
    581   <code>accessory/arduino_libs/AndroidAccessory/AndroidAccessory.h</code> and
    582   <code>accessory/arduino_libs/AndroidAccessory/AndroidAccessory.cpp</code> files. This code
    583   includes most of the logic that will help you implement your own accessory's firmware. It might
    584   be useful to have all three of these files open in a text editor as you read through these next
    585   sections.</p>
    586 
    587   <p>The following sections describe the firmware code in the context of the algorithm described in
    588   <a href="#accessory-protocol">Implementing the Android Accessory Protocol</a>.</p>
    589 
    590   <h3 id="wait-adk">Wait for and detect connected devices</h3>
    591 
    592   <p>In the firmware code (<code>demokit.pde</code>), the <code>loop()</code> function runs
    593   repeatedly and calls <code>AndroidAccessory::isConnected()</code> to check for any connected
    594   devices. If there is a connected device, it continuously updates the input and output streams
    595   going to and from the board and application. If nothing is connected, it continuously checks for
    596   a device to be connected:</p>
    597   <pre>
    598 ...
    599 
    600 AndroidAccessory acc("Google, Inc.",
    601                      "DemoKit",
    602                      "DemoKit Arduino Board",
    603                      "1.0",
    604                      "http://www.android.com",
    605                      "0000000012345678");
    606 
    607 ...
    608 void loop()
    609 {
    610 ...
    611     if (acc.isConnected()) {
    612         //communicate with Android application
    613     }
    614     else{
    615         //set the accessory to its default state
    616     }
    617 ...
    618 }
    619 </pre>
    620 
    621   <h3 id="determine-adk">Determine the connected device's accessory mode support</h3>
    622 
    623   <p>When a device is connected to the ADK board, it can already be in accessory mode, support
    624   accessory mode and is not in that mode, or does not support accessory mode. The
    625   <code>AndroidAccessory::isConnected()</code> method checks for these cases and responds
    626   accordingly when the <code>loop()</code> function calls it. This function first checks to see if
    627   the device that is connected hasn't already been handled. If not, it gets the connected device's
    628   device descriptor to figure out if the device is already in accessory mode by calling
    629   <code>AndroidAccessory::isAccessoryDevice()</code>. This method checks the vendor and product ID
    630   of the device descriptor. A device in accessory mode has a vendor ID of 0x18D1 and a product ID
    631   of 0x2D00 or 0x2D01. If the device is in accessory mode, then the ADK board can <a href=
    632   "#establish">establish communication with the device</a>. If not, the board <a href=
    633   "#start">attempts to start the device in accessory mode</a>.</p>
    634   <pre>
    635 bool AndroidAccessory::isConnected(void)
    636 {
    637     USB_DEVICE_DESCRIPTOR *devDesc = (USB_DEVICE_DESCRIPTOR *) descBuff;
    638     byte err;
    639 
    640     max.Task();
    641     usb.Task();
    642 
    643     if (!connected &amp;&amp;
    644         usb.getUsbTaskState() &gt;= USB_STATE_CONFIGURING &amp;&amp;
    645         usb.getUsbTaskState() != USB_STATE_RUNNING) {
    646         Serial.print("\nDevice addressed... ");
    647         Serial.print("Requesting device descriptor.");
    648 
    649         err = usb.getDevDescr(1, 0, 0x12, (char *) devDesc);
    650         if (err) {
    651             Serial.print("\nDevice descriptor cannot be retrieved. Program Halted\n");
    652             while(1);
    653         }
    654 
    655         if (isAccessoryDevice(devDesc)) {
    656             Serial.print("found android accessory device\n");
    657 
    658             connected = configureAndroid();
    659         } else {
    660             Serial.print("found possible device. switching to serial mode\n");
    661             switchDevice(1);
    662         }
    663     } else if (usb.getUsbTaskState() == USB_DETACHED_SUBSTATE_WAIT_FOR_DEVICE) {
    664         connected = false;
    665     }
    666 
    667     return connected;
    668 }
    669 </pre>
    670 
    671   <h3 id="start-adk">Attempt to start the device in accessory mode</h3>
    672 
    673   <p>If the device is not already in accessory mode, then the ADK board must determine whether or
    674   not it supports it by sending control request 51 to check the version of the USB accessory
    675   protocol that the device supports (see <code>AndroidAccessory::getProtocol()</code>). Protocol
    676   version 1 is the only version for now, but this can be an integer greater than zero in the
    677   future. If the appropriate protocol version is returned, the board sends control request 52 (one
    678   for each string with <code>AndroidAcessory:sendString()</code>) to send it's identifying
    679   information, and tries to start the device in accessory mode with control request 53. The
    680   <code>AndroidAccessory::switchDevice()</code> method takes care of this:</p>
    681   <pre>
    682 bool AndroidAccessory::switchDevice(byte addr)
    683 {
    684     int protocol = getProtocol(addr);
    685     if (protocol == 1) {
    686         Serial.print("device supports protocol 1\n");
    687     } else {
    688         Serial.print("could not read device protocol version\n");
    689         return false;
    690     }
    691 
    692     sendString(addr, ACCESSORY_STRING_MANUFACTURER, manufacturer);
    693     sendString(addr, ACCESSORY_STRING_MODEL, model);
    694     sendString(addr, ACCESSORY_STRING_DESCRIPTION, description);
    695     sendString(addr, ACCESSORY_STRING_VERSION, version);
    696     sendString(addr, ACCESSORY_STRING_URI, uri);
    697     sendString(addr, ACCESSORY_STRING_SERIAL, serial);
    698 
    699     usb.ctrlReq(addr, 0, USB_SETUP_HOST_TO_DEVICE | USB_SETUP_TYPE_VENDOR | USB_SETUP_RECIPIENT_DEVICE,
    700                 ACCESSORY_START, 0, 0, 0, 0, NULL);
    701     return true;
    702 }
    703 </pre>If this method returns false, the board waits until a new device is connected. If it is
    704 successful, the device displays itself on the USB bus as being in accessory mode when the ADK board
    705 re-enumerates the bus. When the device is in accessory mode, the accessory then <a href=
    706 "#establish-adk">establishes communication with the device</a>.
    707 
    708   <h3 id="establish-adk">Establish communication with the device</h3>
    709 
    710   <p>If a device is detected as being in accessory mode, the accessory must find the proper bulk
    711   endpoints and set up communication with the device. When the ADK board detects an Android-powered
    712   device in accessory mode, it calls the <code>AndroidAccessory::configureAndroid()</code>
    713   function:</p>
    714   <pre>
    715 ...
    716 if (isAccessoryDevice(devDesc)) {
    717             Serial.print("found android acessory device\n");
    718 
    719             connected = configureAndroid();
    720         }
    721 ...
    722 </pre>
    723 
    724   <p>which in turn calls the <code>findEndpoints()</code> function:</p>
    725   <pre>
    726 ...
    727 bool AndroidAccessory::configureAndroid(void)
    728 {
    729     byte err;
    730     EP_RECORD inEp, outEp;
    731 
    732     if (!findEndpoints(1, &amp;inEp, &amp;outEp))
    733         return false;
    734 ...
    735 </pre>
    736 
    737   <p>The <code>AndroidAccessory::findEndpoints()</code> function queries the Android-powered
    738   device's configuration descriptor and finds the bulk data endpoints in which to communicate with
    739   the USB device. To do this, it first gets the device's first four bytes of the configuration
    740   descriptor (only need descBuff[2] and descBuff[3]), which contains the information about the
    741   total length of data returned by getting the descriptor. This data is used to determine whether
    742   or not the descriptor can fit in the descriptor buffer. This descriptor also contains information
    743   about all the interfaces and endpoint descriptors. If the descriptor is of appropriate size, the
    744   method reads the entire configuration descriptor and fills the entire descriptor buffer with this
    745   device's configuration descriptor. If for some reason the descriptor is no longer attainable, an
    746   error is returned.</p>
    747   <pre>
    748 ...
    749 
    750 bool AndroidAccessory::findEndpoints(byte addr, EP_RECORD *inEp, EP_RECORD *outEp)
    751 {
    752     int len;
    753     byte err;
    754     uint8_t *p;
    755 
    756     err = usb.getConfDescr(addr, 0, 4, 0, (char *)descBuff);
    757     if (err) {
    758         Serial.print("Can't get config descriptor length\n");
    759         return false;
    760     }
    761 
    762 
    763     len = descBuff[2] | ((int)descBuff[3] &lt;&lt; 8);
    764     if (len &gt; sizeof(descBuff)) {
    765         Serial.print("config descriptor too large\n");
    766             /* might want to truncate here */
    767         return false;
    768     }
    769 
    770     err = usb.getConfDescr(addr, 0, len, 0, (char *)descBuff);
    771     if (err) {
    772         Serial.print("Can't get config descriptor\n");
    773         return false;
    774     }
    775 
    776 ...
    777 </pre>
    778 
    779   <p>Once the descriptor is in memory, a pointer is assigned to the first position of the buffer
    780   and is used to index the buffer for reading. There are two endpoint pointers (input and output)
    781   that are passed into <code>AndroidAccessory::findEndpoints()</code> and their addresses are set
    782   to 0, because the code hasn't found any suitable bulk endpoints yet. A loop reads the buffer,
    783   parsing each configuration, interface, or endpoint descriptor. For each descriptor, Position 0
    784   always contains the size of the descriptor in bytes and position 1 always contains the descriptor
    785   type. Using these two values, the loop skips any configuration and interface descriptors and
    786   increments the buffer with the <code>descLen</code> variable to get to the next descriptor.</p>
    787 
    788   <p class="note"><strong>Note:</strong> An Android-powered device in accessory mode can
    789   potentially have two interfaces, one for the default communication to the device and the other
    790   for ADB communication. The default communication interface is always indexed first, so finding
    791   the first input and output bulk endpoints will return the default communication endpoints, which
    792   is what the <code>demokit.pde</code> sketch does. If you are writing your own firmware, the logic
    793   to find the appropriate endpoints for your accessory might be different.</p>
    794 
    795   <p>When it finds the first input and output endpoint descriptors, it sets the endpoint pointers
    796   to those addresses. If the findEndpoints() function finds both an input and output endpoint, it
    797   returns true. It ignores any other endpoints that it finds (the endpoints for the ADB interface,
    798   if present).</p>
    799   <pre>
    800 ...
    801     p = descBuff;
    802     inEp-&gt;epAddr = 0;
    803     outEp-&gt;epAddr = 0;
    804     while (p &lt; (descBuff + len)){
    805         uint8_t descLen = p[0];
    806         uint8_t descType = p[1];
    807         USB_ENDPOINT_DESCRIPTOR *epDesc;
    808         EP_RECORD *ep;
    809 
    810         switch (descType) {
    811         case USB_DESCRIPTOR_CONFIGURATION:
    812             Serial.print("config desc\n");
    813             break;
    814 
    815         case USB_DESCRIPTOR_INTERFACE:
    816             Serial.print("interface desc\n");
    817             break;
    818 
    819         case USB_DESCRIPTOR_ENDPOINT:
    820             epDesc = (USB_ENDPOINT_DESCRIPTOR *)p;
    821             if (!inEp-&gt;epAddr &amp;&amp; (epDesc-&gt;bEndpointAddress &amp; 0x80))
    822                 ep = inEp;
    823             else if (!outEp-&gt;epAddr)
    824                 ep = outEp;
    825             else
    826                 ep = NULL;
    827 
    828             if (ep) {
    829                 ep-&gt;epAddr = epDesc-&gt;bEndpointAddress &amp; 0x7f;
    830                 ep-&gt;Attr = epDesc-&gt;bmAttributes;
    831                 ep-&gt;MaxPktSize = epDesc-&gt;wMaxPacketSize;
    832                 ep-&gt;sndToggle = bmSNDTOG0;
    833                 ep-&gt;rcvToggle = bmRCVTOG0;
    834             }
    835             break;
    836 
    837         default:
    838             Serial.print("unkown desc type ");
    839             Serial.println( descType, HEX);
    840             break;
    841         }
    842 
    843         p += descLen;
    844     }
    845 
    846     if (!(inEp-&gt;epAddr &amp;&amp; outEp-&gt;epAddr))
    847         Serial.println("can't find accessory endpoints");
    848 
    849     return inEp-&gt;epAddr &amp;&amp; outEp-&gt;epAddr;
    850 }
    851 
    852 ...
    853 </pre>
    854 
    855   <p>Back in the <code>configureAndroid()</code> function, if there were endpoints found, they are
    856   appropriately set up for communication. The device's configuration is set to 1 and the state of
    857   the device is set to "running", which signifies that the device is properly set up to communicate
    858   with your USB accessory. Setting this status prevents the device from being re-detected and
    859   re-configured in the <code>AndroidAccessory::isConnected()</code> function.</p>
    860   <pre>
    861 bool AndroidAccessory::configureAndroid(void)
    862 {
    863     byte err;
    864     EP_RECORD inEp, outEp;
    865 
    866     if (!findEndpoints(1, &amp;inEp, &amp;outEp))
    867         return false;
    868 
    869     memset(&amp;epRecord, 0x0, sizeof(epRecord));
    870 
    871     epRecord[inEp.epAddr] = inEp;
    872     if (outEp.epAddr != inEp.epAddr)
    873         epRecord[outEp.epAddr] = outEp;
    874 
    875     in = inEp.epAddr;
    876     out = outEp.epAddr;
    877 
    878     Serial.print("inEp: ");
    879     Serial.println(inEp.epAddr, HEX);
    880     Serial.print("outEp: ");
    881     Serial.println(outEp.epAddr, HEX);
    882 
    883     epRecord[0] = *(usb.getDevTableEntry(0,0));
    884     usb.setDevTableEntry(1, epRecord);
    885 
    886     err = usb.setConf( 1, 0, 1 );
    887     if (err) {
    888         Serial.print("Can't set config to 1\n");
    889         return false;
    890     }
    891 
    892     usb.setUsbTaskState( USB_STATE_RUNNING );
    893 
    894     return true;
    895 }
    896 </pre>
    897 
    898   <p>Lastly, methods to read and write to the appropriate endpoints are needed. The
    899   <code>demokit.pde</code> sketch calls these methods depending on the data that is read from the
    900   Android-powered device or sent by the ADK board. For instance, moving the joystick on the ADK
    901   shield writes data that is read by the DemoKit application running on the Android-powered device.
    902   Moving sliders on the DemoKit application is read by the <code>demokit.pde</code> sketch and
    903   changes the state of the accessory, such as lighting up or changing the color of the LED
    904   lights.</p>
    905   <pre>
    906 int AndroidAccessory::read(void *buff, int len, unsigned int nakLimit) {
    907   return usb.newInTransfer(1, in, len, (char *)buff, nakLimit); }
    908 
    909 int AndroidAccessory::write(void *buff, int len) {
    910   usb.outTransfer(1, out, len, (char *)buff);
    911   return len; }
    912 
    913 </pre>
    914 
    915   <p>See the <code>firmware/demokit/demokit.pde</code> file for information about how the ADK board
    916   reads and writes data.</p>
    917