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