Android connectivity-USB slave mode (2)

Source: Internet
Author: User

API Overview

Because the additional class library is an encapsulation of the Framework API, the classes supporting the USB slave mode are similar. Even if you are using this additional class library, you can also use the Android. Hardware. USB reference document.

Note:However, there is a usage difference between the additional Class Library and the Framework API. You should pay attention to it.

The following table describes the categories that support APIs in the USB slave mode:

Class

Introduction

UsbManager

Allows you to list connected USB attachments and communicate with them.

UsbAccessory

Represents a USB attachment and contains methods for accessing the attachment ID information.

Differences between the add-on class library and platform APIs

There are two differences between using the Google APIs add-on class library and platform API.

If you use the add-on class library, you must obtain the USB Manager object in the following ways:

USB Manager
= Usbmanager. getinstance (this );,

Otherwise, you must obtain the USB Manager object in the following ways:

USB Manager
= (Usbmanager) getsystemservice (context. usb_service );

When filtering a connected attachment using the intent filter, the intent object passed to your application contains the usbaccessory object. If you are using the add-on class library, you must use the following methods to obtain the USB accessory object:

Usbaccessory accessory
= Usbmanager. getaccessory (intent );

Otherwise, you must use the following method to obtain the USB accessory object:

Usbaccessory accessory
= (Usbaccessory) intent. getparcelableextra (usbmanager. extra_accessory );

Android list requirements

The following lists the content that needs to be added to your application list file before using the USB slave mode API:

1. Because not all Android devices support the USB slave mode API, you must declare the Android. Hardware. USB. Accessory function in the <uses-feature> element;

2. If you are using the add-on class library, add the <uses-library> element to specify the com. Android. Future. USB. Accessory class library to be used;

3. if you are using the add-on class library, you need to set the minimum SDK version of the application to API level 10, if you are using Android. hardware. the minimum version is API level 12.

4. if you want to get a notification of connecting USB attachments, you must specify Android for the <intent-filter> and <meta-data> elements in your main activity. hardware. USB. action. usb_accessory_attached intent. The <meta-data> element points to an external XML resource file, which indicates the identifier information about the attachment you want to detect.

In this XML resource file, use the <USB-accessory> element to declare the attachment you want to filter. Each <USB-accessory> element has the following attributes:

Manufacturer

Model

Version

The resource file is saved 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. The following example shows the format of the XML resource file:

List and resource file examples

The following example shows the list and its corresponding resource files:

<Manifest...>


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



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


...


<Application>


<Uses-library Android: Name = "com. Android. Future. USB. Accessory"/>


<Activity...>


...


<Intent-filter>


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


</Intent-filter>

 


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


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


</Activity>


</Application>

</Manifest>

In this case, the following resource files should be saved in the Res/XML/accessory_filter.xml file, and the pattern, manufacturer, and version number of the attachment to be filtered are specified. USB attachments send these attributes to Android devices:

<?xml version="1.0" encoding="utf-8"?>
 
<resources>
    <usb-accessorymodel="DemoKit"manufacturer="Google"version="1.0"/>

</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.