1 page.title=Accessory Development Kit 2012 Guide 2 @jd:body 3 4 <div id="qv-wrapper"> 5 <div id="qv"> 6 <h2>In this document</h2> 7 <ol> 8 <li><a href="#components">Components</a></li> 9 <li><a href="#clock">Using the Alarm Clock</a></li> 10 <li><a href="#play-audio">Playing Audio</a></li> 11 <li><a href="#dev">Developing Accessories with ADK 2012</a> 12 <ol> 13 <li><a href="#src-download">Downloading the ADK Source</a></li> 14 <li><a href="#dev-setup">Setting Up the Development Environment</a></li> 15 <li><a href="#alt-build">Using the ADK Alternative Build System</a></li> 16 </ol> 17 </li> 18 <li><a href="#adk-conn">How the ADK Connects with Android Devices</a> 19 <ol> 20 <li><a href="#adk-conn-bt">ADK Connection over Bluetooth</a></li> 21 <li><a href="#adk-conn-usb">ADK Connection over USB</a></li> 22 </ol> 23 </li> 24 <li><a href="#audio-dock">USB Audio Dock Implementation</a></li> 25 </ol> 26 27 <h2>See also</h2> 28 <ol> 29 <li><a href="https://developers.google.com/events/io/sessions/gooio2012/128/"> 30 Google I/O Session Video</a></li> 31 <li><a href="http://source.android.com/tech/accessories/aoap/aoa.html"> 32 Android Open Accessory Protocol</a></li> 33 <li><a href="http://source.android.com/tech/accessories/aoap/aoa2.html"> 34 Android Open Accessory Protocol 2.0</a></li> 35 <li><a href="{@docRoot}guide/topics/connectivity/usb/accessory.html"> 36 USB Accessory Dev Guide</a></li> 37 </ol> 38 </div> 39 </div> 40 41 <p>The Android Accessory Development Kit (ADK) for 2012 is the latest reference implementation of an 42 <a href="http://source.android.com/tech/accessories/index.html">Android Open Accessory</a> device, 43 designed to help Android hardware accessory builders and software developers create accessories 44 for Android. The ADK 2012 is based on the <a href="http://arduino.cc">Arduino</a> open source 45 electronics prototyping platform, with some hardware and software extensions that allow it to 46 communicate with Android devices.</p> 47 48 <p>A limited number of these kits were produced and distributed at the Google I/O 2012 developer 49 conference. If you did not receive one of these kits, fear not! The specifications and design files 50 for the hardware were also released for use by manufacturers and hobbyists. You should expect to see 51 kits with similar features available for purchase, or you can build one yourself!</p> 52 53 <p>One of the important new features demonstrated by this ADK is the ability to play audio over a 54 USB connection. Be sure to check out the <a href="#audio-dock">reference implementation</a> of a USB 55 audio dock in this ADK if you are interested in making audio-related USB accessories for 56 Android.</p> 57 58 <h2 id="components">Components</h2> 59 60 <p>The ADK 2012 is based on the Arduino open source electronics prototyping platform and is an open 61 hardware design. The hardware design files and firmware source code are included with the ADK 62 software download. The ADK contains two main physical hardware components:</p> 63 64 <ol> 65 <li>Main processing board containing the microprocessor, USB connections, power connector and 66 input/output pins. This board can be removed and used separately from the rest of the hardware.</li> 67 <li>Shield containing sensors, LEDs, input controls, audio amplifier and speaker output, contained 68 in a custom, polygon box enclosure.</li> 69 </ol> 70 71 <p>The main hardware features of the ADK are as follows:</p> 72 73 <ul> 74 <li>An ARM 32-bit Cortex M3 micro-processor</li> 75 <li>Separate USB connections for an Android device and computer connection for programming and 76 debugging</li> 77 <li>Sensors for light, color, proximity, temperature, humidity, barometric pressure, and 78 acceleration</li> 79 <li>Micro SD Card slot</li> 80 <li>Bluetooth support</li> 81 </ul> 82 83 <p>The ADK comes preloaded with an alarm clock firmware program that you can use immediately. A 84 companion Android application, <a 85 href="https://play.google.com/store/apps/details?id=com.google.android.apps.adk2">ADK 2012</a>, is 86 available on Google Play. The source code for both the Android application and the ADK firmware (an 87 Arduino sketch) can be downloaded from this page.</p> 88 89 <p>The ADK 2012 also comes with additional parts to help you develop accessories with it, 90 including:</p> 91 92 <ul> 93 <li>AC power adapter</li> 94 <li>USB A to Micro USB B connector cable</li> 95 <li>Micro USB B to Micro USB AB connector (small, rectangular plug)</li> 96 <li>Micro SD Card, preinstalled in the ADK SD Card socket</li> 97 </ul> 98 99 <h2 id="clock">Using the Alarm Clock</h2> 100 101 <p>An alarm clock program comes preloaded on the ADK. This firmware program allows you to use the 102 ADK as an alarm clock.</p> 103 104 <p>To use the ADK as an alarm clock:</p> 105 106 <ol> 107 <li>Open up the ADK by gently squeezing the two widest faces of the enclosure.</li> 108 <li>Attach the provided AC power adapter (round connector) to the main ADK board, or attach a USB 109 cable to the port marked <strong>Computer</strong> and a USB port on your computer.</li> 110 <li>Place and hold your fingertip over the clock symbol on the control face. 111 <p class="note"><strong>Note:</strong> You may need to hold your finger in place for 1-2 112 seconds.</p> 113 </li> 114 <li>Use the plus (+) and minus (-) symbols inside the clock digits to set the correct time.</li> 115 <li>Place your fingertip over the alarm clock symbol to activate alarm setting.</li> 116 <li>Use the plus (+) and minus (-) symbols inside the clock digits to set the alarm time.</li> 117 <li>Use the plus (+) and minus (-) symbols inside the last two clock digits to turn the alarm on 118 ({@code on}) or off ({@code oF}).</li> 119 <li>To set the alarm tone, place and hold your fingertip over the alarm clock symbol, then tap the 120 slider control on top of the enclosure.</li> 121 </ol> 122 123 <p>To use the <strong>ADK 2012</strong> companion application for the alarm clock:</p> 124 125 <ol> 126 <li>Load the companion application on your Android device running Android 3.1 (API Level 12) or 127 higher: 128 <ul> 129 <li>Using an NFC-enabled Android device, unlock and hold the device against the left side of 130 the ADK enclosure. Follow the prompts to install the app. 131 <br>- or -</li> 132 <li>Start Google Play on your device, search for the <strong>ADK 2012</strong> application and 133 install it. If you cannot find the app, your device is not compatible. Try installing on another 134 device.</li> 135 </ul> 136 </li> 137 <li>Connect your Android device to the ADK using one of the following methods: 138 <ul> 139 <li id="conn-bt">To connect using Bluetooth: 140 <ol> 141 <li>Plug the AC power adapter into the ADK.</li> 142 <li>On your Android device, turn Bluetooth On (<strong>Settings > 143 Bluetooth</strong>).</li> 144 <li>In the <strong>Settings</strong> page, press the <strong>Bluetooth</strong> option to 145 view paired devices.</li> 146 <li>Select <strong>Search for Devices</strong> to locate the ADK 2012 accessory and follow 147 the on screen instructions to connect.</li> 148 <li>After pairing is complete, start the <strong>ADK 2012</strong> app on your Android 149 device.</li> 150 <li>On the start screen, select the <strong>Use Bluetooth</strong> button.</li> 151 <li>In the list of paired devices, select the <strong>ADK 2012</strong> device.</li> 152 <li>The <strong>ADK 2012</strong> app should switch to the menu screen and you can start 153 interacting with the ADK accessory.</li> 154 </ol> 155 </li> 156 157 <li id="conn-usb">To connect using USB: 158 <ol> 159 <li>Plug the AC power adapter into the ADK.</li> 160 <li>Connect the Micro USB AB connector (small, rectangular plug) to the port labeled 161 <strong>Phone</strong> on the ADK board.</li> 162 <li>Unlock your Android device and connect the Micro USB B connector to your device. 163 <p class="note"><strong>Note:</strong> Your device must support Android USB accessory 164 mode. Devices that support this mode include Google Nexus devices.</p> 165 </li> 166 <li>On the Android device, you should see a prompt to connect to the <strong>ADK DemoKit 167 2012</strong>, select <strong>OK</strong>.</li> 168 <li>The <strong>ADK 2012</strong> app should start automatically and you can start 169 interacting with the ADK.</li> 170 </ol> 171 </li> 172 </ul> 173 </li> 174 </ol> 175 176 <p class="note"><strong>Note:</strong> When using the ADK with a USB connection to an Android 177 device, make sure the AC power adapter is plugged in. A USB connection to a computer does not 178 provide sufficient voltage to power both the ADK and a USB connection to a device.</p> 179 180 181 <h2 id="play-audio">Playing Audio</h2> 182 183 <p>The ADK 2012 comes with audio output capabilities, including an amplifier and speaker. You can 184 use it to play audio from your Android device using either a Bluetooth or USB connection.</p> 185 186 <p>To play audio over Bluetooth:</p> 187 188 <ol> 189 <li>Follow the instructions in the <a href="#conn-bt">previous section</a> to connect your Android 190 device to the ADK over Bluetooth.</li> 191 <li>On your Android device, navigate to the <strong>Settings > Bluetooth</strong> page to view the 192 list of paired devices, and make sure the ADK is connected.</li> 193 <li>Select the settings icon next to the <strong>ADK 2012</strong> item.</li> 194 <li>Make sure the <strong>Media Audio</strong> option is enabled.</li> 195 <li>Navigate to an application that plays music or other audio.</li> 196 <li>Play a song or sound and listen to it on the ADK speaker!</li> 197 </ol> 198 199 <p>To play audio over USB, you must use a device running Android 4.1 (API Level 16) or higher:</p> 200 201 <ol> 202 <li>Plug the AC power adapter into the ADK.</li> 203 <li>Connect the Micro USB AB connector (small, rectangular plug) to the Phone port on the ADK 204 board.</li> 205 <li>Unlock your Android device and connect the Micro USB B connector to your device. 206 <p class="note"><strong>Note:</strong> Your device must support Android USB accessory 207 mode. Devices that support this mode include Google Nexus devices.</p> 208 </li> 209 <li>On the Android device, you should see a prompt to connect to the <strong>ADK DemoKit 210 2012</strong>, select <strong>Cancel</strong> and allow the ADK to connect as a media device 211 only.</li> 212 <li>Navigate to an application that plays music or other audio.</li> 213 <li>Play a song or sound and listen to it on the ADK speaker!</li> 214 </ol> 215 216 <p class="note"><strong>Note:</strong> When using the ADK with a USB connection to an Android 217 device, make sure the AC power adapter is plugged in. A USB connection to a computer does not 218 provide sufficient voltage to power both the ADK and a USB connection to a device.</p> 219 220 221 <h2 id="dev">Developing Accessories with ADK 2012</h2> 222 223 <p>The ADK 2012 is a hardware platform and a set of software libraries for prototyping Android 224 accessories. This section discusses how to set up a development environment for programming the ADK 225 to make it do what you want and to test software for building your own Android accessories.</p> 226 227 <h3 id="src-download">Downloading the ADK Source</h3> 228 229 <p>The support software and hardware specifications for the ADK 2012 are available from the Android 230 source repository. Follow the instructions below to obtain the source material for the ADK.</p> 231 232 <p>To download the ADK 2012 software, source code and hardware design specifications.</p> 233 234 <ol> 235 <li>Download and install <a href="http://git-scm.com/download">Git</a> for your development 236 system.</li> 237 <li>Download and setup the {@code repo} tool, as described on the <a 238 href="http://source.android.com/source/downloading.html#installing-repo">Android open source 239 project</a> site. 240 <p class="note"><strong>Note:</strong> Developers using Windows must use a Linux compatibility 241 package, such as <a href="http://www.cygwin.com/">cygwin</a>, to install and run {@code repo}. 242 Within your compatibility environment, you must install {@code curl}, {@code git} and {@code 243 python} to be able to download and use the {@code repo} tool.</p> 244 </li> 245 <li>In a terminal window, create a new directory for the downloaded source files, initialize and 246 synchronize a local repository: 247 <pre> 248 $> mkdir android-accessories 249 $> cd android-accessories 250 $> repo init -u https://android.googlesource.com/accessories/manifest 251 $> repo sync 252 </pre> 253 </li> 254 </ol> 255 256 <p>After successfully completing this process, you should have the source code and tools for 257 working with the ADK 2012:</p> 258 259 <ul> 260 <li>{@code adk2012/board} - Source code and hardware design files for the ADK 2012</li> 261 <li>{@code adk2012/app} - Source code for the ADK 2012 Android companion application</li> 262 <li>{@code external/ide} - Source code for the ADK 2012 Integrated Development Environment 263 (IDE)</li> 264 <li>{@code external/toolchain} - The toolchain used by the ADK 2012 IDE</li> 265 </ul> 266 267 268 <h3 id="dev-setup">Setting Up the Development Environment</h3> 269 270 <p>The ADK 2012 comes with an integrated development environment (IDE) that you use to develop 271 software and program the ADK 2012 accessory. The following instructions explain how to setup and run 272 the ADK 2012 IDE.</p> 273 274 <p>To set up and run the ADK 2012 IDE:</p> 275 276 <ol> 277 <li>Download and install the Java Development Kit 6 or higher from <a 278 href="http://java.oracle.com">java.oracle.com</a>.</li> 279 <li>Download the ADK 2012 IDE for your development platform: 280 <ul> 281 <li><a href="https://dl-ssl.google.com/android/adk/adk2012_ide-win32-20120629.zip"> 282 Windows</a></li> 283 <li><a href="https://dl-ssl.google.com/android/adk/adk2012_ide-macosx-20120626.zip"> 284 Mac</a></li> 285 <li>Linux 286 <a href="https://dl-ssl.google.com/android/adk/adk2012_ide-linux32-20120626.tgz">32bit</a>, 287 <a href="https://dl-ssl.google.com/android/adk/adk2012_ide-linux64-20120626.tgz">64bit</a> 288 </li> 289 </ul> 290 </li> 291 <li>Unpack the downloaded archive.</li> 292 <li>Run the application by navigating to the unpacked {@code ADK2012_IDE/} folder and execute 293 the {@code arduino} file.</li> 294 </ol> 295 296 <p>After you have successfully installed and run the ADK 2012 IDE, you must configure it to use 297 the ADK 2012 library.</p> 298 299 <p>To configure the ADK 2012 IDE for use with the ADK libraries:</p> 300 301 <ol> 302 <li>Start the ADK 2012 IDE and choose <strong>File > Preferences</strong>.</li> 303 <li>In the <strong>Preferences</strong> dialog, make a note of the <strong>Sketchbook 304 location</strong> directory.</li> 305 <li>Copy the {@code <adk-source-download>/adk2012/board/library/ADK2} directory and its 306 contents into your {@code sketchbook/libraries/} directory, so that you create a {@code 307 sketchbook/libraries/ADK2} directory.</li> 308 <li>Stop and restart the <strong>ADK 2012 IDE</strong>.</li> 309 <li>In the IDE, choose <strong>File > Examples > ADK2</strong> and then choose one of the example 310 sketches: 311 <ul> 312 <li><strong>clock</strong> - The sketch that is preloaded onto the ADK 2012.</li> 313 <li><strong>BluetoothScan</strong> - An example sketch demonstrating code that connects an 314 accessory to an Android device over Bluetooth.</li> 315 <li><strong>usbaccessory</strong> - An example sketch demonstrating code that connects an 316 accessory to an Android device through a USB cable.</li> 317 </ul> 318 </li> 319 <li>Connect a Micro USB cable from the <strong>Computer</strong> port on the ADK board to your 320 development computer.</li> 321 <li>In the <strong>ADK 2012 IDE</strong>, establish a serial port connection with the ADK by 322 selecting <strong>Tools > Serial Port</strong> and selecting the serial port for the ADK.</li> 323 <li>In the <strong>ADK 2012 IDE</strong>, choose the ADK by selecting<strong>Tools > Board > 324 Google ADK2</strong>.</li> 325 <li>Modify an example sketch or create your own.</li> 326 <li>Upload the sketch to the ADK by choosing <strong>File > Upload</strong>. When the <strong>ADK 327 2012 IDE</strong> reports <strong>Done uploading</strong>, the sketch is uploaded and the ADK is 328 ready to communicate with your Android device.</li> 329 </ol> 330 331 <h3 id="alt-build">Using the ADK Alternative Build System</h3> 332 333 <p>An alternative, make file-based build and upload system is also available for the ADK 2012. This 334 system is command line based and intended for production environments where using an IDE environment 335 to load software onto accessory hardware may be inconvenient or undesirable.</p> 336 337 <p>To setup the environment:</p> 338 <ol> 339 <li><a href="#src-download">Download</a> the ADK 2012 source code files.</li> 340 <li>In a terminal window, navigate to {@code 341 <adk-source-download>/adk2012/board/MakefileBasedBuild}.</li> 342 <li>Execute the following command and follow the instructions: 343 <pre>$> ./setup</pre> 344 </li> 345 </ol> 346 347 <p>To build a program for your accessory:</p> 348 <ol> 349 <li>Place your accessory code in the {@code MakefileBasedBuild/app} directory, including all 350 required library files. See the {@code app/main.c} file for an example of the program format.</li> 351 <li>Execute the following command and follow the instructions: 352 <pre>$> ./build</pre> 353 </li> 354 </ol> 355 356 <p>To load the program on your accessory hardware:</p> 357 <ol> 358 <li>Run the build process above and make sure your program compiled successfully.</li> 359 <li>Attach the accessory via USB cable to your development computer.</li> 360 <li>Check which port the accessory is attached to and modify the {@code UART} variable in the 361 {@code flash} script to the correct port address. On linux machines, the port address is typically 362 {@code /dev/ttyUSB0}.</li> 363 <li>Execute the following command to load the program on the accessory: 364 <pre>$> ./flash</pre></li> 365 </ol> 366 367 <h2 id="adk-conn">How the ADK Connects with Android Devices</h2> 368 369 <p>The essential feature of any Android accessory is its ability to connect and communicate with an 370 Android device. Creating a fast and reliable connection between your accessory and Android devices 371 is the first order of business when building software for an accessory. This section describes the 372 connection and communication essentials used in the ADK 2012 so that you can apply them to 373 developing your own Android accessories.</p> 374 375 <h3 id="adk-conn-bt">ADK Connection over Bluetooth</h3> 376 377 <p>The ADK 2012 app and hardware accessory use a Bluetooth Serial Port Profile (SPP) connection to 378 communicate. This connection allows two way communication between the ADK accessory and Android 379 devices.</p> 380 381 <p class="note"><strong>Note:</strong> The implementation of the ADK hardware allows the use of 382 other profiles and multiple connections. However, the basic communication between the ADK 2012 383 accessory and the Android application uses SPP.</p> 384 385 <h4 id="acc-bt-code">Accessory Bluetooth Code</h4> 386 387 <p>In order to enable Bluetooth communications, the {@code clock.ino} sketch for the ADK 2012 388 accessory calls a {@code btStart()} method during the {@code setup()} method to enable radio 389 frequency communications and start listening for Bluetooth connections:</p> 390 391 <pre> 392 ADK L; 393 void setup() { 394 L.adkInit(); 395 L.btStart(); 396 } 397 ... 398 void btStart(){ 399 uint8_t i, dlci; 400 int f; 401 402 L.btEnable(adkBtConnectionRequest, adkBtLinkKeyRequest, adkBtLinkKeyCreated, 403 adkBtPinRequest, NULL); 404 405 dlci = L.btRfcommReserveDlci(RFCOMM_DLCI_NEED_EVEN); 406 407 if(!dlci) dbgPrintf("BTADK: failed to allocate DLCI\n"); 408 else{ 409 410 //change descriptor to be valid... 411 for(i = 0, f = -1; i < sizeof(sdpDescrADK); i++){ 412 413 if(sdpDescrADK[i] == MAGIX){ 414 if(f == -1) f = i; 415 else break; 416 } 417 } 418 419 if(i != sizeof(sdpDescrADK) || f == -1){ 420 421 dbgPrintf("BTADK: failed to find a single marker in descriptor\n"); 422 L.btRfcommReleaseDlci(dlci); 423 return; 424 } 425 426 sdpDescrADK[f] = dlci >> 1; 427 428 dbgPrintf("BTADK has DLCI %u\n", dlci); 429 430 L.btRfcommRegisterPort(dlci, btAdkPortOpen, btAdkPortClose, btAdkPortRx); 431 L.btSdpServiceDescriptorAdd(sdpDescrADK, sizeof(sdpDescrADK)); 432 } 433 } 434 </pre> 435 436 <p>Notice that the {@code sdpDescrADK} object contains a Universally Unique Identifier (UUID) in the 437 variable {@code BT_ADK_UUID}. This identifier <em>must match</em> the device UUID provided in the 438 {@link android.bluetooth.BluetoothSocket} connection request in the Android application code.</p> 439 440 <p>Once Bluetooth is enabled with the code shown above, the accessory listens for connection 441 requests. The ADK library handles listening and connection details, so the accessory calls 442 {@code ADK::adkEventProcess()} once during each loop execution:</p> 443 444 <pre> 445 void loop(void) 446 { 447 ... 448 L.adkEventProcess(); //let the adk framework do its thing 449 ... 450 } 451 </pre> 452 453 <p>If a Bluetooth connection has been established, any commands are routed to the 454 {@code btAdkPortRx()} callback method (which was registered with the ADK library as part of the 455 {@code btStart()} method) and processed accordingly. The ADK accessory sends messages back through 456 the Bluetooth connection using the {@code ADK::btRfcommPortTx()} method. 457 For more details, review the implementations of these methods in the {@code clock.ino} sketch.</p> 458 459 <h4 id="app-bt-code">Android App Bluetooth Code</h4> 460 461 <p>In the ADK 2012 Android app, the code for handling Bluetooth connections is encapsulated in in a 462 {@code BTConnection} class. In this class, the application requests access to the Bluetooth adapter 463 and negotiates a connection with the ADK 2012 accessory. Here is a summary of the relevant code:</p> 464 465 <pre> 466 mAdapter = BluetoothAdapter.getDefaultAdapter(); 467 BluetoothDevice device = mAdapter.getRemoteDevice(address); 468 mSocket = device.createInsecureRfcommSocketToServiceRecord(ADK_UUID); 469 mSocket.connect(); 470 </pre> 471 472 <p>Note the {@code ADK_UUID} parameter in the second line. This identifier must match the identifier 473 output by the accessory (the {@code BT_ADK_UUID} variable mentioned earlier), otherwise the protocol 474 negotiation fails and the {@link android.bluetooth.BluetoothSocket} is not created. Once a 475 connection is established, you obtain {@link java.io.InputStream} and {@link java.io.OutputStream} 476 objects from the socket to communicate with the accessory:</p> 477 478 <pre> 479 mInStream = mSocket.getInputStream(); 480 mOutStream = mSocket.getOutputStream(); 481 </pre> 482 483 <p>Review the {@code BTConnection.java} file provided in the ADK 2012 software download for more 484 implementation details.</p> 485 486 <h3 id="adk-conn-usb">ADK Connection over USB</h3> 487 488 <p>The ADK 2012 app and hardware accessory can also use a USB connection to communicate, similar to 489 the original ADK.</p> 490 491 <h4 id="acc-usb-code">Accessory USB Code</h4> 492 493 <p>The ADK library takes care of most of the implementation details for a USB connection, the 494 accessory code must make a few calls to initialize USB connectivity, including setting the accessory 495 identification strings:</p> 496 497 <pre> 498 ADK L; 499 void setup() { 500 L.adkInit(); 501 L.usbSetAccessoryStringVendor(...); 502 L.usbSetAccessoryStringName(...); 503 L.usbSetAccessoryStringLongname(...); 504 L.usbSetAccessoryStringVersion(...); 505 L.usbSetAccessoryStringUrl(...); 506 L.usbSetAccessoryStringSerial(...); 507 508 L.usbStart(); 509 } 510 </pre> 511 512 <p class="note"><strong>Note:</strong> The identification strings must match the USB accessory 513 filter settings specified in the connecting Android application,otherwise the application cannot 514 connect with the accessory.</p> 515 516 <p>Once USB is enabled with code shown above, the accessory listens for connection requests. The ADK 517 library handles listening and connection details, so the accessory calls {@code 518 ADK::adkEventProcess()} once during each loop execution:</p> 519 520 <pre> 521 void loop(void) 522 { 523 ... 524 L.adkEventProcess(); //let the adk framework do its thing 525 ... 526 } 527 </pre> 528 529 <p>The accessory must then check for a live USB connection to process commands and send 530 messages. Here is a summary of the relevant code:</p> 531 532 <pre> 533 void loop() { 534 if (L.accessoryConnected()) { 535 int recvLen = L.accessoryReceive(msg, sizeof(msg)); 536 if (recvLen > 0) { 537 ... // process message 538 } 539 540 L.accessorySend(outmsg, outmsgLen); 541 } 542 L.adkEventProcess(); 543 } 544 </pre> 545 546 <p>For more details, review the implementations of these methods in the {@code clock.ino} 547 sketch.</p> 548 549 <h4 id="app-usb-code">Android App USB Code</h4> 550 551 <p>In the ADK 2012 Android app, the code for handling USB connections is encapsulated in a 552 {@code UsbConnection} class. This class sets up a {@link android.content.BroadcastReceiver} to 553 listen for USB events and then attempts to connect when a matching connection event is received. 554 Here is a summary of the relevant code:</p> 555 556 <pre> 557 import com.android.future.usb.UsbAccessory; 558 import com.android.future.usb.UsbManager; 559 560 mUSBManager = UsbManager.getInstance(this); 561 UsbAccessory acc = mUSBManager.getAccessoryList()[0]; 562 563 if (!mUSBManager.hasPermission(acc)) return; 564 </pre> 565 566 <p>The ADK 2012 app uses the support library to implement the USB accessory connections, in order to 567 support devices running Android 2.3.4 (API Level 10). If you only need to support Android 568 3.1 (API Level 12) and higher devices, you can replace the first 4 lines the following code:</p> 569 570 <pre> 571 import android.hardware.usb.UsbAccessory 572 import android.hardware.usb.UsbManager 573 574 mUSBManager = (UsbManager) getSystemService(Context.USB_SERVICE); 575 UsbAccessory acc = (UsbAccessory) 576 intent.getParcelableExtra(UsbManager.EXTRA_ACCESSORY); 577 </pre> 578 579 <p>Note that the app only receives events when the USB accessory identification information matches 580 the information in the {@code res/xml/usb_accessory_filter.xml} file, referenced by the 581 applications manifest statement:</p> 582 583 <pre> 584 <meta-data 585 android:name="android.hardware.usb.action.USB_ACCESSORY_ATTACHED" 586 android:resource="@xml/usb_accessory_filter" /> 587 </pre> 588 589 <p>Connections from other USB devices are not received by the ADK 2012 accessory.</p> 590 591 <p>Once the connection is established, the app can communicate with the accessory through file input 592 and output streams, as shown in the following example code:</p> 593 594 <pre> 595 ParcelFileDescriptor mFD = mUSBManager.openAccessory(acc); 596 if (mFD != null) { 597 FileDescripter fd = mFD.getFileDescriptor(); 598 mIS = new FileInputStream(fd); // use this to receive messages 599 mOS = new FileOutputStream(fd); // use this to send commands 600 } 601 </pre> 602 603 <p>Review the {@code UsbConnection.java} file provided in the ADK 2012 source code for more 604 implementation details.</p> 605 606 <h2 id="audio-dock">USB Audio Dock Implementation</h2> 607 608 <p>One of the important new features introduced with the ADK 2012 is the ability to play audio over 609 a USB connection. This innovation was introduced as an update to Android Open Accessory (AOA) 610 <a href="http://source.android.com/tech/accessories/aoap/aoa2.html">protocol 2.0</a> and is 611 available on devices running Android 4.1 (API Level 16) and higher.</p> 612 613 <p>The ADK 2012 provides a reference implementation of this functionality for accessory developers. 614 No software application is required to be installed on the connected Android device, accessory 615 developers only need to support AOA v2. This implementation demonstrates audio output of 16bit, 616 44.1kHz stereo PCM source data compressed into a single channel due to the audio hardware available 617 on the accessory.</p> 618 619 <p>Using the audio output features provided by the ADK library requires only a few function calls. 620 The first few calls are in the accessory {@code setup()} routine, which prepare the accessory for 621 USB connections and audio output, as summarized in the code example below:</p> 622 623 <pre> 624 ADK L; 625 void setup() { 626 L.audioInit(); 627 L.usbh_init() 628 L.usbStart(); 629 } 630 </pre> 631 632 <p>For more information about the {@code ADK::audioInit()} function, see the {@code 633 libraries/ADK/Audio.c} library file. For more information about the {@code ADK::usbh_init()} 634 function, see the {@code libraries/ADK/Usbh.c} library file.</p> 635 636 <p>After completing this setup, the {@code loop()} function calls {@code ADK::adkEventProcess()} to 637 handle audio output and other ADK functions:</p> 638 639 <pre> 640 void loop(void) 641 { 642 ... 643 L.adkEventProcess(); //let the adk framework do its thing 644 ... 645 } 646 </pre> 647 648 <p>This call executes task queuing for the ADK and as part of the execution process, the task queue 649 executes {@code usbh_work()} in {@code libraries/ADK/Usbh.c}, which handles audio output requests. 650 Review the implementation of this function for details. For additional implementation details on 651 audio output, see the {@code libraries/ADK/accessory.c} library file.</p> 652