USB development content UsbManager UsbDevice UsbInterface UsbEndPoint UsbDeviceConnection

1. USB drive free device. (The driver part has been implemented in the Android layer or Linux layer) USB Specifies the driver device. (Hardware manufacturer driver, general communication protocol

2 Obtain connected USB devices and select the desired devices

During USB connection, you need to know the vendorId (device vendorId) and productId(device productId) of the USB device. The attributes in the UsbDevice class can be obtained and the required USB devices can be screened. Add meta-data to the Activity or service of the action

The usb_xml file here is a device filter file in the XML folder under the RES folder. Each USB-device is a USB device

3 Connect the USB device

/** * Find the custom device */

public void findUSB(int VENDORID, Int PRODUCTID) {/ / 1) create a usbManager usbManager = (usbManager) mContext. GetSystemService (Context. USB_SERVICE); HashMap<String, UsbDevice> deviceList = usbManager.getDevicelist (); Iterator<UsbDevice> deviceIterator = deviceList.values().iterator(); while (deviceIterator.hasNext()) { UsbDevice device = deviceIterator.next(); Log.i(TAG, "vendorID--" + device.getVendorId() + "ProductId--" + device.getProductId()); if (device.getVendorId() == VENDORID && device.getProductId() == PRODUCTID) { usbDevice = device; USBDevice}}}Copy the code

4 Search for the device interface

A device may have multiple device interfaces. The quantity is obtained from the interfaceCount of the UsbDevice. GetInterface (int position) UsbInterface UsbInterface = usbDevice.getInterface (int Position)

5 Obtain the communication channel of the USB device

Blog.csdn.net/zqixiao_09/…

USB communication channel is a receiving and sending channel between USB devices. Data is sent or received by obtaining the corresponding breakpoint in the channel to achieve control. UsbEndPoint, breakpoint endpoint is located inside USB peripherals, and the source or destination of all communication data is based on these endpoints, which is an addressable FIFO. Each USB has a unique address that may contain up to sixteen endpoints. The host sends data to a concrete endpoint (FIFO) by emitting the device address and endpoint number for each data transfer. The addresses of each endpoint are 0 to 15, and each endpoint address corresponds to one direction. So endpoint 2-in is completely different from endpoint 2-out. Each device has a default two-way control endpoint 0, so there are no endpoints 0-IN and 0-out.

There are many endpoints in a USB device, up to 16. I use 16 S5PV210 processors, which are EP0 to EP15. An EP is essentially a FIFO, a buffer, and we can think of a USB OTG device as a collection of buffers.

There are 16 endpoints, but we usually only use three. A common practice is EP0 for transport configuration and control information, EP1 for IN_EP, and EP2 for OUT_EP.

IN_EP is used to transfer data from Host to Device. OUT_EP is used to pass data from Device to Host. That is, they are used for sending and receiving respectively

There are four transmission modes. Usbendpoint. type Control transmission, batch transmission, interrupt transmission, and synchronous transmission

USB has the above four transfer types. The peripherals during enumeration tell the host which transport type each endpoint supports.

Each transfer requested by a USB device driver to a USB controller driver is called a Transaction.

There are four types of transactions: Bulk Transaction, Control Transaction, Interrupt Transaction, and Isochronous Transaction.

6 Connect the USB device

When you open a USB device, you need to apply for USB connection permission, and the number of times you apply for permission depends on the connected device. That is to say, each USB device needs to be individually authorized (this is understandable, but in many cases, the system USB permissions popup window is the same, the experience is not good when multiple devices, if you want to modify, I think it can only be solved from the system level).

If (usbManager.hasPermission(usbDevice)) {conn = usbManager.openDevice(usbDevice); } else { usbManager.requestPermission(usbDevice, intent); If (usbManager.hasPermission(usbDevice)) {conn = usbManager.openDevice(usbDevice); } else {log. e(TAG, "no permissions "); } } if(usbDeviceConnect.claimInterface(usbInterface,true)){ if(usbDeviceConnect ! Logger (" Open device successfully ")} var name = usbDeviceConnect. Serial Logger (" Device Serial number: $name")}else{logger(" failed to open connection channel ") usBDeviceconnect.close ()}Copy the code

7 Send and read data

private fun sendData(buffer:ByteArray){ if(usbDeviceConnect == null || outPoint == null) return If (usbDeviceConnect. BulkTransfer (outPoint, buffer, buffer size, 0) > = 0) {logger (" sent successfully ")} else {logger (" failure ")}} / / read the data var isReading = true private fun readData(){ var qr = StringBuffer() Thread(Runnable { while (isReading){ var bytes = byteArrayOf() if(usbDeviceConnect.bulkTransfer(inPoint,bytes,inPoint.maxPacketSize,100)>=0){ var stringBuffer = StringBuffer(inPoint.maxPacketSize) } } }).start() }Copy the code