android8host,USB Host

When your Android-powered device is in USB host mode, it acts as the USB host, powers the bus,

and enumerates connected USB devices. USB host mode is supported in Android 3.1 and higher.

API Overview

Before you begin, it is important to understand the classes that you need to work with. The

following table describes the USB host APIs in the

Table 1. USB Host APIs

Class

Description

Allows you to enumerate and communicate with connected USB devices.

Represents a connected USB device and contains methods to access its identifying

information, interfaces, and endpoints.

Represents an interface of a USB device, which defines a set of functionality for the

device. A device can have one or more interfaces on which to communicate on.

Represents an interface endpoint, which is a communication channel for this interface. An

interface can have one or more endpoints, and usually has input and output endpoints for

two-way communication with the device.

Represents a connection to the device, which transfers data on endpoints. This class

allows you to send data back and forth sychronously or asynchronously.

Represents an asynchronous request to communicate with a device through a

Defines USB constants that correspond to definitions in linux/usb/ch9.h of the Linux

kernel.

In most situations, you need to use all of these classes (

Android Manifest Requirements

The following list describes what you need to add to your application's manifest file before

working with the USB host APIs:

Because not all Android-powered devices are guaranteed to support the USB host APIs,

include a element that declares that your application uses

the android.hardware.usb.host feature.

Set the minimum SDK of the application to API Level 12 or higher. The USB host APIs are not

present on earlier API levels.

If you want your application to be notified of an attached USB device, specify an

and element pair for the

android.hardware.usb.action.USB_DEVICE_ATTACHED intent in your main activity. The

element points to an external XML resource file that declares

identifying information about the device that you want to detect.

In the XML resource file, declare elements for the USB

devices that you want to filter. The following list describes the attributes of

. In general, use vendor and product ID if you want to filter

for a specific device and use class, subclass, and protocol if you want to filter for a group

of USB devices, such as mass storage devices or digital cameras. You can specify none or

all of these attributes. Specifying no attributes matches every USB device, so only do this

if your application requires it:

vendor-id

product-id

class

subclass

protocol (device or interface)

Save the resource file in the res/xml/ directory. The resource file name

(without the .xml extension) must be the same as the one you specified in the

element. The format for the XML resource file is in the

example below.

Manifest and resource file examples

The following example shows a sample manifest and its corresponding resource file:

...

...

android:resource="@xml/device_filter" />

In this case, the following resource file should be saved in

res/xml/device_filter.xml and specifies that any USB device with the specified

attributes should be filtered:

Working with Devices

When users connect USB devices to an Android-powered device, the Android system can determine

whether your application is interested in the connected device. If so, you can set up

communication with the device if desired. To do this, your application has to:

Discover connected USB devices by using an intent filter to be notified when the user

connects a USB device or by enumerating USB devices that are already connected.

Ask the user for permission to connect to the USB device, if not already obtained.

Communicate with the USB device by reading and writing data on the appropriate interface

endpoints.

Discovering a device

Your application can discover USB devices by either using an intent filter to be notified when

the user connects a device or by enumerating USB devices that are already connected. Using an

intent filter is useful if you want to be able to have your application automatically detect a

desired device. Enumerating connected USB devices is useful if you want to get a list of all

connected devices or if your application did not filter for an intent.

Using an intent filter

To have your application discover a particular USB device, you can specify an intent filter to

filter for the android.hardware.usb.action.USB_DEVICE_ATTACHED intent. Along with

this intent filter, you need to specify a resource file that specifies properties of the USB

device, such as product and vendor ID. When users connect a device that matches your device

filter, the system presents them with a dialog that asks if they want to start your application.

If users accept, your application automatically has permission to access the device until the

device is disconnected.

The following example shows how to declare the intent filter:

...

android:resource="@xml/device_filter" />

The following example shows how to declare the corresponding resource file that specifies the

USB devices that you're interested in:

In your activity, you can obtain the

UsbDevice device = (UsbDevice) intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);

Enumerating devices

If your application is interested in inspecting all of the USB devices currently connected

while your application is running, it can enumerate devices on the bus. Use the

UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);

...

HashMap deviceList = manager.getDeviceList();

UsbDevice device = deviceList.get("deviceName");

If desired, you can also just obtain an iterator from the hash map and process each device one

by one:

UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);

...

HashMap deviceList = manager.getDeviceList();

Iterator deviceIterator = deviceList.values().iterator();

while(deviceIterator.hasNext()){

UsbDevice device = deviceIterator.next()

//your code

}

Obtaining permission to communicate with a device

Before communicating with the USB device, your applicaton must have permission from your

users.

Note: If your application uses an

intent filter to discover USB devices as they're connected, it automatically receives

permission if the user allows your application to handle the intent. If not, you must request

permission explicitly in your application before connecting to the device.

Explicitly asking for permission might be neccessary in some situations such as when your

application enumerates USB devices that are already connected and then wants to communicate with

one. You must check for permission to access a device before trying to communicate with it. If

not, you will receive a runtime error if the user denied permission to access the device.

To explicitly obtain permission, first create a broadcast receiver. This receiver listens for

the intent that gets broadcast when you call

private static final String ACTION_USB_PERMISSION =

"com.android.example.USB_PERMISSION";

private final BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {

public void onReceive(Context context, Intent intent) {

String action = intent.getAction();

if (ACTION_USB_PERMISSION.equals(action)) {

synchronized (this) {

UsbDevice device = (UsbDevice)intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);

if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {

if(device != null){

//call method to set up device communication

}

}

else {

Log.d(TAG, "permission denied for device " + device);

}

}

}

}

};

