Android connectivity-USB slave mode (III)

Source: Internet
Author: User

Work with attachments

When a user connects a USB attachment to an Android device, the android system can determine whether your application is interested in the attachment. If you are interested, you can establish a communication with the desired attachment. Here is what your application is going to do:

1. You can use the intent filter to filter attachment device access events or list connected attachment devices to find the corresponding attachment devices;

2. If it is not a known attachment device, ask the user whether to allow communication with the attachment device;

3. Read and Write the data on the corresponding interface endpoint to communicate with the attachment device.

Attachment device found

Your application can discover the desired attachment device by listing the connected attachment device by means of the intent notification when the user accesses the attachment device. If you want your application to automatically detect the desired attachment device, use the intent filter. If you want to obtain a list of all connected attachment devices, or your application does not filter specific intent devices, the list method is useful.

Use intent Filter

You can specify an android. Hardware. USB. Action. usb_accessory_attached intent filter so that your application can discover special USB attachment devices. Together with the intent filter, you also need to specify a resource file that contains the properties of the USB attachment device. These attributes include the manufacturer, mode, and version.

The following example shows how to declare an intent filter:

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

The following example shows the declaration of the corresponding resource file:

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

<Resources>


<USB-accessory manufacturer = "Google, Inc." model = "demokit" version = "1.0"/>

</Resources>

In your activity, you can obtain the USB accessory object representing the connected attachment from the intent object.

When the add-on class library is used:

Usbaccessory accessory
= Usbmanager. getaccessory (intent );

Platform API usage:

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

List attachment devices

When an application is running, you can ask your application to list all identified attachment devices.

Use the getaccessorylist () method to obtain all connected USB attachment devices:

Usbmanager manager = (usbmanager) getsystemservice (context. usb_service );

Usbaccessory [] accessorylist = manager. getacccessorylist ();

Note: currently, only one attachment device can be connected at a time. However, in the future, this API will be designed to support multiple attachment devices.

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.