Android connectivity-USB Host Mode (1)

Source: Internet
Author: User

This article translated from: http://developer.android.com/guide/topics/connectivity/usb/host.html

When your Android device is in USB host mode, it powers the slave device as a USB host and lists connected USB devices. The USB Host Mode is supported after android3.1.

API Overview

Before starting, it is important to understand the classes required in the work. The following table describes the USB Host Mode APIs in the Android. Hardware. USB package.

Table 1. USB Host Mode API

Class

Introduction

USB Manager

Lists connected USB devices and communicates with them.

USB device

Represents a connected USB device and contains methods for identifying information, interfaces, and endpoints.

USB Interface

Represents an interface of a USB device, which defines a set of features of the device. A device can have one or more interfaces for communication.

USB endpoint

Represents an interface endpoint, which is a channel for communication with interfaces. An interface can have one or more endpoints. Usually there are input and output endpoints for duplex communication with devices.

Usbdeviceconnection

Represents a device connection, which transmits data over the endpoint. This class is used to send data back and forth synchronously or asynchronously between two connected devices.

Usbrequest

Represents an asynchronous request that communicates with a device through a USB deviceconnection object.

Usbconstants

Defines the USB constant corresponding to the Linux/USB/ch9.h file definition in the Linux kernel.

In most cases, you need to use all these classes when communicating with a USB device (if you are using asynchronous communication, you only need to use the usbrequest class ). Generally, you need to use a USB Manager object to obtain the desired USB device object. When you have this USB device object, you need to find the corresponding USB interface object and the USB endpoint object that communicates based on this interface. Once the correct endpoint is obtained, you can open the USB deviceconnect object to communicate with the USB device.

Android list requirements

Before using the USB Host Mode API, you need to add the following content to your application list:

1. because not all Android devices support the USB host mode, you must include the <uses-feature> element in your application declaration to declare that your application uses android. hardware. USB. host function.

2. Set the minimum SDK of the application to API Level 12 or higher. The USB Host Mode API does not exist in earlier API versions.

3. if you want your application to receive notifications when accessing a USB device, you must specify android in your main activity. hardware. USB. action. the <intent-filter> and <meta-data> elements of the usb_device_attached type. The <meta-data> element points to an external XML resource file, which declares the identification information of the device to be detected.

In this XML resource file, use the <USB-device> element to declare the USB device you want to filter. The properties of the <USB-device> element are listed below. Generally, you can specify the devices you want to filter by using the vendor-ID and product-ID, and use the class, subclass, and Protocol to specify the USB device group you want to filter, such as large-capacity storage devices or digital cameras. You can choose not to specify any or all attributes. If no attribute is specified, it will match with all USB devices. If the application needs to, you can do this:

A. Vendor-ID

B. Product-ID

C. Class

D. Subclass

E. Protocol (device or interface)

Save the resource file in the Res/XML directory. The resource file name (excluding the. xml extension) must be the same as the name specified in the <meta-data> element. For the XML resource file format, see the following example.

List and resource file examples

The following is a simple configuration file and its corresponding resource file:

<Manifest...>


<Uses-feature Android: Name = "android. Hardware. USB. Host"/>


<Uses-SDK Android: minsdkversion = "12"/>


...


<Application>


<Activity...>


...


<Intent-filter>


<Action Android: Name = "android. Hardware. USB. Action. usb_device_attached"/>


</Intent-filter>

 


<Meta-data Android: Name = "android. Hardware. USB. Action. usb_device_attached"


Android: Resource = "@ XML/device_filter"/>


</Activity>


</Application>

</Manifest>

In this instance, the following resource files should be saved in RES/XML/device_filter.xml, and all attributes used to filter USB devices are specified:

<? XML version = "1.0" encoding = "UTF-8"?>

<Resources>


<USB-device vendor-id = "1234" product-id = "5678" class = "255" subclass = "66" protocol = "1"/>

</Resources>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.