All Categories


Android USB Communication Learn USB Host Device Communication Practical Application

Android4.0 after, OTG began to spread, we can expand the U disk, mouse and other equipment for Andrews equipment. Then you need to understand the USB data exchange.

USB device is divided into the host device and Slave slave device, we usually understand the OTG is Andrew as 'host': through the Andrews equipment as the master device for inter-device communication. Andrews also encapsulated the API so that we do not have to study the USB protocol can be a simple device communication.

First USB Host needs 3.1 or more version of the support, that is, minSdkVersion

12. Then we need a device, an OTG support Andrews mobile phone, of course, also need an OTG data cable.

Then we started backup computer to usb drive understand the API:

UsbManager Manager allows you to enumerate USB for subsequent use

UsbInterface device communication interface

UsbEndpoint interface access points, divided into one-way and two-way, most device requests and responses share the two ports, some devices share the same port. In my test, for example, using the iris recognition device, is the shared port, send and receive can not be used at the same time.

On the device we need to know Vid and Pid, ​​respectively VendorID (manufacturer ID) and ProductID (product ID), it can distinguish the type of equipment, so that you can filter out the required equipment after enumeration.

UsbDevice gets the USB device

UsbDeviceConnection gets the connection to the device

The communication flow is as follows: Instance Manager Enumeration Device Check / Get Permissions Get Interface Get Endpoint On Device Connection Send Data Receive Data Close Connection Release Interface

In order to achieve device communication, I need to design a set of ideas:

Communication is the premise: because in the Andrews platform, but also time-consuming operation, the thread is essential; must be sure to be familiar with the equipment to be communicated, its PID, VID, receive instructions, message format, single packet maximum amount of data, Size end

In order to communicate, we need to establish a connection, the general steps are: insert the device è enumerate the device è get the interface è get the endpoint è establish the connection

Get the connection object, you can send: through bulkTransfer () and controlTransfer () these two methods, bulk transfer and control delivery. As the name implies, the control transfer is located in the control port to send, generally used to set the device parameter information can also be used to pass simple data. More complex information, recommend the previous method, their return value for the length of the response message, -1 means that the message failed to send.

Receive data: on the data reception, there is synchronization and asynchronous difference, synchronization is well understood, send a response to send a response. We talk about asynchronous, on the asynchronous reception, Google official recommended practice is UsbRequest to establish asynchronous requests, but it is difficult to control, on the business logic after listening is not very perfect; so I use a simple and rude way - Sync block data transfer + single case in the thread to continue to monitor, and use HandlerMessage delivery.

============================== == = = = = = = = = = = = = = = = = = = = = === === === === ===========================

Then we need to write a separate demo, to achieve the plan:

Use the USB Host to declare that the application requires the feature and requires the Android version to be higher than 12, which is the minimum version that supports OTG transport requirements; if you need to listen for the device's self-registered broadcast recipient and add permissions to the insert logic Ask, pull out at the same time release the resources and turn off the device connection, insert pull out intention:

android.hardware.usb.action.USB_DEVICE_ATTACHED and android.hardware.usb.action.USB_DEVICE_GRANTED

Need to be configured in the manifest file to join:

Then we set up the connection in the order of initialization:

Get the singleton mode USBManager (not recommended for use, the service will affect the UI thread):

Enumerate the device

Get the device interface

Get the communication endpoint

Open the device connection

Send, need to encapsulate the information in byte array form:

Send is divided into two ways, block transfer and control transmission, the format is:

mDeviceConnection.bulkTransfer (epBulkOut, buffer, buffer.length, timeout);

mDeviceConnection.controlTransfer (requestType,

request, value,

index, buffer,

length, timeout);

Receive, start the thread in singleton form Receive the upper byte array:

Initialize the method

About the Author


My name is Jett Barone. I life in Trebetherick (United Kingdom).

In the event you loved this informative article and you would like to receive more info regarding backup computer to usb drive generously visit the page.

Most Viewed - All Categories