To register the broadcast receiver, add this in your onCreate() method in your

activity:

UsbManager mUsbManager = (UsbManager) getSystemService(Context.USB_SERVICE);

private static final String ACTION_USB_PERMISSION =

"com.android.example.USB_PERMISSION";

...

mPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0);

IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);

registerReceiver(mUsbReceiver, filter);

To display the dialog that asks users for permission to connect to the device, call the

UsbDevice device;

...

mUsbManager.requestPermission(device, mPermissionIntent);

When users reply to the dialog, your broadcast receiver receives the intent that contains the

Communicating with a device

Communication with a USB device can be either synchronous or asynchronous. In either case, you

should create a new thread on which to carry out all data transmissions, so you don't block the

UI thread. To properly set up communication with a device, you need to obtain the appropriate

Check a

When you are certain that you want to communicate with the device, find the appropriate

When you find the correct endpoint, open a

Supply the data that you want to transmit on the endpoint with the Processes and

Threads.

The following code snippet is a trivial way to do a synchronous data transfer. Your code

should have more logic to correctly find the correct interface and endpoints to communicate on

and also should do any transferring of data in a different thread than the main UI thread:

private Byte[] bytes

private static int TIMEOUT = 0;

private boolean forceClaim = true;

...

UsbInterface intf = device.getInterface(0);

UsbEndpoint endpoint = intf.getEndpoint(0);

UsbDeviceConnection connection = mUsbManager.openDevice(device);

connection.claimInterface(intf, forceClaim);

connection.bulkTransfer(endpoint, bytes, bytes.length, TIMEOUT); //do in another thread

To send data asynchronously, use the

For more information, see the AdbTest sample, which shows how to do

asynchronous bulk transfers, and the MissleLauncher sample, which

shows how to listen on an interrupt endpoint asynchronously.

Terminating communication with a device

When you are done communicating with a device or if the device was detached, close the

BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {

public void onReceive(Context context, Intent intent) {

String action = intent.getAction();

if (UsbManager.ACTION_USB_DEVICE_DETACHED.equals(action)) {

UsbDevice device = (UsbDevice)intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);

if (device != null) {

// call your method that cleans up and closes communication with the device

}

}

}

};

Creating the broadcast receiver within the application, and not the manifest, allows your

application to only handle detached events while it is running. This way, detached events are

only sent to the application that is currently running and not broadcast to all applications.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
毕业设计,基于SpringBoot+Vue+MySQL开发的公寓报修管理系统,源码+数据库+毕业论文+视频演示 现代经济快节奏发展以及不断完善升级的信息化技术,让传统数据信息的管理升级为软件存储,归纳,集中处理数据信息的管理方式。本公寓报修管理系统就是在这样的大环境下诞生,其可以帮助管理者在短时间内处理完毕庞大的数据信息,使用这种软件工具可以帮助管理人员提高事务处理效率,达到事半功倍的效果。此公寓报修管理系统利用当下成熟完善的Spring Boot框架,使用跨平台的可开发大型商业网站的Java语言,以及最受欢迎的RDBMS应用软件之一的MySQL数据库进行程序开发。公寓报修管理系统有管理员,住户,维修人员。管理员可以管理住户信息和维修人员信息,可以审核维修人员的请假信息,住户可以申请维修,可以对维修结果评价,维修人员负责住户提交的维修信息,也可以请假。公寓报修管理系统的开发根据操作人员需要设计的界面简洁美观,在功能模块布局上跟同类型网站保持一致,程序在实现基本要求功能时,也为数据信息面临的安全问题提供了一些实用的解决方案。可以说该程序在帮助管理者高效率地处理工作事务的同时,也实现了数据信息的整体化,规范化与自动化。 关键词:公寓报修管理系统;Spring Boot框架;MySQL;自动化;VUE
毕业设计,基于SpringBoot+Vue+MySQL开发的社区医院管理系统,源码+数据库+毕业论文+视频演示 信息数据从传统到当代,是一直在变革当中,突如其来的互联网让传统的信息管理看到了革命性的曙光,因为传统信息管理从时效性,还是安全性,还是可操作性等各个方面来讲,遇到了互联网时代才发现能补上自古以来的短板,有效的提升管理的效率和业务水平。传统的管理模式,时间越久管理的内容越多,也需要更多的人来对数据进行整理,并且数据的汇总查询方面效率也是极其的低下,并且数据安全方面永远不会保证安全性能。结合数据内容管理的种种缺点,在互联网时代都可以得到有效的补充。结合先进的互联网技术,开发符合需求的软件,让数据内容管理不管是从录入的及时性,查看的及时性还是汇总分析的及时性,都能让正确率达到最高,管理更加的科学和便捷。本次开发的社区医院管理系统实现了病例信息、字典表、家庭医生、健康档案、就诊信息、前台、药品、用户、用户、用户表等功能。系统用到了关系型数据库中王者MySql作为系统的数据库,有效的对数据进行安全的存储,有效的备份,对数据可靠性方面得到了保证。并且程序也具备程序需求的所有功能,使得操作性还是安全性都大大提高,让社区医院管理系统更能从理念走到现实,确确实实的让人们提升信息处理效率。 关键字:社区医院管理系统;信息管理,时效性,安全性,MySql
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值