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