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