From 3a77ccc63f11b26067f03ef61a713ff2599fbb5f Mon Sep 17 00:00:00 2001 From: Joe Fernandez Date: Sun, 24 Jun 2012 23:58:25 -0700 Subject: docs: ADK 2012 documentation with AOA protocol docs Change-Id: Ife96b3ffa890d42fc97125b30bdd389448645562 --- docs/html/guide/guide_toc.cs | 1 - docs/html/guide/topics/connectivity/usb/adk.jd | 909 ------------------------- docs/html/tools/adk/adk.jd | 709 +++++++++++++++++++ docs/html/tools/adk/adk2.jd | 617 +++++++++++++++++ docs/html/tools/adk/aoa.jd | 186 +++++ docs/html/tools/adk/aoa2.jd | 227 ++++++ docs/html/tools/adk/index.jd | 34 + docs/html/tools/tools_toc.cs | 20 +- 8 files changed, 1792 insertions(+), 911 deletions(-) delete mode 100644 docs/html/guide/topics/connectivity/usb/adk.jd create mode 100644 docs/html/tools/adk/adk.jd create mode 100644 docs/html/tools/adk/adk2.jd create mode 100644 docs/html/tools/adk/aoa.jd create mode 100644 docs/html/tools/adk/aoa2.jd create mode 100644 docs/html/tools/adk/index.jd (limited to 'docs/html') diff --git a/docs/html/guide/guide_toc.cs b/docs/html/guide/guide_toc.cs index 44b977e327dd..0b677c9ed666 100644 --- a/docs/html/guide/guide_toc.cs +++ b/docs/html/guide/guide_toc.cs @@ -407,7 +407,6 @@
  • diff --git a/docs/html/guide/topics/connectivity/usb/adk.jd b/docs/html/guide/topics/connectivity/usb/adk.jd deleted file mode 100644 index 034728cf9e2c..000000000000 --- a/docs/html/guide/topics/connectivity/usb/adk.jd +++ /dev/null @@ -1,909 +0,0 @@ -page.title=Android Open Accessory Development Kit -@jd:body - - - -

    The Android 3.1 platform (also backported to Android 2.3.4) introduces Android Open Accessory - support, which allows external USB hardware (an Android USB accessory) to interact with an - Android-powered device in a special "accessory" mode. When an Android-powered powered device is - in accessory mode, the connected accessory acts as the USB host (powers the bus and enumerates - devices) and the Android-powered device acts as the USB device. Android USB accessories are - specifically designed to attach to Android-powered devices and adhere to a simple protocol - (Android accessory protocol) that allows them to detect Android-powered devices that support - accessory mode. Accessories must also provide 500mA at 5V for charging power. Many previously - released Android-powered devices are only capable of acting as a USB device and cannot initiate - connections with external USB devices. Android Open Accessory support overcomes this limitation - and allows you to build accessories that can interact with an assortment of Android-powered - devices by allowing the accessory to initiate the connection.

    - -

    Note: Accessory mode is ultimately dependent on the device's - hardware and not all devices will support accessory mode. Devices that support accessory mode can - be filtered using a <uses-feature> element in your corresponding application's - Android manifest. For more information, see the USB Accessory Developer Guide.

    - -

    The following list of distributers are currently producing Android Open Accessory compatible - development boards:

    - - - -

    We expect more hardware distributers to create a variety of kits, so please stay tuned for - further developments.

    - -

    ADK Components

    -

    The Android Open Accessory Development Kit (ADK) provides an implementation of an Android USB - accessory that is based on the Arduino open source electronics - prototyping platform, the accessory's hardware design files, code that implements the - accessory's firmware, and the Android application that interacts with the accessory. The hardware - design files and firmware code are contained in the ADK package download.

    -

    The main hardware and software components of the ADK include:

    - - - -

    Getting Started with the ADK

    - -

    The following sections describe how to install the Arduino software on your computer, use the - Arduino IDE to install the ADK board's firmware, and install and run the accompanying - Android application for the ADK board. Before you begin, download the following items to set up - your development environment:

    - - - -

    Installing the Arduino software and necessary libraries

    - -

    To install the Arduino software:

    - -
      -
    1. - Download and install the Arduino 1.0 or - higher as described on the Arduino website. - -

      Note: If you are on a Mac, install the FTDI USB Serial - Driver that is included in the Arduino package, even though the installation instructions say - otherwise.

      -
    2. - -
    3. Download and - extract the ADK package to a directory of your choice. You should have an app, - arduino_libs, and hardware directories.
    4. - -
    5. Download and extract - the CapSense package to a directory of your choice.
    6. - -
    7. Install the necessary libraries: - -

      On Windows:

      - -
        -
      1. Copy the arduino_libs/AndroidAccessory and - arduino_libs/USB_Host_Shield directories (the complete directories, - not just the files within) to the <arduino_installation_root>/libraries/ - directory.
      2. - -
      3. Copy the extracted CapSense/ library directory and its contents to the - <arduino_installation_root>/libraries/ directory.
      4. -
      - -

      On Mac:

      - -
        -
      1. Create, if it does not already exist, an Arduino - directory inside your user account's Documents directory, and within - that, a libraries directory.
      2. - -
      3. Copy the arduino_libs/AndroidAccessory and - arduino_libs/USB_Host_Shield directories (the - complete directories, not just the files within) to your - Documents/Arduino/libraries/ directory.
      4. - -
      5. Copy the extracted CapSense/ library directory and its contents to the - Documents/Arduino/libraries/ directory. -
      - -

      On Linux (Ubuntu):

      - -
        -
      1. Copy the firmware/arduino_libs/AndroidAccessory and - firmware/arduino_libs/USB_Host_Shield directories (the complete directories, - not just the files within) to the <arduino_installation_root>/libraries/ - directory.
      2. - -
      3. Copy the extracted CapSense/ library directory and its contents to the - <arduino_installation_root>/libraries/ directory.
      4. - -
      5. Install the avr-libc library by entering sudo apt-get install avr-libc - from a shell prompt.
      6. -
      -
    8. -
    - -

    You should now have three new directories in the Arduino libraries/ directory: - AndroidAccessory, USB_Host_Shield, and CapSense.

    - -

    Installing the firmware to the ADK board

    - -

    To install the firmware to the ADK board:

    - -
      -
    1. Connect the ADK board to your computer using the micro-USB port, which allows two-way - communication and provides power to the ADK board.
    2. - -
    3. Launch the Arduino IDE.
    4. - -
    5. Click Tools > Board > Arduino Mega 2560 to specify the ADK board's - type.
    6. - -
    7. Select the appropriate USB port: - -
        -
      • On Windows: click Tools > Serial Port > COM# to specify the port - of communication. The COM port number varies depending on your computer. COM1 is usually - reserved for serial port connections. You most likely want COM2 or COM3.
      • - -
      • On Mac: Click Tools > Serial Port > dev/tty.usbserial-### to - specify the port of communication.
      • - -
      • On Linux (Ubuntu): Click Tools > Serial Port > dev/ttyUSB# to - specify the port of communication.
      • -
      -
    8. - -
    9. To open the Demokit sketch (firmware code), click File > Examples > - AndroidAccessory > demokit.
    10. - -
    11. Click Sketch > Verify/Compile to ensure that the sketch has no - errors.
    12. - -
    13. Select File > Upload. When Arduino outputs Done - uploading., the board is ready to communicate with your Android-powered device.
    14. -
    - -

    Running the DemoKit Android application

    - -

    The DemoKit Android application runs on your Android-powered device and communicates with the - ADK board. The ADK board receives commands such as lighting up the board's LEDs or sends data - from the board such as joystick movement and temperature readings.

    - -

    To install and run the application in Eclipse:

    - -
      -
    1. Install the - Google APIs API Level 10 add-on library, which includes the Open Accessory library for - 2.3.4 devices that support accessory mode. This library is also forward compatible with Android - 3.1 or newer devices that support accessory mode. If you only care about Android 3.1 or newer - devices, all you need is API Level 12. For more information on deciding which API level to use, - see the USB Accessory - documentation.
    2. - -
    3. Click File > New > Project..., then select Android > - Android Project
    4. - -
    5. In the Project name: field, type DemoKit.
    6. - -
    7. Choose Create project from existing source, click Browse, - select the app directory, click Open to close that dialog and then - click Finish.
    8. - -
    9. For Build Target, select Google APIs (Platform 2.3.3, API Level 10). - -

      Note: Even though the add-on is labeled as - 2.3.3, the newest Google API add-on library for API level 10 adds USB Open - Accessory API support for 2.3.4 devices.

      -
    10. - -
    11. Click Finish.
    12. - -
    13. Install the application to your device.
    14. - -
    15. Connect the ADK board (USB-A) to your Android-powered device (micro-USB). Ensure that the - power cable to the accessory is plugged in or that the micro-USB port on the accesory is - connected to your computer for power (this also allows you to monitor the - ADK board). When connected, accept the prompt that asks for whether or not to open the - DemoKit application to connect to the accessory. If the prompt does not show up, connect and - reconnect the accessory.
    16. -
    - -

    You can now interact with the ADK board by moving the color LED or servo sliders (make sure - the servos are connected) or by pressing the relay buttons in the application. On the ADK shield, - you can press the buttons and move the joystick to see their outputs displayed in the - application.

    - -

    Monitoring the ADK Board

    - -

    The ADK firmware consists of a few files that you should be looking at if you want to build - your own accessory. The files in the arduino_libs/AndroidAccessory - directory are the most important files and have the logic to detect and connect to - Android-powered devices that support accessory mode. Feel free to add debug statements (Arduino - Serial.println() statements) to the code located in the - <arduino_installation_root>/libraries/AndroidAccessory directory and - demokit.pde sketch and re-upload the sketch to the ADK board to - discover more about how the firmware works.

    - -

    You can view the debug statements in the Arduino Serial Monitor by clicking Tools > - Serial Monitor and setting the baud to 115200. The following sections about how - accessories communicate with Android-powered devices describe much of what you should be doing in - your own accessory.

    - -

    Implementing the Android Accessory Protocol

    - -

    An Android USB accessory must adhere to Android Accessory Protocol, which defines how - an accessory detects and sets up communication with an Android-powered device. In general, an - accessory should carry out the following steps:

    - -
      -
    1. Wait for and detect connected devices
    2. - -
    3. Determine the device's accessory mode support
    4. - -
    5. Attempt to start the device in accessory mode if needed
    6. - -
    7. Establish communication with the device if it supports the Android accessory protocol
    8. -
    - -

    The following sections go into depth about how to implement these steps.

    - -

    Wait for and detect connected devices

    - -

    Your accessory should have logic to continuously check - for connected Android-powered devices. When a device is connected, your accessory should - determine if the device supports accessory mode.

    - -

    Determine the device's accessory mode support

    - - -

    When an Android-powered device is connected, it can be in one of three states:

    - -
      -
    1. The attached device supports Android accessory mode and is already in accessory mode.
    2. - -
    3. The attached device supports Android accessory mode, but it is not in accessory mode.
    4. - -
    5. The attached device does not support Android accessory mode.
    6. -
    - -

    During the initial connection, the accessory should check the vendor and product IDs of the - connected device's USB device descriptor. The vendor ID should match Google's ID (0x18D1) and the - product ID should be 0x2D00 or 0x2D01 if the device is already in accessory mode (case A). If so, - the accessory can now establish communication with the device through - bulk transfer endpoints with its own communication protocol. There is no need to start the device - in accessory mode.

    - -

    Note: 0x2D00 is reserved for Android-powered devices that - support accessory mode. 0x2D01 is reserved for devices that support accessory mode as well as the - ADB (Android Debug Bridge) protocol, which exposes a second interface with two bulk endpoints for - ADB. You can use these endpoints for debugging the accessory application if you are simulating - the accessory on a computer. In general, do not use this interface unless your accessory is - implementing a passthrough to ADB on the device.

    - -

    If the vendor and product ID do not match, there is no way to distinguish between states b and - c, so the accessory attempts to start the device in accessory mode to figure - out if the device is supported.

    - -

    Attempt to start the device in accessory mode

    - -

    If the vendor and product IDs do not correspond to an Android-powered device in accessory - mode, the accessory cannot discern whether the device supports accessory mode and is not in that - state, or if the device does not support accessory mode at all. This is because devices that - support accessory mode but aren't in it initially report the device's manufacturer vendor ID and - product ID, and not the special Android Open Accessory ones. In either case, the accessory should try to start - the device into accessory mode to figure out if the device supports it. The following steps - explain how to do this:

    - -
      -
    1. Send a 51 control request ("Get Protocol") to figure out if the device supports the Android - accessory protocol. A non-zero number is returned if the protocol is supported, which - represents the version of the protocol that the device supports (currently, only version 1 - exists). This request is a control request on endpoint 0 with the following characteristics: -
      -requestType:    USB_DIR_IN | USB_TYPE_VENDOR
      -request:        51
      -value:          0
      -index:          0
      -data:           protocol version number (16 bits little endian sent from the device to the accessory)
      -
      -
    2. - -
    3. If the device returns a proper protocol version, send identifying string information to the - device. This information allows the device to figure out an appropriate application for this - accessory and also present the user with a URL if an appropriate application does not exist. - These requests are control requests on endpoint 0 (for each string ID) with the following - characteristics: -
      -requestType:    USB_DIR_OUT | USB_TYPE_VENDOR
      -request:        52
      -value:          0
      -index:          string ID
      -data            zero terminated UTF8 string sent from accessory to device
      -
      - -

      The following string IDs are supported, with a maximum size of 256 bytes for each string - (must be zero terminated with \0).

      -
      -manufacturer name:  0
      -model name:         1
      -description:        2
      -version:            3
      -URI:                4
      -serial number:      5
      -
      -
    4. - -
    5. When the identifying strings are sent, request the device start up in accessory mode. This - request is a control request on endpoint 0 with the following characteristics: -
      -requestType:    USB_DIR_OUT | USB_TYPE_VENDOR
      -request:        53
      -value:          0
      -index:          0
      -data:           none
      -
      -
    6. -
    - -

    After sending the final control request, the connected USB device should re-introduce itself - on the bus in accessory mode and the accessory can re-enumerate the connected devices. The - algorithm jumps back to determining the device's accessory mode support - to check for the vendor and product ID. The vendor ID and product ID of the device will be - different if the device successfully switched to accessory mode and will now correspond to - Google's vendor and product IDs instead of the device manufacturer's IDs. The accessory can now - establish communication with the device.

    - -

    If at any point these steps fail, the device does not support Android accessory mode and the - accessory should wait for the next device to be connected.

    - -

    Establish communication with the device

    - -

    If an Android-powered device in accessory mode is detected, the accessory can query the - device's interface and endpoint descriptors to obtain the bulk endpoints to communicate with the - device. An Android-powered device that has a product ID of 0x2D00 has one interface with two bulk - endpoints for input and output communication. A device with product ID of 0x2D01 has two - interfaces with two bulk endpoints each for input and output communication. The first interface - is for standard communication while the second interface is for ADB communication. To communicate - on an interface, all you need to do is find the first bulk input and output endpoints, set the - device's configuration to a value of 1 with a SET_CONFIGURATION (0x09) device request, then - communicate using the endpoints.

    - -

    How the ADK board implements the Android Accessory protocol

    - -

    If you have access to the ADK board and shield, the following sections describe the firmware - code that you installed onto the ADK board. The firmware demonstrates a practical example of how - to implement the Android Accessory protocol. Even if you do not have the ADK board and shield, - reading through how the hardware detects and interacts with devices in accessory mode is still - useful if you want to port the code over for your own accessories.

    - -

    The important pieces of the firmware are the - arduino_libs/AndroidAccessory/examples/demokit/demokit/demokit.pde sketch, which is - the code that receives and sends data to the DemoKit application running on the Android-powered - device. The code to detect and set up communication with the Android-powered device is contained - in the arduino_libs/AndroidAccessory/AndroidAccessory.h and - arduino_libs/AndroidAccessory/AndroidAccessory.cpp files. This code - includes most of the logic that will help you implement your own accessory's firmware. It might - be useful to have all three of these files open in a text editor as you read through these next - sections.

    - -

    The following sections describe the firmware code in the context of the algorithm described in - Implementing the Android Accessory Protocol.

    - -

    Wait for and detect connected devices

    - -

    In the firmware code (demokit.pde), the loop() function runs - repeatedly and calls AndroidAccessory::isConnected() to check for any connected - devices. If there is a connected device, it continuously updates the input and output streams - going to and from the board and application. If nothing is connected, it continuously checks for - a device to be connected:

    -
    -...
    -
    -AndroidAccessory acc("Google, Inc.",
    -                     "DemoKit",
    -                     "DemoKit Arduino Board",
    -                     "1.0",
    -                     "http://www.android.com",
    -                     "0000000012345678");
    -
    -...
    -void loop()
    -{
    -...
    -    if (acc.isConnected()) {
    -        //communicate with Android application
    -    }
    -    else{
    -        //set the accessory to its default state
    -    }
    -...
    -}
    -
    - -

    Determine the connected device's accessory mode support

    - -

    When a device is connected to the ADK board, it can already be in accessory mode, support - accessory mode and is not in that mode, or does not support accessory mode. The - AndroidAccessory::isConnected() method checks for these cases and responds - accordingly when the loop() function calls it. This function first checks to see if - the device that is connected hasn't already been handled. If not, it gets the connected device's - device descriptor to figure out if the device is already in accessory mode by calling - AndroidAccessory::isAccessoryDevice(). This method checks the vendor and product ID - of the device descriptor. A device in accessory mode has a vendor ID of 0x18D1 and a product ID - of 0x2D00 or 0x2D01. If the device is in accessory mode, then the ADK board can establish communication with the device. If not, the board attempts to start the device in accessory mode.

    -
    -bool AndroidAccessory::isConnected(void)
    -{
    -    USB_DEVICE_DESCRIPTOR *devDesc = (USB_DEVICE_DESCRIPTOR *) descBuff;
    -    byte err;
    -
    -    max.Task();
    -    usb.Task();
    -
    -    if (!connected &&
    -        usb.getUsbTaskState() >= USB_STATE_CONFIGURING &&
    -        usb.getUsbTaskState() != USB_STATE_RUNNING) {
    -        Serial.print("\nDevice addressed... ");
    -        Serial.print("Requesting device descriptor.");
    -
    -        err = usb.getDevDescr(1, 0, 0x12, (char *) devDesc);
    -        if (err) {
    -            Serial.print("\nDevice descriptor cannot be retrieved. Program Halted\n");
    -            while(1);
    -        }
    -
    -        if (isAccessoryDevice(devDesc)) {
    -            Serial.print("found android accessory device\n");
    -
    -            connected = configureAndroid();
    -        } else {
    -            Serial.print("found possible device. switching to serial mode\n");
    -            switchDevice(1);
    -        }
    -    } else if (usb.getUsbTaskState() == USB_DETACHED_SUBSTATE_WAIT_FOR_DEVICE) {
    -        connected = false;
    -    }
    -
    -    return connected;
    -}
    -
    - -

    Attempt to start the device in accessory mode

    - -

    If the device is not already in accessory mode, then the ADK board must determine whether or - not it supports it by sending control request 51 to check the version of the USB accessory - protocol that the device supports (see AndroidAccessory::getProtocol()). Protocol - version 1 is the only version for now, but this can be an integer greater than zero in the - future. If the appropriate protocol version is returned, the board sends control request 52 (one - for each string with AndroidAcessory:sendString()) to send it's identifying - information, and tries to start the device in accessory mode with control request 53. The - AndroidAccessory::switchDevice() method takes care of this:

    -
    -bool AndroidAccessory::switchDevice(byte addr)
    -{
    -    int protocol = getProtocol(addr);
    -    if (protocol == 1) {
    -        Serial.print("device supports protocol 1\n");
    -    } else {
    -        Serial.print("could not read device protocol version\n");
    -        return false;
    -    }
    -
    -    sendString(addr, ACCESSORY_STRING_MANUFACTURER, manufacturer);
    -    sendString(addr, ACCESSORY_STRING_MODEL, model);
    -    sendString(addr, ACCESSORY_STRING_DESCRIPTION, description);
    -    sendString(addr, ACCESSORY_STRING_VERSION, version);
    -    sendString(addr, ACCESSORY_STRING_URI, uri);
    -    sendString(addr, ACCESSORY_STRING_SERIAL, serial);
    -
    -    usb.ctrlReq(addr, 0, USB_SETUP_HOST_TO_DEVICE | USB_SETUP_TYPE_VENDOR | USB_SETUP_RECIPIENT_DEVICE,
    -                ACCESSORY_START, 0, 0, 0, 0, NULL);
    -    return true;
    -}
    -
    If this method returns false, the board waits until a new device is connected. If it is -successful, the device displays itself on the USB bus as being in accessory mode when the ADK board -re-enumerates the bus. When the device is in accessory mode, the accessory then establishes communication with the device. - -

    Establish communication with the device

    - -

    If a device is detected as being in accessory mode, the accessory must find the proper bulk - endpoints and set up communication with the device. When the ADK board detects an Android-powered - device in accessory mode, it calls the AndroidAccessory::configureAndroid() - function:

    -
    -...
    -if (isAccessoryDevice(devDesc)) {
    -            Serial.print("found android acessory device\n");
    -
    -            connected = configureAndroid();
    -        }
    -...
    -
    - -

    which in turn calls the findEndpoints() function:

    -
    -...
    -bool AndroidAccessory::configureAndroid(void)
    -{
    -    byte err;
    -    EP_RECORD inEp, outEp;
    -
    -    if (!findEndpoints(1, &inEp, &outEp))
    -        return false;
    -...
    -
    - -

    The AndroidAccessory::findEndpoints() function queries the Android-powered - device's configuration descriptor and finds the bulk data endpoints in which to communicate with - the USB device. To do this, it first gets the device's first four bytes of the configuration - descriptor (only need descBuff[2] and descBuff[3]), which contains the information about the - total length of data returned by getting the descriptor. This data is used to determine whether - or not the descriptor can fit in the descriptor buffer. This descriptor also contains information - about all the interfaces and endpoint descriptors. If the descriptor is of appropriate size, the - method reads the entire configuration descriptor and fills the entire descriptor buffer with this - device's configuration descriptor. If for some reason the descriptor is no longer attainable, an - error is returned.

    -
    -...
    -
    -bool AndroidAccessory::findEndpoints(byte addr, EP_RECORD *inEp, EP_RECORD *outEp)
    -{
    -    int len;
    -    byte err;
    -    uint8_t *p;
    -
    -    err = usb.getConfDescr(addr, 0, 4, 0, (char *)descBuff);
    -    if (err) {
    -        Serial.print("Can't get config descriptor length\n");
    -        return false;
    -    }
    -
    -
    -    len = descBuff[2] | ((int)descBuff[3] << 8);
    -    if (len > sizeof(descBuff)) {
    -        Serial.print("config descriptor too large\n");
    -            /* might want to truncate here */
    -        return false;
    -    }
    -
    -    err = usb.getConfDescr(addr, 0, len, 0, (char *)descBuff);
    -    if (err) {
    -        Serial.print("Can't get config descriptor\n");
    -        return false;
    -    }
    -
    -...
    -
    - -

    Once the descriptor is in memory, a pointer is assigned to the first position of the buffer - and is used to index the buffer for reading. There are two endpoint pointers (input and output) - that are passed into AndroidAccessory::findEndpoints() and their addresses are set - to 0, because the code hasn't found any suitable bulk endpoints yet. A loop reads the buffer, - parsing each configuration, interface, or endpoint descriptor. For each descriptor, Position 0 - always contains the size of the descriptor in bytes and position 1 always contains the descriptor - type. Using these two values, the loop skips any configuration and interface descriptors and - increments the buffer with the descLen variable to get to the next descriptor.

    - -

    Note: An Android-powered device in accessory mode can - potentially have two interfaces, one for the default communication to the device and the other - for ADB communication. The default communication interface is always indexed first, so finding - the first input and output bulk endpoints will return the default communication endpoints, which - is what the demokit.pde sketch does. If you are writing your own firmware, the logic - to find the appropriate endpoints for your accessory might be different.

    - -

    When it finds the first input and output endpoint descriptors, it sets the endpoint pointers - to those addresses. If the findEndpoints() function finds both an input and output endpoint, it - returns true. It ignores any other endpoints that it finds (the endpoints for the ADB interface, - if present).

    -
    -...
    -    p = descBuff;
    -    inEp->epAddr = 0;
    -    outEp->epAddr = 0;
    -    while (p < (descBuff + len)){
    -        uint8_t descLen = p[0];
    -        uint8_t descType = p[1];
    -        USB_ENDPOINT_DESCRIPTOR *epDesc;
    -        EP_RECORD *ep;
    -
    -        switch (descType) {
    -        case USB_DESCRIPTOR_CONFIGURATION:
    -            Serial.print("config desc\n");
    -            break;
    -
    -        case USB_DESCRIPTOR_INTERFACE:
    -            Serial.print("interface desc\n");
    -            break;
    -
    -        case USB_DESCRIPTOR_ENDPOINT:
    -            epDesc = (USB_ENDPOINT_DESCRIPTOR *)p;
    -            if (!inEp->epAddr && (epDesc->bEndpointAddress & 0x80))
    -                ep = inEp;
    -            else if (!outEp->epAddr)
    -                ep = outEp;
    -            else
    -                ep = NULL;
    -
    -            if (ep) {
    -                ep->epAddr = epDesc->bEndpointAddress & 0x7f;
    -                ep->Attr = epDesc->bmAttributes;
    -                ep->MaxPktSize = epDesc->wMaxPacketSize;
    -                ep->sndToggle = bmSNDTOG0;
    -                ep->rcvToggle = bmRCVTOG0;
    -            }
    -            break;
    -
    -        default:
    -            Serial.print("unkown desc type ");
    -            Serial.println( descType, HEX);
    -            break;
    -        }
    -
    -        p += descLen;
    -    }
    -
    -    if (!(inEp->epAddr && outEp->epAddr))
    -        Serial.println("can't find accessory endpoints");
    -
    -    return inEp->epAddr && outEp->epAddr;
    -}
    -
    -...
    -
    - -

    Back in the configureAndroid() function, if there were endpoints found, they are - appropriately set up for communication. The device's configuration is set to 1 and the state of - the device is set to "running", which signifies that the device is properly set up to communicate - with your USB accessory. Setting this status prevents the device from being re-detected and - re-configured in the AndroidAccessory::isConnected() function.

    -
    -bool AndroidAccessory::configureAndroid(void)
    -{
    -    byte err;
    -    EP_RECORD inEp, outEp;
    -
    -    if (!findEndpoints(1, &inEp, &outEp))
    -        return false;
    -
    -    memset(&epRecord, 0x0, sizeof(epRecord));
    -
    -    epRecord[inEp.epAddr] = inEp;
    -    if (outEp.epAddr != inEp.epAddr)
    -        epRecord[outEp.epAddr] = outEp;
    -
    -    in = inEp.epAddr;
    -    out = outEp.epAddr;
    -
    -    Serial.print("inEp: ");
    -    Serial.println(inEp.epAddr, HEX);
    -    Serial.print("outEp: ");
    -    Serial.println(outEp.epAddr, HEX);
    -
    -    epRecord[0] = *(usb.getDevTableEntry(0,0));
    -    usb.setDevTableEntry(1, epRecord);
    -
    -    err = usb.setConf( 1, 0, 1 );
    -    if (err) {
    -        Serial.print("Can't set config to 1\n");
    -        return false;
    -    }
    -
    -    usb.setUsbTaskState( USB_STATE_RUNNING );
    -
    -    return true;
    -}
    -
    - -

    Lastly, methods to read and write to the appropriate endpoints are needed. The - demokit.pde sketch calls these methods depending on the data that is read from the - Android-powered device or sent by the ADK board. For instance, moving the joystick on the ADK - shield writes data that is read by the DemoKit application running on the Android-powered device. - Moving sliders on the DemoKit application is read by the demokit.pde sketch and - changes the state of the accessory, such as lighting up or changing the color of the LED - lights.

    -
    -int AndroidAccessory::read(void *buff, int len, unsigned int nakLimit) {
    -  return usb.newInTransfer(1, in, len, (char *)buff, nakLimit); }
    -
    -int AndroidAccessory::write(void *buff, int len) {
    -  usb.outTransfer(1, out, len, (char *)buff);
    -  return len; }
    -
    - -

    See the demokit.pde sketch for information about how the ADK board - reads and writes data.

    diff --git a/docs/html/tools/adk/adk.jd b/docs/html/tools/adk/adk.jd new file mode 100644 index 000000000000..2a0c7170f786 --- /dev/null +++ b/docs/html/tools/adk/adk.jd @@ -0,0 +1,709 @@ +page.title=Accessory Development Kit 2011 Guide +@jd:body + + + +

    The Android Open Accessory Development Kit (ADK) is a reference implementation of an Android + Open Accessory, based on the Arduino open source electronics + prototyping platform. The accessory's hardware design files, code that implements the + accessory's firmware, and the Android application that interacts with the accessory are provided + as part of the kit to help hardware builders and software developers get started building their + own accessories. The hardware design files and firmware code are contained in the ADK package download.

    + +

    A limited number of kits were produced and distributed at the Google I/O 2011 developer + conference. However, many hardware builders have reproduced and enhanced the original design and + these boards are available for purchase. The following list of distributors are currently + producing Android Open Accessory compatible development boards:

    + + + +

    We expect more hardware distributers to create a variety of kits, so please stay tuned for + further developments.

    + +

    ADK Components

    + +

    The main hardware and software components of the ADK include:

    + + + +

    Getting Started with the ADK

    + +

    The following sections describe how to install the Arduino software on your computer, use the + Arduino IDE to install the ADK board's firmware, and install and run the accompanying + Android application for the ADK board. Before you begin, download the following items to set up + your development environment:

    + + + +

    Installing the Arduino software and necessary libraries

    + +

    To install the Arduino software:

    + +
      +
    1. + Download and install the Arduino 1.0 or + higher as described on the Arduino website. + +

      Note: If you are on a Mac, install the FTDI USB Serial + Driver that is included in the Arduino package, even though the installation instructions say + otherwise.

      +
    2. + +
    3. Download and + extract the ADK package to a directory of your choice. You should have an app, + arduino_libs, and hardware directories.
    4. + +
    5. Download and extract + the CapSense package to a directory of your choice.
    6. + +
    7. Install the necessary libraries: + +

      On Windows:

      + +
        +
      1. Copy the arduino_libs/AndroidAccessory and + arduino_libs/USB_Host_Shield directories (the complete directories, + not just the files within) to the <arduino_installation_root>/libraries/ + directory.
      2. + +
      3. Copy the extracted CapSense/ library directory and its contents to the + <arduino_installation_root>/libraries/ directory.
      4. +
      + +

      On Mac:

      + +
        +
      1. Create, if it does not already exist, an Arduino + directory inside your user account's Documents directory, and within + that, a libraries directory.
      2. + +
      3. Copy the arduino_libs/AndroidAccessory and + arduino_libs/USB_Host_Shield directories (the + complete directories, not just the files within) to your + Documents/Arduino/libraries/ directory.
      4. + +
      5. Copy the extracted CapSense/ library directory and its contents to the + Documents/Arduino/libraries/ directory. +
      + +

      On Linux (Ubuntu):

      + +
        +
      1. Copy the firmware/arduino_libs/AndroidAccessory and + firmware/arduino_libs/USB_Host_Shield directories (the complete directories, + not just the files within) to the <arduino_installation_root>/libraries/ + directory.
      2. + +
      3. Copy the extracted CapSense/ library directory and its contents to the + <arduino_installation_root>/libraries/ directory.
      4. + +
      5. Install the avr-libc library by entering sudo apt-get install avr-libc + from a shell prompt.
      6. +
      +
    8. +
    + +

    You should now have three new directories in the Arduino libraries/ directory: + AndroidAccessory, USB_Host_Shield, and CapSense.

    + +

    Installing the firmware to the ADK board

    + +

    To install the firmware to the ADK board:

    + +
      +
    1. Connect the ADK board to your computer using the micro-USB port, which allows two-way + communication and provides power to the ADK board.
    2. + +
    3. Launch the Arduino IDE.
    4. + +
    5. Click Tools > Board > Arduino Mega 2560 to specify the ADK board's + type.
    6. + +
    7. Select the appropriate USB port: + +
        +
      • On Windows: click Tools > Serial Port > COM# to specify the port + of communication. The COM port number varies depending on your computer. COM1 is usually + reserved for serial port connections. You most likely want COM2 or COM3.
      • + +
      • On Mac: Click Tools > Serial Port > dev/tty.usbserial-### to + specify the port of communication.
      • + +
      • On Linux (Ubuntu): Click Tools > Serial Port > dev/ttyUSB# to + specify the port of communication.
      • +
      +
    8. + +
    9. To open the Demokit sketch (firmware code), click File > Examples > + AndroidAccessory > demokit.
    10. + +
    11. Click Sketch > Verify/Compile to ensure that the sketch has no + errors.
    12. + +
    13. Select File > Upload. When Arduino outputs Done + uploading., the board is ready to communicate with your Android-powered device.
    14. +
    + +

    Running the DemoKit Android application

    + +

    The DemoKit Android application runs on your Android-powered device and communicates with the + ADK board. The ADK board receives commands such as lighting up the board's LEDs or sends data + from the board such as joystick movement and temperature readings.

    + +

    To install and run the application in Eclipse:

    + +
      +
    1. Install the + Google APIs API Level 10 add-on library, which includes the Open Accessory library for + 2.3.4 devices that support accessory mode. This library is also forward compatible with Android + 3.1 or newer devices that support accessory mode. If you only care about Android 3.1 or newer + devices, all you need is API Level 12. For more information on deciding which API level to use, + see the USB Accessory + documentation.
    2. + +
    3. Click File > New > Project..., then select Android > + Android Project
    4. + +
    5. In the Project name: field, type DemoKit.
    6. + +
    7. Choose Create project from existing source, click Browse, + select the app directory, click Open to close that dialog and then + click Finish.
    8. + +
    9. For Build Target, select Google APIs (Platform 2.3.3, API Level 10). + +

      Note: Even though the add-on is labeled as + 2.3.3, the newest Google API add-on library for API level 10 adds USB Open + Accessory API support for 2.3.4 devices.

      +
    10. + +
    11. Click Finish.
    12. + +
    13. Install the application to your device.
    14. + +
    15. Connect the ADK board (USB-A) to your Android-powered device (micro-USB). Ensure that the + power cable to the accessory is plugged in or that the micro-USB port on the accesory is + connected to your computer for power (this also allows you to monitor the + ADK board). When connected, accept the prompt that asks for whether or not to open the + DemoKit application to connect to the accessory. If the prompt does not show up, connect and + reconnect the accessory.
    16. +
    + +

    You can now interact with the ADK board by moving the color LED or servo sliders (make sure + the servos are connected) or by pressing the relay buttons in the application. On the ADK shield, + you can press the buttons and move the joystick to see their outputs displayed in the + application.

    + +

    Monitoring the ADK Board

    + +

    The ADK firmware consists of a few files that you should be looking at if you want to build + your own accessory. The files in the arduino_libs/AndroidAccessory + directory are the most important files and have the logic to detect and connect to + Android-powered devices that support accessory mode. Feel free to add debug statements (Arduino + Serial.println() statements) to the code located in the + <arduino_installation_root>/libraries/AndroidAccessory directory and + demokit.pde sketch and re-upload the sketch to the ADK board to + discover more about how the firmware works.

    + +

    You can view the debug statements in the Arduino Serial Monitor by clicking Tools > + Serial Monitor and setting the baud to 115200. The following sections about how + accessories communicate with Android-powered devices describe much of what you should be doing in + your own accessory.

    + +

    How the ADK board implements the Android Accessory protocol

    + +

    If you have access to the ADK board and shield, the following sections describe the firmware + code that you installed onto the ADK board. The firmware demonstrates a practical example of how + to implement the Android Accessory protocol. Even if you do not have the ADK board and shield, + reading through how the hardware detects and interacts with devices in accessory mode is still + useful if you want to port the code over for your own accessories.

    + +

    The important pieces of the firmware are the + arduino_libs/AndroidAccessory/examples/demokit/demokit/demokit.pde sketch, which is + the code that receives and sends data to the DemoKit application running on the Android-powered + device. The code to detect and set up communication with the Android-powered device is contained + in the arduino_libs/AndroidAccessory/AndroidAccessory.h and + arduino_libs/AndroidAccessory/AndroidAccessory.cpp files. This code + includes most of the logic that will help you implement your own accessory's firmware. It might + be useful to have all three of these files open in a text editor as you read through these next + sections.

    + +

    The following sections describe the firmware code in the context of the algorithm described in + Implementing the Android Accessory Protocol.

    + +

    Wait for and detect connected devices

    + +

    In the firmware code (demokit.pde), the loop() function runs + repeatedly and calls AndroidAccessory::isConnected() to check for any connected + devices. If there is a connected device, it continuously updates the input and output streams + going to and from the board and application. If nothing is connected, it continuously checks for + a device to be connected:

    +
    +...
    +
    +AndroidAccessory acc("Google, Inc.",
    +                     "DemoKit",
    +                     "DemoKit Arduino Board",
    +                     "1.0",
    +                     "http://www.android.com",
    +                     "0000000012345678");
    +
    +...
    +void loop()
    +{
    +...
    +    if (acc.isConnected()) {
    +        //communicate with Android application
    +    }
    +    else{
    +        //set the accessory to its default state
    +    }
    +...
    +}
    +
    + +

    Determine the connected device's accessory mode support

    + +

    When a device is connected to the ADK board, it can already be in accessory mode, support + accessory mode and is not in that mode, or does not support accessory mode. The + AndroidAccessory::isConnected() method checks for these cases and responds + accordingly when the loop() function calls it. This function first checks to see if + the device that is connected hasn't already been handled. If not, it gets the connected device's + device descriptor to figure out if the device is already in accessory mode by calling + AndroidAccessory::isAccessoryDevice(). This method checks the vendor and product ID + of the device descriptor. A device in accessory mode has a vendor ID of 0x18D1 and a product ID + of 0x2D00 or 0x2D01. If the device is in accessory mode, then the ADK board can establish communication with the device. If not, the board attempts to start the device in accessory mode.

    +
    +bool AndroidAccessory::isConnected(void)
    +{
    +    USB_DEVICE_DESCRIPTOR *devDesc = (USB_DEVICE_DESCRIPTOR *) descBuff;
    +    byte err;
    +
    +    max.Task();
    +    usb.Task();
    +
    +    if (!connected &&
    +        usb.getUsbTaskState() >= USB_STATE_CONFIGURING &&
    +        usb.getUsbTaskState() != USB_STATE_RUNNING) {
    +        Serial.print("\nDevice addressed... ");
    +        Serial.print("Requesting device descriptor.");
    +
    +        err = usb.getDevDescr(1, 0, 0x12, (char *) devDesc);
    +        if (err) {
    +            Serial.print("\nDevice descriptor cannot be retrieved. Program Halted\n");
    +            while(1);
    +        }
    +
    +        if (isAccessoryDevice(devDesc)) {
    +            Serial.print("found android accessory device\n");
    +
    +            connected = configureAndroid();
    +        } else {
    +            Serial.print("found possible device. switching to serial mode\n");
    +            switchDevice(1);
    +        }
    +    } else if (usb.getUsbTaskState() == USB_DETACHED_SUBSTATE_WAIT_FOR_DEVICE) {
    +        connected = false;
    +    }
    +
    +    return connected;
    +}
    +
    + +

    Attempt to start the device in accessory mode

    + +

    If the device is not already in accessory mode, then the ADK board must determine whether or + not it supports it by sending control request 51 to check the version of the USB accessory + protocol that the device supports (see AndroidAccessory::getProtocol()). Protocol + version 1 is supported by Android 2.3.4 (API Level 10) and higher. Protocol version 2 is + supported by Android 4.1 (API Level 16) and higher. Versions greater than 2 may supported in + the future. + If the appropriate protocol version is returned, the board sends control request 52 (one + for each string with AndroidAcessory:sendString()) to send it's identifying + information, and tries to start the device in accessory mode with control request 53. The + AndroidAccessory::switchDevice() method takes care of this:

    +
    +bool AndroidAccessory::switchDevice(byte addr)
    +{
    +    int protocol = getProtocol(addr);
    +    if (protocol >= 1) {
    +        Serial.print("device supports protocol 1\n");
    +    } else {
    +        Serial.print("could not read device protocol version\n");
    +        return false;
    +    }
    +
    +    sendString(addr, ACCESSORY_STRING_MANUFACTURER, manufacturer);
    +    sendString(addr, ACCESSORY_STRING_MODEL, model);
    +    sendString(addr, ACCESSORY_STRING_DESCRIPTION, description);
    +    sendString(addr, ACCESSORY_STRING_VERSION, version);
    +    sendString(addr, ACCESSORY_STRING_URI, uri);
    +    sendString(addr, ACCESSORY_STRING_SERIAL, serial);
    +
    +    usb.ctrlReq(addr, 0, USB_SETUP_HOST_TO_DEVICE | USB_SETUP_TYPE_VENDOR | USB_SETUP_RECIPIENT_DEVICE,
    +                ACCESSORY_START, 0, 0, 0, 0, NULL);
    +    return true;
    +}
    +
    If this method returns false, the board waits until a new device is connected. If it is +successful, the device displays itself on the USB bus as being in accessory mode when the ADK board +re-enumerates the bus. When the device is in accessory mode, the accessory then establishes communication with the device. + +

    Establish communication with the device

    + +

    If a device is detected as being in accessory mode, the accessory must find the proper bulk + endpoints and set up communication with the device. When the ADK board detects an Android-powered + device in accessory mode, it calls the AndroidAccessory::configureAndroid() + function:

    +
    +...
    +if (isAccessoryDevice(devDesc)) {
    +            Serial.print("found android acessory device\n");
    +
    +            connected = configureAndroid();
    +        }
    +...
    +
    + +

    which in turn calls the findEndpoints() function:

    +
    +...
    +bool AndroidAccessory::configureAndroid(void)
    +{
    +    byte err;
    +    EP_RECORD inEp, outEp;
    +
    +    if (!findEndpoints(1, &inEp, &outEp))
    +        return false;
    +...
    +
    + +

    The AndroidAccessory::findEndpoints() function queries the Android-powered + device's configuration descriptor and finds the bulk data endpoints in which to communicate with + the USB device. To do this, it first gets the device's first four bytes of the configuration + descriptor (only need descBuff[2] and descBuff[3]), which contains the information about the + total length of data returned by getting the descriptor. This data is used to determine whether + or not the descriptor can fit in the descriptor buffer. This descriptor also contains information + about all the interfaces and endpoint descriptors. If the descriptor is of appropriate size, the + method reads the entire configuration descriptor and fills the entire descriptor buffer with this + device's configuration descriptor. If for some reason the descriptor is no longer attainable, an + error is returned.

    +
    +...
    +
    +bool AndroidAccessory::findEndpoints(byte addr, EP_RECORD *inEp, EP_RECORD *outEp)
    +{
    +    int len;
    +    byte err;
    +    uint8_t *p;
    +
    +    err = usb.getConfDescr(addr, 0, 4, 0, (char *)descBuff);
    +    if (err) {
    +        Serial.print("Can't get config descriptor length\n");
    +        return false;
    +    }
    +
    +
    +    len = descBuff[2] | ((int)descBuff[3] << 8);
    +    if (len > sizeof(descBuff)) {
    +        Serial.print("config descriptor too large\n");
    +            /* might want to truncate here */
    +        return false;
    +    }
    +
    +    err = usb.getConfDescr(addr, 0, len, 0, (char *)descBuff);
    +    if (err) {
    +        Serial.print("Can't get config descriptor\n");
    +        return false;
    +    }
    +
    +...
    +
    + +

    Once the descriptor is in memory, a pointer is assigned to the first position of the buffer + and is used to index the buffer for reading. There are two endpoint pointers (input and output) + that are passed into AndroidAccessory::findEndpoints() and their addresses are set + to 0, because the code hasn't found any suitable bulk endpoints yet. A loop reads the buffer, + parsing each configuration, interface, or endpoint descriptor. For each descriptor, Position 0 + always contains the size of the descriptor in bytes and position 1 always contains the descriptor + type. Using these two values, the loop skips any configuration and interface descriptors and + increments the buffer with the descLen variable to get to the next descriptor.

    + +

    Note: An Android-powered device in accessory mode can + potentially have two interfaces, one for the default communication to the device and the other + for ADB communication. The default communication interface is always indexed first, so finding + the first input and output bulk endpoints will return the default communication endpoints, which + is what the demokit.pde sketch does. If you are writing your own firmware, the logic + to find the appropriate endpoints for your accessory might be different.

    + +

    When it finds the first input and output endpoint descriptors, it sets the endpoint pointers + to those addresses. If the findEndpoints() function finds both an input and output endpoint, it + returns true. It ignores any other endpoints that it finds (the endpoints for the ADB interface, + if present).

    +
    +...
    +    p = descBuff;
    +    inEp->epAddr = 0;
    +    outEp->epAddr = 0;
    +    while (p < (descBuff + len)){
    +        uint8_t descLen = p[0];
    +        uint8_t descType = p[1];
    +        USB_ENDPOINT_DESCRIPTOR *epDesc;
    +        EP_RECORD *ep;
    +
    +        switch (descType) {
    +        case USB_DESCRIPTOR_CONFIGURATION:
    +            Serial.print("config desc\n");
    +            break;
    +
    +        case USB_DESCRIPTOR_INTERFACE:
    +            Serial.print("interface desc\n");
    +            break;
    +
    +        case USB_DESCRIPTOR_ENDPOINT:
    +            epDesc = (USB_ENDPOINT_DESCRIPTOR *)p;
    +            if (!inEp->epAddr && (epDesc->bEndpointAddress & 0x80))
    +                ep = inEp;
    +            else if (!outEp->epAddr)
    +                ep = outEp;
    +            else
    +                ep = NULL;
    +
    +            if (ep) {
    +                ep->epAddr = epDesc->bEndpointAddress & 0x7f;
    +                ep->Attr = epDesc->bmAttributes;
    +                ep->MaxPktSize = epDesc->wMaxPacketSize;
    +                ep->sndToggle = bmSNDTOG0;
    +                ep->rcvToggle = bmRCVTOG0;
    +            }
    +            break;
    +
    +        default:
    +            Serial.print("unkown desc type ");
    +            Serial.println( descType, HEX);
    +            break;
    +        }
    +
    +        p += descLen;
    +    }
    +
    +    if (!(inEp->epAddr && outEp->epAddr))
    +        Serial.println("can't find accessory endpoints");
    +
    +    return inEp->epAddr && outEp->epAddr;
    +}
    +
    +...
    +
    + +

    Back in the configureAndroid() function, if there were endpoints found, they are + appropriately set up for communication. The device's configuration is set to 1 and the state of + the device is set to "running", which signifies that the device is properly set up to communicate + with your USB accessory. Setting this status prevents the device from being re-detected and + re-configured in the AndroidAccessory::isConnected() function.

    +
    +bool AndroidAccessory::configureAndroid(void)
    +{
    +    byte err;
    +    EP_RECORD inEp, outEp;
    +
    +    if (!findEndpoints(1, &inEp, &outEp))
    +        return false;
    +
    +    memset(&epRecord, 0x0, sizeof(epRecord));
    +
    +    epRecord[inEp.epAddr] = inEp;
    +    if (outEp.epAddr != inEp.epAddr)
    +        epRecord[outEp.epAddr] = outEp;
    +
    +    in = inEp.epAddr;
    +    out = outEp.epAddr;
    +
    +    Serial.print("inEp: ");
    +    Serial.println(inEp.epAddr, HEX);
    +    Serial.print("outEp: ");
    +    Serial.println(outEp.epAddr, HEX);
    +
    +    epRecord[0] = *(usb.getDevTableEntry(0,0));
    +    usb.setDevTableEntry(1, epRecord);
    +
    +    err = usb.setConf( 1, 0, 1 );
    +    if (err) {
    +        Serial.print("Can't set config to 1\n");
    +        return false;
    +    }
    +
    +    usb.setUsbTaskState( USB_STATE_RUNNING );
    +
    +    return true;
    +}
    +
    + +

    Lastly, methods to read and write to the appropriate endpoints are needed. The + demokit.pde sketch calls these methods depending on the data that is read from the + Android-powered device or sent by the ADK board. For instance, moving the joystick on the ADK + shield writes data that is read by the DemoKit application running on the Android-powered device. + Moving sliders on the DemoKit application is read by the demokit.pde sketch and + changes the state of the accessory, such as lighting up or changing the color of the LED + lights.

    +
    +int AndroidAccessory::read(void *buff, int len, unsigned int nakLimit) {
    +  return usb.newInTransfer(1, in, len, (char *)buff, nakLimit); }
    +
    +int AndroidAccessory::write(void *buff, int len) {
    +  usb.outTransfer(1, out, len, (char *)buff);
    +  return len; }
    +
    + +

    See the demokit.pde sketch for information about how the ADK board + reads and writes data.

    diff --git a/docs/html/tools/adk/adk2.jd b/docs/html/tools/adk/adk2.jd new file mode 100644 index 000000000000..d3fed306a7e1 --- /dev/null +++ b/docs/html/tools/adk/adk2.jd @@ -0,0 +1,617 @@ +page.title=Accessory Development Kit 2012 Guide +@jd:body + + + +

    The Android Accessory Development Kit (ADK) for 2012 is the latest reference implementation of +an Android Open Accessory device, designed to help Android hardware accessory +builders and software developers create accessories for Android. The ADK 2012 is based on the Arduino open source electronics prototyping platform, with some +hardware and software extensions that allow it to communicate with Android devices.

    + +

    A limited number of these kits were produced and distributed at the Google I/O 2012 developer +conference. If you did not receive one of these kits, fear not! The specifications and design files +for the hardware were also released for use by manufacturers and hobbyists. You should expect to see +kits with similar features available for purchase, or you can build one yourself!

    + +

    One of the important new features demonstrated by this ADK is the ability to play audio over a +USB connection. Be sure to check out the reference implementation of a USB +audio dock in this ADK if you are interested in making audio-related USB accessories for +Android.

    + +

    Components

    + +

    The ADK 2012 is based on the Arduino open source electronics prototyping platform and is an open +hardware design. The hardware design files and firmware source code are included with the ADK +software download. The ADK contains two main physical hardware components:

    + +
      +
    1. Main processing board containing the microprocessor, USB connections, power connector and +input/output pins. This board can be removed and used separately from the rest of the hardware.
    2. +
    3. Shield containing sensors, LEDs, input controls, audio amplifier and speaker output, contained +in a custom, polygon box enclosure.
    4. +
    + +

    The main hardware features of the ADK are as follows:

    + + + +

    The ADK comes preloaded with an alarm clock firmware program that you can use immediately. A +companion Android application, ADK 2012, is +available on Google Play. The source code for both the Android application and the ADK firmware (an +Arduino sketch) can be downloaded from this page.

    + +

    The ADK 2012 also comes with additional parts to help you develop accessories with it, +including:

    + + + +

    Using the Alarm Clock

    + +

    An alarm clock program comes preloaded on the ADK. This firmware program allows you to use the +ADK as an alarm clock.

    + +

    To use the ADK as an alarm clock:

    + +
      +
    1. Open up the ADK by gently squeezing the two widest faces of the enclosure.
    2. +
    3. Attach the provided AC power adapter (round connector) to the main ADK board, or attach a USB +cable to the port marked Computer and a USB port on your computer.
    4. +
    5. Place and hold your fingertip over the clock symbol on the control face. +

      Note: You may need to hold your finger in place for 1-2 +seconds.

      +
    6. +
    7. Use the plus (+) and minus (-) symbols inside the clock digits to set the correct time.
    8. +
    9. Place your fingertip over the alarm clock symbol to activate alarm setting.
    10. +
    11. Use the plus (+) and minus (-) symbols inside the clock digits to set the alarm time.
    12. +
    13. Use the plus (+) and minus (-) symbols inside the last two clock digits to turn the alarm on +({@code on}) or off ({@code oF}).
    14. +
    15. To set the alarm tone, place and hold your fingertip over the alarm clock symbol, then tap the +slider control on top of the enclosure.
    16. +
    + +

    To use the ADK 2012 companion application for the alarm clock:

    + +
      +
    1. Load the companion application on your Android device running Android 3.1 (API Level 12) or +higher: +
        +
      • Using an NFC-enabled Android device, unlock and hold the device against the left side of +the ADK enclosure. Follow the prompts to install the app. +
        - or -
      • +
      • Start Google Play on your device, search for the ADK 2012 application and +install it. If you cannot find the app, your device is not compatible. Try installing on another +device.
      • +
      +
    2. +
    3. Connect your Android device to the ADK using one of the following methods: +
        +
      • To connect using Bluetooth: +
          +
        1. Plug the AC power adapter into the ADK.
        2. +
        3. On your Android device, turn Bluetooth On (Settings > +Bluetooth).
        4. +
        5. In the Settings page, press the Bluetooth option to +view paired devices.
        6. +
        7. Select Search for Devices to locate the ADK 2012 accessory and follow +the on screen instructions to connect.
        8. +
        9. After pairing is complete, start the ADK 2012 app on your Android +device.
        10. +
        11. On the start screen, select the Use Bluetooth button.
        12. +
        13. In the list of paired devices, select the ADK 2012 device.
        14. +
        15. The ADK 2012 app should switch to the menu screen and you can start +interacting with the ADK accessory.
        16. +
        +
      • + +
      • To connect using USB: +
          +
        1. Plug the AC power adapter into the ADK.
        2. +
        3. Connect the Micro USB AB connector (small, rectangular plug) to the port labeled +Phone on the ADK board.
        4. +
        5. Unlock your Android device and connect the Micro USB B connector to your device. +

          Note: Your device must support Android USB accessory +mode. Devices that support this mode include Google Nexus devices.

          +
        6. +
        7. On the Android device, you should see a prompt to connect to the ADK DemoKit +2012, select OK.
        8. +
        9. The ADK 2012 app should start automatically and you can start +interacting with the ADK.
        10. +
        +
      • +
      +
    4. +
    + +

    Note: When using the ADK with a USB connection to an Android +device, make sure the AC power adapter is plugged in. A USB connection to a computer does not +provide sufficient voltage to power both the ADK and a USB connection to a device.

    + + +

    Playing Audio

    + +

    The ADK 2012 comes with audio output capabilities, including an amplifier and speaker. You can +use it to play audio from your Android device using either a Bluetooth or USB connection.

    + +

    To play audio over Bluetooth:

    + +
      +
    1. Follow the instructions in the previous section to connect your Android +device to the ADK over Bluetooth.
    2. +
    3. On your Android device, navigate to the Settings > Bluetooth page to view the +list of paired devices, and make sure the ADK is connected.
    4. +
    5. Select the settings icon next to the ADK 2012 item.
    6. +
    7. Make sure the Media Audio option is enabled.
    8. +
    9. Navigate to an application that plays music or other audio.
    10. +
    11. Play a song or sound and listen to it on the ADK speaker!
    12. +
    + +

    To play audio over USB, you must use a device running Android 4.1 (API Level 16) or higher:

    + +
      +
    1. Plug the AC power adapter into the ADK.
    2. +
    3. Connect the Micro USB AB connector (small, rectangular plug) to the Phone port on the ADK +board.
    4. +
    5. Unlock your Android device and connect the Micro USB B connector to your device. +

      Note: Your device must support Android USB accessory +mode. Devices that support this mode include Google Nexus devices.

      +
    6. +
    7. On the Android device, you should see a prompt to connect to the ADK DemoKit +2012, select Cancel and allow the ADK to connect as a media device +only.
    8. +
    9. Navigate to an application that plays music or other audio.
    10. +
    11. Play a song or sound and listen to it on the ADK speaker!
    12. +
    + +

    Note: When using the ADK with a USB connection to an Android +device, make sure the AC power adapter is plugged in. A USB connection to a computer does not +provide sufficient voltage to power both the ADK and a USB connection to a device.

    + + +

    Developing Accessories with ADK 2012

    + +

    The ADK 2012 is a hardware platform and a set of software libraries for prototyping Android +accessories. This section discusses how to set up a development environment for programming the ADK +to make it do what you want and to test software for building your own Android accessories.

    + +

    Downloading the ADK Source

    + +

    The support software and hardware specifications for the ADK 2012 are available from the Android +source repository. Follow the instructions below to obtain the source material for the ADK.

    + +

    To download the ADK 2012 software, source code and hardware design specifications.

    + +
      +
    1. Download and install Git for your development +system.
    2. +
    3. Download and setup the {@code repo} tool, as described on the Android open source +project site.
    4. +
    5. In a terminal window, create a new directory for the downloaded source files, initialize and +synchronize a local repository: +
      +$> mkdir android-accessories
      +$> cd android-accessories
      +$> repo -init -u https://android.googlesource.com/platform/manifest -b android-accessories
      +$> repo sync
      +
      +
    6. +
    + +

    After successfully completing this process, you should have the source code and tools for +working with the ADK 2012:

    + + + + +

    Setting Up the Development Environment

    + +

    The ADK 2012 source code repository includes an integrated development environment (IDE) that you +can use to develop software for and program the ADK 2012 accessory. The following instructions +explain how to build and run the ADK 2012 IDE.

    + +

    To set up, build and run the ADK 2012 IDE:

    + +
      +
    1. Download the ADK 2012 source code files.
    2. +
    3. Download and install the Java Development Kit 6 or higher from java.oracle.com.
    4. +
    5. Download and install Apache Ant.
    6. +
    7. In a terminal window, navigate to {@code <adk-source-download>/external/ide/build} +and execute the following command to build the ADK 2012 IDE: +
      $> ant
      +
    8. +
    9. After the build successfully completes, execute the following command to run the ADK 2012 +IDE: +
      $> ant run
    10. +
    + +

    After you have successfully built and run the ADK 2012 IDE, you must configure it to use the +ADK 2012 library.

    + +

    To configure the ADK 2012 IDE for use with the ADK libraries:

    + +
      +
    1. Start the ADK 2012 IDE and choose File > Preferences.
    2. +
    3. In the Preferences dialog, make a note of the Sketchbook +location directory.
    4. +
    5. Copy the {@code <adk-source-download>/adk2012/board/library/ADK2} directory and its +contents into your {@code sketchbook/libraries/} directory, so that you create a {@code +sketchbook/libraries/ADK2} directory.
    6. +
    7. Stop and restart the ADK 2012 IDE.
    8. +
    9. In the IDE, choose File > Examples > ADK2 and then choose one of the example +sketches: +
        +
      • clock - The sketch that is preloaded onto the ADK 2012.
      • +
      • BluetoothScan - An example sketch demonstrating code that connects an +accessory to an Android device over Bluetooth.
      • +
      • usbaccessory - An example sketch demonstrating code that connects an +accessory to an Android device through a USB cable.
      • +
      +
    10. +
    11. Connect a Micro USB cable from the Computer port on the ADK board to your +development computer.
    12. +
    13. In the ADK 2012 IDE, establish a serial port connection with the ADK by +selecting Tools > Serial Port and selecting the serial port for the ADK.
    14. +
    15. In the ADK 2012 IDE, choose the ADK by selectingTools > Board > +Google ADK2.
    16. +
    17. Modify an example sketch or create your own.
    18. +
    19. Upload the sketch to the ADK by choosing File > Upload. When the ADK +2012 IDE reports Done uploading, the sketch is uploaded and the ADK is +ready to communicate with your Android device.
    20. +
    + +

    Using the ADK Alternative Build System

    + +

    An alternative build and load system is also available for the ADK 2012. This system is command +line based and intended for production environments where using an IDE environment to load software +onto accessory hardware may be inconvenient or undesirable.

    + +

    To use the command line based build system:

    +
      +
    1. Download the ADK 2012 source code files.
    2. +
    3. Download and install the make tool on your +development system.
    4. +
    5. Update your system’s PATH to include {@code +<adk-source-download>/external/toolchain/bin}.
    6. +
    7. Open a terminal window, navigate to {@code +<adk-source-download>/external/toolchain/app} and execute the following command: +
      $> make
    8. +
    + +

    How the ADK Connects with Android Devices

    + +

    The essential feature of any Android accessory is its ability to connect and communicate with an +Android device. Creating a fast and reliable connection between your accessory and Android devices +is the first order of business when building software for an accessory. This section describes the +connection and communication essentials used in the ADK 2012 so that you can apply them to +developing your own Android accessories.

    + +

    ADK Connection over Bluetooth

    + +

    The ADK 2012 app and hardware accessory use a Bluetooth Serial Port Profile (SPP) connection to +communicate. This connection allows two way communication between the ADK accessory and Android +devices.

    + +

    Note: The implementation of the ADK hardware allows the use of +other profiles and multiple connections. However, the basic communication between the ADK 2012 +accessory and the Android application uses SPP.

    + +

    Accessory Bluetooth Code

    + +

    In order to enable Bluetooth communications, the {@code clock.ino} sketch for the ADK 2012 +accessory calls a {@code btStart()} method during the {@code setup()} method to enable radio +frequency communications and start listening for Bluetooth connections:

    + +
    +ADK L;
    +void setup() {
    + L.adkInit();
    + L.btStart();
    +}
    +...
    +void btStart(){
    +    uint8_t i, dlci;
    +    int f;
    +
    +    L.btEnable(adkBtConnectionRequest, adkBtLinkKeyRequest, adkBtLinkKeyCreated,
    +               adkBtPinRequest, NULL);
    +
    +    dlci = L.btRfcommReserveDlci(RFCOMM_DLCI_NEED_EVEN);
    +
    +    if(!dlci) dbgPrintf("BTADK: failed to allocate DLCI\n");
    +    else{
    +
    +        //change descriptor to be valid...
    +        for(i = 0, f = -1; i < sizeof(sdpDescrADK); i++){
    +
    +            if(sdpDescrADK[i] == MAGIX){
    +                if(f == -1) f = i;
    +                else break;
    +            }
    +        }
    +
    +        if(i != sizeof(sdpDescrADK) || f == -1){
    +
    +            dbgPrintf("BTADK: failed to find a single marker in descriptor\n");
    +            L.btRfcommReleaseDlci(dlci);
    +            return;
    +        }
    +
    +        sdpDescrADK[f] = dlci >> 1;
    +
    +        dbgPrintf("BTADK has DLCI %u\n", dlci);
    +
    +        L.btRfcommRegisterPort(dlci, btAdkPortOpen, btAdkPortClose, btAdkPortRx);
    +        L.btSdpServiceDescriptorAdd(sdpDescrADK, sizeof(sdpDescrADK));
    +    }
    +}
    +
    + +

    Notice that the {@code sdpDescrADK} object contains a Universally Unique Identifier (UUID) in the +variable {@code BT_ADK_UUID}. This identifier must match the device UUID provided in the +{@link android.bluetooth.BluetoothSocket} connection request in the Android application code.

    + +

    Once Bluetooth is enabled with the code shown above, the accessory listens for connection +requests. The ADK library handles listening and connection details, so the accessory calls +{@code ADK::adkEventProcess()} once during each loop execution:

    + +
    +void loop(void)
    +{
    +  ...
    +  L.adkEventProcess(); //let the adk framework do its thing
    +  ...
    +}
    +
    + +

    If a Bluetooth connection has been established, any commands are routed to the +{@code btAdkPortRx()} callback method (which was registered with the ADK library as part of the +{@code btStart()} method) and processed accordingly. The ADK accessory sends messages back through +the Bluetooth connection using the {@code ADK::btRfcommPortTx()} method. +For more details, review the implementations of these methods in the {@code clock.ino} sketch.

    + +

    Android App Bluetooth Code

    + +

    In the ADK 2012 Android app, the code for handling Bluetooth connections is encapsulated in in a +{@code BTConnection} class. In this class, the application requests access to the Bluetooth adapter +and negotiates a connection with the ADK 2012 accessory. Here is a summary of the relevant code:

    + +
    +mAdapter = BluetoothAdapter.getDefaultAdapter();
    +BluetoothDevice device = mAdapter.getRemoteDevice(address);
    +mSocket = device.createInsecureRfcommSocketToServiceRecord(ADK_UUID);
    +mSocket.connect();
    +
    + +

    Note the {@code ADK_UUID} parameter in the second line. This identifier must match the identifier +output by the accessory (the {@code BT_ADK_UUID} variable mentioned earlier), otherwise the protocol +negotiation fails and the {@link android.bluetooth.BluetoothSocket} is not created. Once a +connection is established, you obtain {@link java.io.InputStream} and {@link java.io.OutputStream} +objects from the socket to communicate with the accessory:

    + +
    +mInStream = mSocket.getInputStream();
    +mOutStream = mSocket.getOutputStream();
    +
    + +

    Review the {@code BTConnection.java} file provided in the ADK 2012 software download for more +implementation details.

    + +

    ADK Connection over USB

    + +

    The ADK 2012 app and hardware accessory can also use a USB connection to communicate, similar to +the original ADK.

    + +

    Accessory USB Code

    + +

    The ADK library takes care of most of the implementation details for a USB connection, the +accessory code must make a few calls to initialize USB connectivity, including setting the accessory +identification strings:

    + +
    +ADK L;
    +void setup() {
    +  L.adkInit();
    +  L.usbSetAccessoryStringVendor(...);
    +  L.usbSetAccessoryStringName(...);
    +  L.usbSetAccessoryStringLongname(...);
    +  L.usbSetAccessoryStringVersion(...);
    +  L.usbSetAccessoryStringUrl(...);
    +  L.usbSetAccessoryStringSerial(...);
    +
    +  L.usbStart();
    +}
    +
    + +

    Note: The identification strings must match the USB accessory +filter settings specified in the connecting Android application,otherwise the application cannot +connect with the accessory.

    + +

    Once USB is enabled with code shown above, the accessory listens for connection requests. The ADK +library handles listening and connection details, so the accessory calls {@code +ADK::adkEventProcess()} once during each loop execution:

    + +
    +void loop(void)
    +{
    +  ...
    +  L.adkEventProcess(); //let the adk framework do its thing
    +  ...
    +}
    +
    + +

    The accessory must then check for a live USB connection to process commands and send +messages. Here is a summary of the relevant code:

    + +
    +void loop() {
    +  if (L.accessoryConnected()) {
    +    int recvLen = L.accessoryReceive(msg, sizeof(msg));
    +    if (recvLen > 0) {
    +      ... // process message
    +    }
    +
    +    L.accessorySend(outmsg, outmsgLen);
    +  }
    +  L.adkEventProcess();
    +}  
    +
    + +

    For more details, review the implementations of these methods in the {@code clock.ino} +sketch.

    + +

    Android App USB Code

    + +

    In the ADK 2012 Android app, the code for handling USB connections is encapsulated in a +{@code UsbConnection} class. This class sets up a {@link android.content.BroadcastReceiver} to +listen for USB events and then attempts to connect when a matching connection event is received. +Here is a summary of the relevant code:

    + +
    +import com.android.future.usb.UsbAccessory;
    +import com.android.future.usb.UsbManager;
    +
    +mUSBManager = UsbManager.getInstance(this);
    +UsbAccessory acc = mUSBManager.getAccessoryList()[0];
    +
    +if (!mUSBManager.hasPermission(acc)) return;
    +
    + +

    The ADK 2012 app uses the support library to implement the USB accessory connections, in order to +support devices running Android 2.3.4 (API Level 10). If you only need to support Android +3.1 (API Level 12) and higher devices, you can replace the first 4 lines the following code:

    + +
    +import android.hardware.usb.UsbAccessory
    +import android.hardware.usb.UsbManager
    +
    +mUSBManager = (UsbManager) getSystemService(Context.USB_SERVICE);
    +UsbAccessory acc = (UsbAccessory)
    +                   intent.getParcelableExtra(UsbManager.EXTRA_ACCESSORY);
    +
    + +

    Note that the app only receives events when the USB accessory identification information matches +the information in the {@code res/xml/usb_accessory_filter.xml} file, referenced by the +application’s manifest statement:

    + +
    +<meta-data
    +    android:name="android.hardware.usb.action.USB_ACCESSORY_ATTACHED"
    +    android:resource="@xml/usb_accessory_filter" />
    +
    + +

    Connections from other USB devices are not received by the ADK 2012 accessory.

    + +

    Once the connection is established, the app can communicate with the accessory through file input +and output streams, as shown in the following example code:

    + +
    +ParcelFileDescriptor mFD = mUSBManager.openAccessory(acc);
    +if (mFD != null) {
    +  FileDescripter fd = mFD.getFileDescriptor();
    +  mIS = new FileInputStream(fd);  // use this to receive messages
    +  mOS = new FileOutputStream(fd); // use this to send commands
    +}
    +
    + +

    Review the {@code UsbConnection.java} file provided in the ADK 2012 source code for more +implementation details.

    + +

    USB Audio Dock Implementation

    + +

    One of the important new features introduced with the ADK 2012 is the ability to play audio over +a USB connection. This innovation was introduced as an update to Android Open Accessory (AOA) +protocol 2.0 and is available on devices running Android 4.1 (API Level 16) +and higher.

    + +

    The ADK 2012 provides a reference implementation of this functionality for accessory developers. +No software application is required to be installed on the connected Android device, accessory +developers only need to support AOA v2. This implementation demonstrates audio output of 16bit, +44.1kHz stereo PCM source data compressed into a single channel due to the audio hardware available +on the accessory.

    + +

    Using the audio output features provided by the ADK library requires only a few function calls. +The first few calls are in the accessory {@code setup()} routine, which prepare the accessory for +USB connections and audio output, as summarized in the code example below:

    + +
    +ADK L;
    +void setup() {
    +  L.audioInit();
    +  L.usbh_init()
    +  L.usbStart();
    +}
    +
    + +

    For more information about the {@code ADK::audioInit()} function, see the {@code +libraries/ADK/Audio.c} library file. For more information about the {@code ADK::usbh_init()} +function, see the {@code libraries/ADK/Usbh.c} library file.

    + +

    After completing this setup, the {@code loop()} function calls {@code ADK::adkEventProcess()} to +handle audio output and other ADK functions:

    + +
    +void loop(void)
    +{
    +  ...
    +  L.adkEventProcess(); //let the adk framework do its thing
    +  ...
    +}
    +
    + +

    This call executes task queuing for the ADK and as part of the execution process, the task queue +executes {@code usbh_work()} in {@code libraries/ADK/Usbh.c}, which handles audio output requests. +Review the implementation of this function for details. For additional implementation details on +audio output, see the {@code libraries/ADK/accessory.c} library file.

    diff --git a/docs/html/tools/adk/aoa.jd b/docs/html/tools/adk/aoa.jd new file mode 100644 index 000000000000..7884d6ecef82 --- /dev/null +++ b/docs/html/tools/adk/aoa.jd @@ -0,0 +1,186 @@ +page.title=Android Open Accessory Protocol +@jd:body + + + +

    With Android 3.1, the platform introduces Android Open Accessory + support, which allows external USB hardware (an Android USB accessory) to interact with an + Android-powered device in a special accessory mode. When an Android-powered powered device is + in accessory mode, the connected accessory acts as the USB host (powers the bus and enumerates + devices) and the Android-powered device acts as the USB device. Android USB accessories are + specifically designed to attach to Android-powered devices and adhere to a simple protocol + (Android accessory protocol) that allows them to detect Android-powered devices that support + accessory mode. Accessories must also provide 500mA at 5V for charging power. Many previously + released Android-powered devices are only capable of acting as a USB device and cannot initiate + connections with external USB devices. Android Open Accessory support overcomes this limitation + and allows you to build accessories that can interact with an assortment of Android-powered + devices by allowing the accessory to initiate the connection.

    + +

    Note: Accessory mode is ultimately dependent on the device's + hardware and not all devices support accessory mode. Devices that support accessory mode can + be filtered using a <uses-feature> element in your corresponding application's + Android manifest. For more information, see the USB Accessory developer +guide.

    + +

    Implementing the Android Accessory Protocol

    + +

    An Android USB accessory must adhere to Android Accessory Protocol, which defines how + an accessory detects and sets up communication with an Android-powered device. In general, an + accessory should carry out the following steps:

    + +
      +
    1. Wait for and detect connected devices
    2. + +
    3. Determine the device's accessory mode support
    4. + +
    5. Attempt to start the device in accessory mode if needed
    6. + +
    7. Establish communication with the device if it supports the Android accessory protocol
    8. +
    + +

    The following sections go into depth about how to implement these steps.

    + +

    Wait for and detect connected devices

    + +

    Your accessory should have logic to continuously check + for connected Android-powered devices. When a device is connected, your accessory should + determine if the device supports accessory mode.

    + +

    Determine the device's accessory mode support

    + + +

    When an Android-powered device is connected, it can be in one of three states:

    + +
      +
    1. The attached device supports Android accessory mode and is already in accessory mode.
    2. + +
    3. The attached device supports Android accessory mode, but it is not in accessory mode.
    4. + +
    5. The attached device does not support Android accessory mode.
    6. +
    + +

    During the initial connection, the accessory should check the vendor and product IDs of the + connected device's USB device descriptor. The vendor ID should match Google's ID (0x18D1) and the + product ID should be 0x2D00 or 0x2D01 if the device is already in accessory mode (case A). If so, + the accessory can now establish communication with the device through + bulk transfer endpoints with its own communication protocol. There is no need to start the device + in accessory mode.

    + +

    Note: 0x2D00 is reserved for Android-powered devices that + support accessory mode. 0x2D01 is reserved for devices that support accessory mode as well as the + ADB (Android Debug Bridge) protocol, which exposes a second interface with two bulk endpoints for + ADB. You can use these endpoints for debugging the accessory application if you are simulating + the accessory on a computer. In general, do not use this interface unless your accessory is + implementing a passthrough to ADB on the device.

    + +

    If the vendor and product ID do not match, there is no way to distinguish between states b and + c, so the accessory attempts to start the device in accessory mode to figure + out if the device is supported.

    + +

    Attempt to start the device in accessory mode

    + +

    If the vendor and product IDs do not correspond to an Android-powered device in accessory + mode, the accessory cannot discern whether the device supports accessory mode and is not in that + state, or if the device does not support accessory mode at all. This is because devices that + support accessory mode but aren't in it initially report the device's manufacturer vendor ID and + product ID, and not the special Android Open Accessory ones. In either case, the accessory should +try to start + the device into accessory mode to figure out if the device supports it. The following steps + explain how to do this:

    + +
      +
    1. Send a 51 control request ("Get Protocol") to figure out if the device supports the Android + accessory protocol. A non-zero number is returned if the protocol is supported, which + represents the version of the protocol that the device supports (currently, only version 1 + exists). This request is a control request on endpoint 0 with the following characteristics: +
      +requestType:    USB_DIR_IN | USB_TYPE_VENDOR
      +request:        51
      +value:          0
      +index:          0
      +data:           protocol version number (16 bits little endian sent from the device to the
      +accessory)
      +
      +
    2. + +
    3. If the device returns a proper protocol version, send identifying string information to the + device. This information allows the device to figure out an appropriate application for this + accessory and also present the user with a URL if an appropriate application does not exist. + These requests are control requests on endpoint 0 (for each string ID) with the following + characteristics: +
      +requestType:    USB_DIR_OUT | USB_TYPE_VENDOR
      +request:        52
      +value:          0
      +index:          string ID
      +data            zero terminated UTF8 string sent from accessory to device
      +
      + +

      The following string IDs are supported, with a maximum size of 256 bytes for each string + (must be zero terminated with \0).

      +
      +manufacturer name:  0
      +model name:         1
      +description:        2
      +version:            3
      +URI:                4
      +serial number:      5
      +
      +
    4. + +
    5. When the identifying strings are sent, request the device start up in accessory mode. This + request is a control request on endpoint 0 with the following characteristics: +
      +requestType:    USB_DIR_OUT | USB_TYPE_VENDOR
      +request:        53
      +value:          0
      +index:          0
      +data:           none
      +
      +
    6. +
    + +

    After sending the final control request, the connected USB device should re-introduce itself + on the bus in accessory mode and the accessory can re-enumerate the connected devices. The + algorithm jumps back to determining the device's accessory mode support + to check for the vendor and product ID. The vendor ID and product ID of the device will be + different if the device successfully switched to accessory mode and will now correspond to + Google's vendor and product IDs instead of the device manufacturer's IDs. The accessory can now + establish communication with the device.

    + +

    If at any point these steps fail, the device does not support Android accessory mode and the + accessory should wait for the next device to be connected.

    + +

    Establish communication with the device

    + +

    If an Android-powered device in accessory mode is detected, the accessory can query the + device's interface and endpoint descriptors to obtain the bulk endpoints to communicate with the + device. An Android-powered device that has a product ID of 0x2D00 has one interface with two bulk + endpoints for input and output communication. A device with product ID of 0x2D01 has two + interfaces with two bulk endpoints each for input and output communication. The first interface + is for standard communication while the second interface is for ADB communication. To communicate + on an interface, all you need to do is find the first bulk input and output endpoints, set the + device's configuration to a value of 1 with a SET_CONFIGURATION (0x09) device request, then + communicate using the endpoints.

    + diff --git a/docs/html/tools/adk/aoa2.jd b/docs/html/tools/adk/aoa2.jd new file mode 100644 index 000000000000..2a3b2f0e1531 --- /dev/null +++ b/docs/html/tools/adk/aoa2.jd @@ -0,0 +1,227 @@ +page.title=Android Open Accessory Protocol 2.0 +@jd:body + + + +

    This document describes the changes to the Android Open Accessory (AOA) protocol since its +initial release, and is a supplement to the documentation of the first +release of AOA.

    + +

    The Android Open Accessory Protocol 2.0 adds two new features: audio output (from the Android +device to the accessory) and support for the accessory acting as one or more human interface devices +(HID) to the Android device. The Android SDK APIs available to Android application developers +remain unchanged.

    + +

    Detecting Android Open Accessory 2.0 Support

    + +

    In order for an accessory to determine if a connected Android device supports accessories and at +what protocol level, the accessory must send a {@code getProtocol()} command and check the result. +Android devices supporting the initial version of the Android Open Accessory protocol return a +{@code 1}, representing the protocol version number. Devices that support the new features described +in this document must return {@code 2} for the protocol version. Version 2.0 of the protocol is +upwardly compatible, so accessories designed for the original accessory protocol still work +with newer Android devices. The following code from the Android Development Kit +2011 {@code AndroidAccessory} library demonstrates this protocol check:

    + +
    +bool AndroidAccessory::switchDevice(byte addr)
    +{
    +    int protocol = getProtocol(addr);
    +    if (protocol >= 1) {
    +        Serial.print("device supports protocol 1 or higher\n");
    +    } else {
    +        Serial.print("could not read device protocol version\n");
    +        return false;
    +    }
    +
    +    sendString(addr, ACCESSORY_STRING_MANUFACTURER, manufacturer);
    +    sendString(addr, ACCESSORY_STRING_MODEL, model);
    +    sendString(addr, ACCESSORY_STRING_DESCRIPTION, description);
    +    sendString(addr, ACCESSORY_STRING_VERSION, version);
    +    sendString(addr, ACCESSORY_STRING_URI, uri);
    +    sendString(addr, ACCESSORY_STRING_SERIAL, serial);
    +
    +    usb.ctrlReq(addr, 0, USB_SETUP_HOST_TO_DEVICE | USB_SETUP_TYPE_VENDOR |
    +USB_SETUP_RECIPIENT_DEVICE,
    +                ACCESSORY_START, 0, 0, 0, 0, NULL);
    +    return true;
    +}
    +
    + +

    AOA 2.0 includes new USB product IDs, one for each combination of USB interfaces available when +in accessory mode. The possible USB interfaces are:

    + + + +

    In AOA 1.0, there are only two USB product IDs:

    + + + +

    AOA 2.0 adds an optional USB audio interface and, therefore, includes product IDs for the new +combinations of USB interfaces:

    + + + +

    Audio Support

    + +

    AOA 2.0 includes optional support for audio output from an Android device to an accessory. This +version of the protocol supports a standard USB audio class interface that is capable of 2 channel +16-bit PCM audio with a bit rate of 44100 Khz. AOA 2.0 is currently limited to this output mode, but +additional audio modes may be added in the future.

    + +

    To enable the audio support, the accessory must send a new USB control request:

    + +
    +SET_AUDIO_MODE
    +requestType:    USB_DIR_OUT | USB_TYPE_VENDOR
    +request:        58
    +value:          0 for no audio (default),
    +                1 for 2 channel, 16-bit PCM at 44100 KHz
    +index:          0
    +data            none
    +
    + +

    This command must be sent before sending the {@code ACCESSORY_START} command for +entering accessory mode.

    + +

    HID Support

    + +

    AOA 2.0 allows the accessory to register one or more HID devices with +an Android device. This approach reverses the direction of communication for typical USB HID +devices like USB mice and keyboards. Normally, the HID device is a peripheral connected to a USB +host like a personal computer. But in the case of the AOA protocol, the USB host acts as one or more +input devices to a USB peripheral.

    + +

    HID support in AOA 2.0 is simply a proxy for standard HID events. The implementation makes no +assumptions about the content or type of events and merely passes it through to the input system, +so an AOA 2.0 accessory can act as any HID device (mouse, keyboard, game controller, etc.). It +can be used for something as simple as the play/pause button on a media dock, or something as +complicated as a docking station with a mouse and full QWERTY keyboard.

    + +

    The AOA 2.0 protocol adds four new USB control requests to allow the accessory to act as one or +more HID input devices to the Android device. Since HID support is done entirely through +control requests on endpoint zero, no new USB interface is needed to provide this support. The +control requests are as follows:

    + + + +

    The code definitions for these new control requests are as follows:

    + +
    +/* Control request for registering a HID device.
    + * Upon registering, a unique ID is sent by the accessory in the
    + * value parameter. This ID will be used for future commands for
    + * the device
    + *
    + *  requestType:    USB_DIR_OUT | USB_TYPE_VENDOR
    + *  request:        ACCESSORY_REGISTER_HID_DEVICE
    + *  value:          Accessory assigned ID for the HID device
    + *  index:          total length of the HID report descriptor
    + *  data            none
    + */
    +#define ACCESSORY_REGISTER_HID         54
    +
    +/* Control request for unregistering a HID device.
    + *
    + *  requestType:    USB_DIR_OUT | USB_TYPE_VENDOR
    + *  request:        ACCESSORY_REGISTER_HID
    + *  value:          Accessory assigned ID for the HID device
    + *  index:          0
    + *  data            none
    + */
    +#define ACCESSORY_UNREGISTER_HID         55
    +
    +/* Control request for sending the HID report descriptor.
    + * If the HID descriptor is longer than the endpoint zero max packet size,
    + * the descriptor will be sent in multiple ACCESSORY_SET_HID_REPORT_DESC
    + * commands. The data for the descriptor must be sent sequentially
    + * if multiple packets are needed.
    + *
    + *  requestType:    USB_DIR_OUT | USB_TYPE_VENDOR
    + *  request:        ACCESSORY_SET_HID_REPORT_DESC
    + *  value:          Accessory assigned ID for the HID device
    + *  index:          offset of data in descriptor
    + *                      (needed when HID descriptor is too big for one packet)
    + *  data            the HID report descriptor
    + */
    +#define ACCESSORY_SET_HID_REPORT_DESC         56
    +
    +/* Control request for sending HID events.
    + *
    + *  requestType:    USB_DIR_OUT | USB_TYPE_VENDOR
    + *  request:        ACCESSORY_SEND_HID_EVENT
    + *  value:          Accessory assigned ID for the HID device
    + *  index:          0
    + *  data            the HID report for the event
    + */
    +#define ACCESSORY_SEND_HID_EVENT         57
    +
    + +

    Interoperability with AOA 1.0 Features

    + +

    The original AOA protocol provided support for an Android application to +communicate directly with a USB host (accessory) over USB. AOA 2.0 keeps that support, but adds new +features to allow the accessory to communicate with the Android operating system itself +(specifically the audio and input systems). The design of the AOA 2.0 makes it is possible to build +an accessory that also makes use of the new audio and/or HID support in addition to the original +feature set. Simply use the new features described in this document in addition to the original AOA +protocol features.

    + +

    Connecting AOA 2.0 without an Android App

    + +

    It is possible to design an accessory (for example, an audio dock) that uses the new audio and +HID support, but does not need to communicate with an application on the Android device. In that +case, the user would not want to see the dialog prompts related to finding and associating the newly +attached accessory with an Android application that can communicate with it. To prevent these +dialogs from appearing after the device and accessory are connected, the accessory can simply not +send the manufacturer and model names to the Android device. If these strings are not provided to +the Android device, then the accessory is able to make use of the new audio and HID support in AOA +2.0 without the system attempting to find an application to communicate with the accessory. Also, +if these strings are not provided, the accessory USB interface is not present in the Android +device USB configuration after the device enters accessory mode.

    \ No newline at end of file diff --git a/docs/html/tools/adk/index.jd b/docs/html/tools/adk/index.jd new file mode 100644 index 000000000000..4b9b042c91bc --- /dev/null +++ b/docs/html/tools/adk/index.jd @@ -0,0 +1,34 @@ +page.title=Accessory Development Kit +@jd:body + +

    The Accessory Development Kit (ADK) is a reference implementation for hardware manufacturers and +hobbyists to use as a starting point for building accessories for Android. Each ADK release is +provided with source code and hardware specifications to make the process of developing your own +accessories easier. Creating new and alternative hardware based on the ADK is encouraged!

    + +

    Android accessories can be audio docking stations, exercise machines, personal medical testing +devices, weather stations, or any other external hardware device that adds to the functionality of +Android.

    + +

    Accessories use the Android Open Accessory (AOA) protocol to communicate with Android +devices, over USB cable or through a Bluetooth connection. If you are building an accessory for +Android devices, make sure you review the information below to understand about how to implement the +AOA protocol.

    + +

    The following sections provide more information about the Android Accessory Development Kits, how +to use them, and how to get started building your own accessories for Android.

    + +
    +
    ADK 2012 Guide
    +
    Guide to getting started with the ADK released at Google I/O 2012.
    + +
    ADK 2011 Guide
    +
    Guide to getting started with the original ADK, released at Google I/O 2011.
    + +
    Android Open Accessory Protocol
    +
    Guide to implementing the Android Open Accessory Protocol.
    + +
    Android Open Accessory Protocol 2.0
    +
    A description and guide to implementing the extended Android Open Accessory Protocol which + supports audio dock accessories.
    +
    diff --git a/docs/html/tools/tools_toc.cs b/docs/html/tools/tools_toc.cs index 27fbd2de74fc..21d45fa2f0f2 100644 --- a/docs/html/tools/tools_toc.cs +++ b/docs/html/tools/tools_toc.cs @@ -197,12 +197,30 @@ class="en">USB Drivers
  • - + + -- cgit v1.2.3-59-g8ed1b