blob: d5be8ab3bb492a25c1142fdf2ac248babecea1a0 [file] [log] [blame]
Joe Fernandez3a77ccc2012-06-24 23:58:25 -07001page.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>
Joe Fernandezd2d6c092012-07-11 15:26:09 -070029 <li><a href="https://developers.google.com/events/io/sessions/gooio2012/128/">
30 Google I/O Session Video</a></li>
Joe Fernandez3a77ccc2012-06-24 23:58:25 -070031 <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>
Joe Fernandezd2d6c092012-07-11 15:26:09 -070033 <li><a href="{@docRoot}guide/topics/connectivity/usb/accessory.html">
34 USB Accessory Dev Guide</a></li>
Joe Fernandez3a77ccc2012-06-24 23:58:25 -070035 </ol>
36 </div>
37</div>
38
39<p>The Android Accessory Development Kit (ADK) for 2012 is the latest reference implementation of
40an <a href="aoa.html">Android Open Accessory</a> device, designed to help Android hardware accessory
41builders and software developers create accessories for Android. The ADK 2012 is based on the <a
42href="http://arduino.cc">Arduino</a> open source electronics prototyping platform, with some
43hardware 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
46conference. If you did not receive one of these kits, fear not! The specifications and design files
47for the hardware were also released for use by manufacturers and hobbyists. You should expect to see
48kits 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
51USB connection. Be sure to check out the <a href="#audio-dock">reference implementation</a> of a USB
52audio dock in this ADK if you are interested in making audio-related USB accessories for
53Android.</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
58hardware design. The hardware design files and firmware source code are included with the ADK
59software 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
63input/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
65in 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
73debugging</li>
74 <li>Sensors for light, color, proximity, temperature, humidity, barometric pressure, and
75acceleration</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
81companion Android application, <a
82href="https://play.google.com/store/apps/details?id=com.google.android.apps.adk2">ADK 2012</a>, is
83available on Google Play. The source code for both the Android application and the ADK firmware (an
84Arduino 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,
87including:</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
99ADK 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
106cable 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
109seconds.</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
117slider 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
124higher:
125 <ul>
126 <li>Using an NFC-enabled Android device, unlock and hold the device against the left side of
127the 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
130install it. If you cannot find the app, your device is not compatible. Try installing on another
131device.</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 >
140Bluetooth</strong>).</li>
141 <li>In the <strong>Settings</strong> page, press the <strong>Bluetooth</strong> option to
142view paired devices.</li>
143 <li>Select <strong>Search for Devices</strong> to locate the ADK 2012 accessory and follow
144the on screen instructions to connect.</li>
145 <li>After pairing is complete, start the <strong>ADK 2012</strong> app on your Android
146device.</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
150interacting 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
161mode. 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
1642012</strong>, select <strong>OK</strong>.</li>
165 <li>The <strong>ADK 2012</strong> app should start automatically and you can start
166interacting 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
174device, make sure the AC power adapter is plugged in. A USB connection to a computer does not
175provide 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
181use 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
187device to the ADK over Bluetooth.</li>
188 <li>On your Android device, navigate to the <strong>Settings > Bluetooth</strong> page to view the
189list 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
201board.</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
204mode. 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
2072012</strong>, select <strong>Cancel</strong> and allow the ADK to connect as a media device
208only.</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
214device, make sure the AC power adapter is plugged in. A USB connection to a computer does not
215provide 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
221accessories. This section discusses how to set up a development environment for programming the ADK
222to 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
227source 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
233system.</li>
234 <li>Download and setup the {@code repo} tool, as described on the <a
235href="http://source.android.com/source/downloading.html#installing-repo">Android open source
Joe Fernandezd2d6c092012-07-11 15:26:09 -0700236project</a> site.
237 <p class="note"><strong>Note:</strong> Developers using Windows must use a Linux compatibility
238package, such as <a href="http://www.cygwin.com/">cygwin</a>, to install and run {@code repo}.
239Within your compatibility environment, you must install {@code curl}, {@code git} and {@code
240python} to be able to download and use the {@code repo} tool.</p>
241 </li>
Joe Fernandez3a77ccc2012-06-24 23:58:25 -0700242 <li>In a terminal window, create a new directory for the downloaded source files, initialize and
243synchronize a local repository:
244<pre>
245$> mkdir android-accessories
246$> cd android-accessories
Joe Fernandez79dee592012-06-29 14:39:00 -0700247$> repo init -u https://android.googlesource.com/accessories/manifest
Joe Fernandez3a77ccc2012-06-24 23:58:25 -0700248$> 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
254working 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
Joe Fernandezcaa008f2012-06-27 23:33:20 -0700267<p>The ADK 2012 comes with an integrated development environment (IDE) that you use to develop
268software and program the ADK 2012 accessory. The following instructions explain how to setup and run
269the ADK 2012 IDE.</p>
Joe Fernandez3a77ccc2012-06-24 23:58:25 -0700270
Joe Fernandezcaa008f2012-06-27 23:33:20 -0700271<p>To set up and run the ADK 2012 IDE:</p>
Joe Fernandez3a77ccc2012-06-24 23:58:25 -0700272
273<ol>
Joe Fernandez3a77ccc2012-06-24 23:58:25 -0700274 <li>Download and install the Java Development Kit 6 or higher from <a
Joe Fernandezcaa008f2012-06-27 23:33:20 -0700275 href="http://java.oracle.com">java.oracle.com</a>.</li>
276 <li>Download the ADK 2012 IDE for your development platform:
277 <ul>
Joe Fernandezd2d6c092012-07-11 15:26:09 -0700278 <li><a href="https://dl-ssl.google.com/android/adk/adk2012_ide-win32-20120629.zip">
Joe Fernandezcaa008f2012-06-27 23:33:20 -0700279 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>
Joe Fernandez3a77ccc2012-06-24 23:58:25 -0700287 </li>
Joe Fernandezcaa008f2012-06-27 23:33:20 -0700288 <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>
Joe Fernandez3a77ccc2012-06-24 23:58:25 -0700291</ol>
292
Joe Fernandezcaa008f2012-06-27 23:33:20 -0700293<p>After you have successfully installed and run the ADK 2012 IDE, you must configure it to use
294the ADK 2012 library.</p>
Joe Fernandez3a77ccc2012-06-24 23:58:25 -0700295
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
301location</strong> directory.</li>
302 <li>Copy the {@code &lt;adk-source-download&gt;/adk2012/board/library/ADK2} directory and its
303contents into your {@code sketchbook/libraries/} directory, so that you create a {@code
304sketchbook/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
307sketches:
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
311accessory to an Android device over Bluetooth.</li>
312 <li><strong>usbaccessory</strong> - An example sketch demonstrating code that connects an
313accessory 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
317development computer.</li>
318 <li>In the <strong>ADK 2012 IDE</strong>, establish a serial port connection with the ADK by
319selecting <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 >
321Google 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
3242012 IDE</strong> reports <strong>Done uploading</strong>, the sketch is uploaded and the ADK is
325ready to communicate with your Android device.</li>
326</ol>
327
328<h3 id="alt-build">Using the ADK Alternative Build System</h3>
329
Joe Fernandezd9435b32012-06-26 23:32:03 -0700330<p>An alternative, make file-based build and upload system is also available for the ADK 2012. This
331system is command line based and intended for production environments where using an IDE environment
332to load software onto accessory hardware may be inconvenient or undesirable.</p>
Joe Fernandez3a77ccc2012-06-24 23:58:25 -0700333
Joe Fernandezd9435b32012-06-26 23:32:03 -0700334<p>To setup the environment:</p>
Joe Fernandez3a77ccc2012-06-24 23:58:25 -0700335<ol>
336 <li><a href="#src-download">Download</a> the ADK 2012 source code files.</li>
Joe Fernandezd9435b32012-06-26 23:32:03 -0700337 <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
347required 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>
Joe Fernandez3a77ccc2012-06-24 23:58:25 -0700362</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
367Android device. Creating a fast and reliable connection between your accessory and Android devices
368is the first order of business when building software for an accessory. This section describes the
369connection and communication essentials used in the ADK 2012 so that you can apply them to
370developing 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
375communicate. This connection allows two way communication between the ADK accessory and Android
376devices.</p>
377
378<p class="note"><strong>Note:</strong> The implementation of the ADK hardware allows the use of
379other profiles and multiple connections. However, the basic communication between the ADK 2012
380accessory 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
385accessory calls a {@code btStart()} method during the {@code setup()} method to enable radio
386frequency communications and start listening for Bluetooth connections:</p>
387
388<pre>
389ADK L;
390void setup() {
391 L.adkInit();
392 L.btStart();
393}
394...
395void 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
434variable {@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
438requests. 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>
442void 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
453the Bluetooth connection using the {@code ADK::btRfcommPortTx()} method.
454For 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
460and negotiates a connection with the ADK 2012 accessory. Here is a summary of the relevant code:</p>
461
462<pre>
463mAdapter = BluetoothAdapter.getDefaultAdapter();
464BluetoothDevice device = mAdapter.getRemoteDevice(address);
465mSocket = device.createInsecureRfcommSocketToServiceRecord(ADK_UUID);
466mSocket.connect();
467</pre>
468
469<p>Note the {@code ADK_UUID} parameter in the second line. This identifier must match the identifier
470output by the accessory (the {@code BT_ADK_UUID} variable mentioned earlier), otherwise the protocol
471negotiation fails and the {@link android.bluetooth.BluetoothSocket} is not created. Once a
472connection is established, you obtain {@link java.io.InputStream} and {@link java.io.OutputStream}
473objects from the socket to communicate with the accessory:</p>
474
475<pre>
476mInStream = mSocket.getInputStream();
477mOutStream = mSocket.getOutputStream();
478</pre>
479
480<p>Review the {@code BTConnection.java} file provided in the ADK 2012 software download for more
481implementation 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
486the 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
491accessory code must make a few calls to initialize USB connectivity, including setting the accessory
492identification strings:</p>
493
494<pre>
495ADK L;
496void 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
510filter settings specified in the connecting Android application,otherwise the application cannot
511connect with the accessory.</p>
512
513<p>Once USB is enabled with code shown above, the accessory listens for connection requests. The ADK
514library handles listening and connection details, so the accessory calls {@code
515ADK::adkEventProcess()} once during each loop execution:</p>
516
517<pre>
518void 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
527messages. Here is a summary of the relevant code:</p>
528
529<pre>
530void 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}
544sketch.</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
550listen for USB events and then attempts to connect when a matching connection event is received.
551Here is a summary of the relevant code:</p>
552
553<pre>
554import com.android.future.usb.UsbAccessory;
555import com.android.future.usb.UsbManager;
556
557mUSBManager = UsbManager.getInstance(this);
558UsbAccessory acc = mUSBManager.getAccessoryList()[0];
559
560if (!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
564support devices running Android 2.3.4 (API Level 10). If you only need to support Android
5653.1 (API Level 12) and higher devices, you can replace the first 4 lines the following code:</p>
566
567<pre>
568import android.hardware.usb.UsbAccessory
569import android.hardware.usb.UsbManager
570
571mUSBManager = (UsbManager) getSystemService(Context.USB_SERVICE);
572UsbAccessory 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
577the information in the {@code res/xml/usb_accessory_filter.xml} file, referenced by the
578applications 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
589and output streams, as shown in the following example code:</p>
590
591<pre>
592ParcelFileDescriptor mFD = mUSBManager.openAccessory(acc);
593if (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
601implementation 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
606a 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)
608and higher.</p>
609
610<p>The ADK 2012 provides a reference implementation of this functionality for accessory developers.
611No software application is required to be installed on the connected Android device, accessory
612developers only need to support AOA v2. This implementation demonstrates audio output of 16bit,
61344.1kHz stereo PCM source data compressed into a single channel due to the audio hardware available
614on the accessory.</p>
615
616<p>Using the audio output features provided by the ADK library requires only a few function calls.
617The first few calls are in the accessory {@code setup()} routine, which prepare the accessory for
618USB connections and audio output, as summarized in the code example below:</p>
619
620<pre>
621ADK L;
622void 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
630libraries/ADK/Audio.c} library file. For more information about the {@code ADK::usbh_init()}
631function, 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
634handle audio output and other ADK functions:</p>
635
636<pre>
637void 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
646executes {@code usbh_work()} in {@code libraries/ADK/Usbh.c}, which handles audio output requests.
647Review the implementation of this function for details. For additional implementation details on
648audio output, see the {@code libraries/ADK/accessory.c} library file.</p>