to top
Android APIs
public class

UsbDeviceConnection

extends Object
java.lang.Object
   ↳ android.hardware.usb.UsbDeviceConnection

Class Overview

This class is used for sending and receiving data and control messages to a USB device. Instances of this class are created by openDevice(UsbDevice).

Summary

Public Methods
int bulkTransfer(UsbEndpoint endpoint, byte[] buffer, int offset, int length, int timeout)
Performs a bulk transaction on the given endpoint.
int bulkTransfer(UsbEndpoint endpoint, byte[] buffer, int length, int timeout)
Performs a bulk transaction on the given endpoint.
boolean claimInterface(UsbInterface intf, boolean force)
Claims exclusive access to a UsbInterface.
void close()
Releases all system resources related to the device.
int controlTransfer(int requestType, int request, int value, int index, byte[] buffer, int length, int timeout)
Performs a control transaction on endpoint zero for this device.
int controlTransfer(int requestType, int request, int value, int index, byte[] buffer, int offset, int length, int timeout)
Performs a control transaction on endpoint zero for this device.
int getFileDescriptor()
Returns the native file descriptor for the device, or -1 if the device is not opened.
byte[] getRawDescriptors()
Returns the raw USB descriptors for the device.
String getSerial()
Returns the serial number for the device.
boolean releaseInterface(UsbInterface intf)
Releases exclusive access to a UsbInterface.
UsbRequest requestWait()
Waits for the result of a queue(ByteBuffer, int) operation Note that this may return requests queued on multiple UsbEndpoints.
[Expand]
Inherited Methods
From class java.lang.Object

Public Methods

public int bulkTransfer (UsbEndpoint endpoint, byte[] buffer, int offset, int length, int timeout)

Added in API level 18

Performs a bulk transaction on the given endpoint. The direction of the transfer is determined by the direction of the endpoint.

Parameters
endpoint the endpoint for this transaction
buffer buffer for data to send or receive
offset the index of the first byte in the buffer to send or receive
length the length of the data to send or receive
timeout in milliseconds
Returns
  • length of data transferred (or zero) for success, or negative value for failure

public int bulkTransfer (UsbEndpoint endpoint, byte[] buffer, int length, int timeout)

Added in API level 12

Performs a bulk transaction on the given endpoint. The direction of the transfer is determined by the direction of the endpoint.

This method transfers data starting from index 0 in the buffer. To specify a different offset, use bulkTransfer(UsbEndpoint, byte[], int, int, int).

Parameters
endpoint the endpoint for this transaction
buffer buffer for data to send or receive
length the length of the data to send or receive
timeout in milliseconds
Returns
  • length of data transferred (or zero) for success, or negative value for failure

public boolean claimInterface (UsbInterface intf, boolean force)

Added in API level 12

Claims exclusive access to a UsbInterface. This must be done before sending or receiving data on any UsbEndpoints belonging to the interface.

Parameters
intf the interface to claim
force true to disconnect kernel driver if necessary
Returns
  • true if the interface was successfully claimed

public void close ()

Added in API level 12

Releases all system resources related to the device. Once the object is closed it cannot be used again. The client must call openDevice(UsbDevice) again to retrieve a new instance to reestablish communication with the device.

public int controlTransfer (int requestType, int request, int value, int index, byte[] buffer, int length, int timeout)

Added in API level 12

Performs a control transaction on endpoint zero for this device. The direction of the transfer is determined by the request type. If requestType & USB_ENDPOINT_DIR_MASK is USB_DIR_OUT, then the transfer is a write, and if it is USB_DIR_IN, then the transfer is a read.

This method transfers data starting from index 0 in the buffer. To specify a different offset, use controlTransfer(int, int, int, int, byte[], int, int, int).

Parameters
requestType request type for this transaction
request request ID for this transaction
value value field for this transaction
index index field for this transaction
buffer buffer for data portion of transaction, or null if no data needs to be sent or received
length the length of the data to send or receive
timeout in milliseconds
Returns
  • length of data transferred (or zero) for success, or negative value for failure

public int controlTransfer (int requestType, int request, int value, int index, byte[] buffer, int offset, int length, int timeout)

Added in API level 18

Performs a control transaction on endpoint zero for this device. The direction of the transfer is determined by the request type. If requestType & USB_ENDPOINT_DIR_MASK is USB_DIR_OUT, then the transfer is a write, and if it is USB_DIR_IN, then the transfer is a read.

Parameters
requestType request type for this transaction
request request ID for this transaction
value value field for this transaction
index index field for this transaction
buffer buffer for data portion of transaction, or null if no data needs to be sent or received
offset the index of the first byte in the buffer to send or receive
length the length of the data to send or receive
timeout in milliseconds
Returns
  • length of data transferred (or zero) for success, or negative value for failure

public int getFileDescriptor ()

Added in API level 12

Returns the native file descriptor for the device, or -1 if the device is not opened. This is intended for passing to native code to access the device.

Returns
  • the native file descriptor

public byte[] getRawDescriptors ()

Added in API level 13

Returns the raw USB descriptors for the device. This can be used to access descriptors not supported directly via the higher level APIs.

Returns
  • raw USB descriptors

public String getSerial ()

Added in API level 12

Returns the serial number for the device. This will return null if the device has not been opened.

Returns
  • the device serial number

public boolean releaseInterface (UsbInterface intf)

Added in API level 12

Releases exclusive access to a UsbInterface.

Returns
  • true if the interface was successfully released

public UsbRequest requestWait ()

Added in API level 12

Waits for the result of a queue(ByteBuffer, int) operation Note that this may return requests queued on multiple UsbEndpoints. When multiple endpoints are in use, getEndpoint() and getClientData() can be useful in determining how to process the result of this function.

Returns
  • a completed USB request, or null if an error occurred