Skip to content

Most visited

Recently visited

navigation

UsbDeviceConnection

public class UsbDeviceConnection
extends Object

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


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 offset, 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 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(long timeout)

Waits for the result of a queue(ByteBuffer) operation

Note that this may return requests queued on multiple UsbEndpoints.

UsbRequest requestWait()

Waits for the result of a queue(ByteBuffer) operation

Note that this may return requests queued on multiple UsbEndpoints.

boolean setConfiguration(UsbConfiguration configuration)

Sets the device's current UsbConfiguration.

boolean setInterface(UsbInterface intf)

Sets the current UsbInterface.

Protected methods

void finalize()

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.

Inherited methods

From class java.lang.Object

Public methods

bulkTransfer

added in API level 18
int bulkTransfer (UsbEndpoint endpoint, 
                byte[] buffer, 
                int offset, 
                int length, 
                int timeout)

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

Parameters
endpoint UsbEndpoint: the endpoint for this transaction

buffer byte: buffer for data to send or receive

offset int: the index of the first byte in the buffer to send or receive

length int: the length of the data to send or receive

timeout int: in milliseconds, 0 is infinite

Returns
int length of data transferred (or zero) for success, or negative value for failure

bulkTransfer

added in API level 12
int bulkTransfer (UsbEndpoint endpoint, 
                byte[] buffer, 
                int length, 
                int timeout)

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 UsbEndpoint: the endpoint for this transaction

buffer byte: buffer for data to send or receive; can be null to wait for next transaction without reading data

length int: the length of the data to send or receive

timeout int: in milliseconds, 0 is infinite

Returns
int length of data transferred (or zero) for success, or negative value for failure

claimInterface

added in API level 12
boolean claimInterface (UsbInterface intf, 
                boolean force)

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

Parameters
intf UsbInterface: the interface to claim

force boolean: true to disconnect kernel driver if necessary

Returns
boolean true if the interface was successfully claimed

close

added in API level 12
void close ()

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.

controlTransfer

added in API level 18
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. 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 int: request type for this transaction

request int: request ID for this transaction

value int: value field for this transaction

index int: index field for this transaction

buffer byte: buffer for data portion of transaction, or null if no data needs to be sent or received

offset int: the index of the first byte in the buffer to send or receive

length int: the length of the data to send or receive

timeout int: in milliseconds

Returns
int length of data transferred (or zero) for success, or negative value for failure

controlTransfer

added in API level 12
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. 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 int: request type for this transaction

request int: request ID for this transaction

value int: value field for this transaction

index int: index field for this transaction

buffer byte: buffer for data portion of transaction, or null if no data needs to be sent or received

length int: the length of the data to send or receive

timeout int: in milliseconds

Returns
int length of data transferred (or zero) for success, or negative value for failure

getFileDescriptor

added in API level 12
int getFileDescriptor ()

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
int the native file descriptor

getRawDescriptors

added in API level 13
byte[] getRawDescriptors ()

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

Returns
byte[] raw USB descriptors

getSerial

added in API level 12
String getSerial ()

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

Returns
String the device serial number

releaseInterface

added in API level 12
boolean releaseInterface (UsbInterface intf)

Releases exclusive access to a UsbInterface.

Parameters
intf UsbInterface

Returns
boolean true if the interface was successfully released

requestWait

UsbRequest requestWait (long timeout)

Waits for the result of a queue(ByteBuffer) 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.

Android processes UsbRequests asynchronously. Hence it is not guaranteed that requestWait(0) returns a request that has been queued right before even if the request could have been processed immediately.

Parameters
timeout long: timeout in milliseconds. If 0 this method does not wait.

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

Throws
BufferOverflowException if the number of bytes read or written is more than the limit of the request's buffer. The number of bytes is determined by the length parameter of queue(ByteBuffer, int)
TimeoutException if no request was received in timeout milliseconds.

requestWait

added in API level 12
UsbRequest requestWait ()

Waits for the result of a queue(ByteBuffer) 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
UsbRequest a completed USB request, or null if an error occurred

Throws
IllegalArgumentException Before API Build.VERSION_CODES.O: if the number of bytes read or written is more than the limit of the request's buffer. The number of bytes is determined by the length parameter of queue(ByteBuffer, int)
BufferOverflowException In API Build.VERSION_CODES.O and after: if the number of bytes read or written is more than the limit of the request's buffer. The number of bytes is determined by the length parameter of queue(ByteBuffer, int)

setConfiguration

added in API level 21
boolean setConfiguration (UsbConfiguration configuration)

Sets the device's current UsbConfiguration.

Parameters
configuration UsbConfiguration

Returns
boolean true if the configuration was successfully set

setInterface

added in API level 21
boolean setInterface (UsbInterface intf)

Sets the current UsbInterface. Used to select between two interfaces with the same ID but different alternate setting.

Parameters
intf UsbInterface

Returns
boolean true if the interface was successfully selected

Protected methods

finalize

added in API level 12
void finalize ()

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object. A subclass overrides the finalize method to dispose of system resources or to perform other cleanup.

The general contract of finalize is that it is invoked if and when the Java™ virtual machine has determined that there is no longer any means by which this object can be accessed by any thread that has not yet died, except as a result of an action taken by the finalization of some other object or class which is ready to be finalized. The finalize method may take any action, including making this object available again to other threads; the usual purpose of finalize, however, is to perform cleanup actions before the object is irrevocably discarded. For example, the finalize method for an object that represents an input/output connection might perform explicit I/O transactions to break the connection before the object is permanently discarded.

The finalize method of class Object performs no special action; it simply returns normally. Subclasses of Object may override this definition.

The Java programming language does not guarantee which thread will invoke the finalize method for any given object. It is guaranteed, however, that the thread that invokes finalize will not be holding any user-visible synchronization locks when finalize is invoked. If an uncaught exception is thrown by the finalize method, the exception is ignored and finalization of that object terminates.

After the finalize method has been invoked for an object, no further action is taken until the Java virtual machine has again determined that there is no longer any means by which this object can be accessed by any thread that has not yet died, including possible actions by other objects or classes which are ready to be finalized, at which point the object may be discarded.

The finalize method is never invoked more than once by a Java virtual machine for any given object.

Any exception thrown by the finalize method causes the finalization of this object to be halted, but is otherwise ignored.

Throws
Throwable
This site uses cookies to store your preferences for site-specific language and display options.

Get the latest Android developer news and tips that will help you find success on Google Play.

* Required Fields

Hooray!

Browse this site in ?

You requested a page in , but your language preference for this site is .

Would you like to change your language preference and browse this site in ? If you want to change your language preference later, use the language menu at the bottom of each page.

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.

Take a one-minute survey?
Help us improve Android tools and documentation.