1page.title=Android Open Accessory Protocol 1.0
2@jd:body
3
4<!--
5    Copyright 2013 The Android Open Source Project
6
7    Licensed under the Apache License, Version 2.0 (the "License");
8    you may not use this file except in compliance with the License.
9    You may obtain a copy of the License at
10
11        http://www.apache.org/licenses/LICENSE-2.0
12
13    Unless required by applicable law or agreed to in writing, software
14    distributed under the License is distributed on an "AS IS" BASIS,
15    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16    See the License for the specific language governing permissions and
17    limitations under the License.
18-->
19<p>An Android USB accessory must adhere to Android Accessory Protocol, which defines how
20an accessory detects and sets up communication with an Android-powered device. In general, an
21accessory should carry out the following steps:</p>
22<ul>
23<li>Wait for and detect connected devices</li>
24<li>Determine the device's accessory mode support</li>
25<li>Attempt to start the device in accessory mode if needed</li>
26<li>Establish communication with the device if it supports the Android accessory protocol</li>
27</ul>
28<p>The following sections explain how to implement these steps.</p>
29<h2 id="wait-for-and-detect-connected-devices">Wait for and Detect Connected Devices</h2>
30<p>Your accessory should have logic to continuously check for connected Android-powered devices.
31When a device is connected, your accessory should determine if the device supports accessory mode.</p>
32<h2 id="determine-accessory-mode-support">Determine Accessory Mode Support</h2>
33<p>When an Android-powered device is connected, it can be in one of three states:</p>
34<ul>
35<li>The attached device supports Android accessory mode and is already in accessory mode.</li>
36<li>The attached device supports Android accessory mode, but it is not in accessory mode.</li>
37<li>The attached device does not support Android accessory mode.</li>
38</ul>
39<p>During the initial connection, the accessory should check the vendor and product IDs of the
40connected device's USB device descriptor. The vendor ID should match Google's ID (<code>0x18D1</code>) and the
41product ID should be <code>0x2D00</code> or <code>0x2D01</code> if the device is already in accessory mode (case A). If
42so, the accessory can now
43<a href="#establish-communication-with-the-device">establish communication with the device</a> through
44bulk transfer endpoints with its own communication protocol. There is no need to start the device
45in accessory mode.</p>
46<p><strong>Note:</strong> <code>0x2D00</code> is reserved for Android-powered devices that
47support accessory mode. <code>0x2D01</code> is reserved for devices that support accessory mode as well as the
48ADB (Android Debug Bridge) protocol, which exposes a second interface with two bulk endpoints for
49ADB. You can use these endpoints for debugging the accessory application if you are simulating
50the accessory on a computer. In general, do not use this interface unless your accessory is
51implementing a passthrough to ADB on the device.</p>
52<p>If the vendor and product ID do not match, there is no way to distinguish between states b and c, so
53the accessory <a href="#attempt-to-start-in-accessory-mode">attempts to start the device in accessory mode</a>
54to determine if the device is supported.</p>
55<h2 id="attempt-to-start-in-accessory-mode">Attempt to Start in Accessory Mode</h2>
56<p>If the vendor and product IDs do not correspond to an Android-powered device in accessory
57mode, the accessory cannot discern whether the device supports accessory mode and is not in that
58state, or if the device does not support accessory mode at all. This is because devices that
59support accessory mode but aren't in it initially report the device's manufacturer vendor ID and
60product ID, and not the special Android Open Accessory ones. In either case, the accessory should
61try to start the device into accessory mode to figure out if the device supports it. The following
62steps explain how to do this:</p>
63<ul>
64  <li>Send a 51 control request ("Get Protocol") to figure out if the device supports the Android
65  accessory protocol. A non-zero number is returned if the protocol is supported, which
66  represents the version of the protocol that the device supports (currently, only version 1
67  exists). This request is a control request on endpoint 0 with the following characteristics:
68
69<pre>
70requestType:    USB_DIR_IN | USB_TYPE_VENDOR
71request:        51
72value:          0
73index:          0
74data:           protocol version number (16 bits little endian sent from the
75                device to the accessory)
76</pre>
77  </li>
78  <li>If the device returns a proper protocol version, send identifying string information to the
79  device. This information allows the device to figure out an appropriate application for this
80  accessory and also present the user with a URL if an appropriate application does not exist.
81  These requests are control requests on endpoint 0 (for each string ID) with the following
82  characteristics:
83
84<pre>
85requestType:    USB_DIR_OUT | USB_TYPE_VENDOR
86request:        52
87value:          0
88index:          string ID
89data            zero terminated UTF8 string sent from accessory to device
90</pre>
91
92  <p>The following string IDs are supported, with a maximum size of 256 bytes for each string
93  (must be zero terminated with `\0`).</p>
94
95<pre>
96manufacturer name:  0
97model name:         1
98description:        2
99version:            3
100URI:                4
101serial number:      5
102</pre>
103  </li>
104  <li>When the identifying strings are sent, request the device start up in accessory mode. This
105  request is a control request on endpoint 0 with the following characteristics:
106
107<pre>
108requestType:    USB_DIR_OUT | USB_TYPE_VENDOR
109request:        53
110value:          0
111index:          0
112data:           none
113</pre>
114  </li>
115</ul>
116
117<p>After sending the final control request, the connected USB device should re-introduce itself
118on the bus in accessory mode and the accessory can re-enumerate the connected devices. The
119algorithm jumps back to
120<a href="#determine-accessory-mode-support">determining the device's accessory mode support</a>
121to check for the vendor and product ID. The vendor ID and product ID of the device will be
122different if the device successfully switched to accessory mode and will now correspond to
123Google's vendor and product IDs instead of the device manufacturer's IDs. The accessory can now
124<a href="#establish-communication-with-the-device">establish communication with the device</a>.</p>
125<p>If at any point these steps fail, the device does not support Android accessory mode and the
126accessory should wait for the next device to be connected.</p>
127<h2 id="establish-communication-with-the-device">Establish Communication with the Device</h2>
128<p>If an Android-powered device in accessory mode is detected, the accessory can query the
129device's interface and endpoint descriptors to obtain the bulk endpoints to communicate with the
130device. An Android-powered device that has a product ID of <code>0x2D00</code> has one interface with two bulk
131endpoints for input and output communication. A device with product ID of <code>0x2D01</code> has two
132interfaces with two bulk endpoints each for input and output communication. The first interface
133is for standard communication while the second interface is for ADB communication. To communicate
134on an interface, all you need to do is find the first bulk input and output endpoints, set the
135device's configuration to a value of 1 with a <code>SET_CONFIGURATION</code> (<code>0x09</code>) device request, then
136communicate using the endpoints.</p>
137
138
139