135f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lypage.title=Android Open Accessory Protocol 1.0
235f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly@jd:body
335f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly
435f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<!--
5768b82a9dfbdd8504eae2736d283a60a37c7a547Clay Murphy    Copyright 2013 The Android Open Source Project
635f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly
735f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly    Licensed under the Apache License, Version 2.0 (the "License");
835f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly    you may not use this file except in compliance with the License.
935f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly    You may obtain a copy of the License at
1035f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly
1135f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly        http://www.apache.org/licenses/LICENSE-2.0
1235f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly
1335f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly    Unless required by applicable law or agreed to in writing, software
1435f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly    distributed under the License is distributed on an "AS IS" BASIS,
1535f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1635f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly    See the License for the specific language governing permissions and
1735f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly    limitations under the License.
1835f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly-->
1935f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<p>An Android USB accessory must adhere to Android Accessory Protocol, which defines how
2035f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyan accessory detects and sets up communication with an Android-powered device. In general, an
2135f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyaccessory should carry out the following steps:</p>
2235f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<ul>
2335f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<li>Wait for and detect connected devices</li>
2435f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<li>Determine the device's accessory mode support</li>
2535f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<li>Attempt to start the device in accessory mode if needed</li>
2635f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<li>Establish communication with the device if it supports the Android accessory protocol</li>
2735f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly</ul>
2835f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<p>The following sections explain how to implement these steps.</p>
2935f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<h2 id="wait-for-and-detect-connected-devices">Wait for and Detect Connected Devices</h2>
3035f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<p>Your accessory should have logic to continuously check for connected Android-powered devices.
3135f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert LyWhen a device is connected, your accessory should determine if the device supports accessory mode.</p>
3235f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<h2 id="determine-accessory-mode-support">Determine Accessory Mode Support</h2>
3335f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<p>When an Android-powered device is connected, it can be in one of three states:</p>
3435f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<ul>
3535f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<li>The attached device supports Android accessory mode and is already in accessory mode.</li>
3635f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<li>The attached device supports Android accessory mode, but it is not in accessory mode.</li>
3735f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<li>The attached device does not support Android accessory mode.</li>
3835f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly</ul>
3935f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<p>During the initial connection, the accessory should check the vendor and product IDs of the
4035f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyconnected device's USB device descriptor. The vendor ID should match Google's ID (<code>0x18D1</code>) and the
4135f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyproduct ID should be <code>0x2D00</code> or <code>0x2D01</code> if the device is already in accessory mode (case A). If
4235f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyso, the accessory can now
4335f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<a href="#establish-communication-with-the-device">establish communication with the device</a> through
4435f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lybulk transfer endpoints with its own communication protocol. There is no need to start the device
4535f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyin accessory mode.</p>
4635f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<p><strong>Note:</strong> <code>0x2D00</code> is reserved for Android-powered devices that
4735f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lysupport accessory mode. <code>0x2D01</code> is reserved for devices that support accessory mode as well as the
4835f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert LyADB (Android Debug Bridge) protocol, which exposes a second interface with two bulk endpoints for
4935f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert LyADB. You can use these endpoints for debugging the accessory application if you are simulating
5035f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lythe accessory on a computer. In general, do not use this interface unless your accessory is
5135f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyimplementing a passthrough to ADB on the device.</p>
5235f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<p>If the vendor and product ID do not match, there is no way to distinguish between states b and c, so
5335f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lythe accessory <a href="#attempt-to-start-in-accessory-mode">attempts to start the device in accessory mode</a>
5435f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyto determine if the device is supported.</p>
5535f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<h2 id="attempt-to-start-in-accessory-mode">Attempt to Start in Accessory Mode</h2>
5635f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<p>If the vendor and product IDs do not correspond to an Android-powered device in accessory
5735f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lymode, the accessory cannot discern whether the device supports accessory mode and is not in that
5835f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lystate, or if the device does not support accessory mode at all. This is because devices that
5935f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lysupport accessory mode but aren't in it initially report the device's manufacturer vendor ID and
6035f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyproduct ID, and not the special Android Open Accessory ones. In either case, the accessory should
6135f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lytry to start the device into accessory mode to figure out if the device supports it. The following
6235f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lysteps explain how to do this:</p>
6335f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<ul>
6435f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly  <li>Send a 51 control request ("Get Protocol") to figure out if the device supports the Android
6535f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly  accessory protocol. A non-zero number is returned if the protocol is supported, which
6635f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly  represents the version of the protocol that the device supports (currently, only version 1
6735f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly  exists). This request is a control request on endpoint 0 with the following characteristics:
6835f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly
6935f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<pre>
7035f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert LyrequestType:    USB_DIR_IN | USB_TYPE_VENDOR
7135f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyrequest:        51
7235f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyvalue:          0
7335f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyindex:          0
7435f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lydata:           protocol version number (16 bits little endian sent from the
7535f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly                device to the accessory)
7635f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly</pre>
7735f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly  </li>
7835f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly  <li>If the device returns a proper protocol version, send identifying string information to the
7935f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly  device. This information allows the device to figure out an appropriate application for this
8035f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly  accessory and also present the user with a URL if an appropriate application does not exist.
8135f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly  These requests are control requests on endpoint 0 (for each string ID) with the following
8235f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly  characteristics:
8335f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly
8435f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<pre>
8535f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert LyrequestType:    USB_DIR_OUT | USB_TYPE_VENDOR
8635f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyrequest:        52
8735f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyvalue:          0
8835f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyindex:          string ID
8935f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lydata            zero terminated UTF8 string sent from accessory to device
9035f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly</pre>
9135f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly
9235f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly  <p>The following string IDs are supported, with a maximum size of 256 bytes for each string
9335f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly  (must be zero terminated with `\0`).</p>
9435f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly
9535f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<pre>
9635f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lymanufacturer name:  0
9735f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lymodel name:         1
9835f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lydescription:        2
9935f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyversion:            3
10035f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert LyURI:                4
10135f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyserial number:      5
10235f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly</pre>
10335f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly  </li>
10435f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly  <li>When the identifying strings are sent, request the device start up in accessory mode. This
10535f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly  request is a control request on endpoint 0 with the following characteristics:
10635f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly
10735f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<pre>
10835f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert LyrequestType:    USB_DIR_OUT | USB_TYPE_VENDOR
10935f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyrequest:        53
11035f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyvalue:          0
11135f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyindex:          0
11235f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lydata:           none
11335f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly</pre>
11435f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly  </li>
11535f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly</ul>
11635f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly
11735f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<p>After sending the final control request, the connected USB device should re-introduce itself
11835f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyon the bus in accessory mode and the accessory can re-enumerate the connected devices. The
11935f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyalgorithm jumps back to
12035f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<a href="#determine-accessory-mode-support">determining the device's accessory mode support</a>
12135f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyto check for the vendor and product ID. The vendor ID and product ID of the device will be
12235f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lydifferent if the device successfully switched to accessory mode and will now correspond to
12335f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert LyGoogle's vendor and product IDs instead of the device manufacturer's IDs. The accessory can now
12435f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<a href="#establish-communication-with-the-device">establish communication with the device</a>.</p>
12535f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<p>If at any point these steps fail, the device does not support Android accessory mode and the
12635f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyaccessory should wait for the next device to be connected.</p>
12735f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<h2 id="establish-communication-with-the-device">Establish Communication with the Device</h2>
12835f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly<p>If an Android-powered device in accessory mode is detected, the accessory can query the
12935f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lydevice's interface and endpoint descriptors to obtain the bulk endpoints to communicate with the
13035f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lydevice. An Android-powered device that has a product ID of <code>0x2D00</code> has one interface with two bulk
13135f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyendpoints for input and output communication. A device with product ID of <code>0x2D01</code> has two
13235f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyinterfaces with two bulk endpoints each for input and output communication. The first interface
13335f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyis for standard communication while the second interface is for ADB communication. To communicate
13435f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lyon an interface, all you need to do is find the first bulk input and output endpoints, set the
13535f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lydevice's configuration to a value of 1 with a <code>SET_CONFIGURATION</code> (<code>0x09</code>) device request, then
13635f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Lycommunicate using the endpoints.</p>
13735f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly
13835f2fda6aaeaf733ab68a3b7f7ccc67f009c09a9Robert Ly
139