# UsbDeviceConnection

Added in[API level 12](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  
Summary:[Methods](https://developer.android.com/reference/android/hardware/usb/UsbDeviceConnection#pubmethods)\|[Protected Methods](https://developer.android.com/reference/android/hardware/usb/UsbDeviceConnection#promethods)\|[Inherited Methods](https://developer.android.com/reference/android/hardware/usb/UsbDeviceConnection#inhmethods)  

# UsbDeviceConnection

*** ** * ** ***

[Kotlin](https://developer.android.com/reference/kotlin/android/hardware/usb/UsbDeviceConnection "View this page in Kotlin")\|Java

`
public


class
UsbDeviceConnection
`  
`

extends `[Object](https://developer.android.com/reference/java/lang/Object)`


``


`

|---|------------------------------------------|
| [java.lang.Object](https://developer.android.com/reference/java/lang/Object) ||
| ↳ | android.hardware.usb.UsbDeviceConnection |

<br />

*** ** * ** ***

This class is used for sending and receiving data and control messages to a USB device. Instances of this class are created by[UsbManager.openDevice](https://developer.android.com/reference/android/hardware/usb/UsbManager#openDevice(android.hardware.usb.UsbDevice)).

## Summary

|                                                                                                                                                                                                                                                                 ### Public methods                                                                                                                                                                                                                                                                  ||
|------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| ` int`                                                                                   | ` `[bulkTransfer](https://developer.android.com/reference/android/hardware/usb/UsbDeviceConnection#bulkTransfer(android.hardware.usb.UsbEndpoint,%20byte[],%20int,%20int,%20int))`(`[UsbEndpoint](https://developer.android.com/reference/android/hardware/usb/UsbEndpoint)` endpoint, byte[] buffer, int offset, int length, int timeout) ` Performs a bulk transaction on the given endpoint.                                                           |
| ` int`                                                                                   | ` `[bulkTransfer](https://developer.android.com/reference/android/hardware/usb/UsbDeviceConnection#bulkTransfer(android.hardware.usb.UsbEndpoint,%20byte[],%20int,%20int))`(`[UsbEndpoint](https://developer.android.com/reference/android/hardware/usb/UsbEndpoint)` endpoint, byte[] buffer, int length, int timeout) ` Performs a bulk transaction on the given endpoint.                                                                              |
| ` boolean`                                                                               | ` `[claimInterface](https://developer.android.com/reference/android/hardware/usb/UsbDeviceConnection#claimInterface(android.hardware.usb.UsbInterface,%20boolean))`(`[UsbInterface](https://developer.android.com/reference/android/hardware/usb/UsbInterface)` intf, boolean force) ` Claims exclusive access to a[UsbInterface](https://developer.android.com/reference/android/hardware/usb/UsbInterface).                                             |
| ` void`                                                                                  | ` `[close](https://developer.android.com/reference/android/hardware/usb/UsbDeviceConnection#close())`() ` Releases all system resources related to the device.                                                                                                                                                                                                                                                                                            |
| ` int`                                                                                   | ` `[controlTransfer](https://developer.android.com/reference/android/hardware/usb/UsbDeviceConnection#controlTransfer(int,%20int,%20int,%20int,%20byte[],%20int,%20int,%20int))`(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](https://developer.android.com/reference/android/hardware/usb/UsbDeviceConnection#controlTransfer(int,%20int,%20int,%20int,%20byte[],%20int,%20int))`(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](https://developer.android.com/reference/android/hardware/usb/UsbDeviceConnection#getFileDescriptor())`() ` Returns the native file descriptor for the device, or -1 if the device is not opened.                                                                                                                                                                                                                                   |
| ` byte[]`                                                                                | ` `[getRawDescriptors](https://developer.android.com/reference/android/hardware/usb/UsbDeviceConnection#getRawDescriptors())`() ` Returns the raw USB descriptors for the device.                                                                                                                                                                                                                                                                         |
| ` `[String](https://developer.android.com/reference/java/lang/String)                    | ` `[getSerial](https://developer.android.com/reference/android/hardware/usb/UsbDeviceConnection#getSerial())`() ` Returns the serial number for the device.                                                                                                                                                                                                                                                                                               |
| ` boolean`                                                                               | ` `[releaseInterface](https://developer.android.com/reference/android/hardware/usb/UsbDeviceConnection#releaseInterface(android.hardware.usb.UsbInterface))`(`[UsbInterface](https://developer.android.com/reference/android/hardware/usb/UsbInterface)` intf) ` Releases exclusive access to a[UsbInterface](https://developer.android.com/reference/android/hardware/usb/UsbInterface).                                                                 |
| ` `[UsbRequest](https://developer.android.com/reference/android/hardware/usb/UsbRequest) | ` `[requestWait](https://developer.android.com/reference/android/hardware/usb/UsbDeviceConnection#requestWait(long))`(long timeout) ` Waits for the result of a[UsbRequest.queue(ByteBuffer)](https://developer.android.com/reference/android/hardware/usb/UsbRequest#queue(java.nio.ByteBuffer))operation Note that this may return requests queued on multiple[UsbEndpoint](https://developer.android.com/reference/android/hardware/usb/UsbEndpoint)s. |
| ` `[UsbRequest](https://developer.android.com/reference/android/hardware/usb/UsbRequest) | ` `[requestWait](https://developer.android.com/reference/android/hardware/usb/UsbDeviceConnection#requestWait())`() ` Waits for the result of a[UsbRequest.queue(ByteBuffer)](https://developer.android.com/reference/android/hardware/usb/UsbRequest#queue(java.nio.ByteBuffer))operation Note that this may return requests queued on multiple[UsbEndpoint](https://developer.android.com/reference/android/hardware/usb/UsbEndpoint)s.                 |
| ` boolean`                                                                               | ` `[setConfiguration](https://developer.android.com/reference/android/hardware/usb/UsbDeviceConnection#setConfiguration(android.hardware.usb.UsbConfiguration))`(`[UsbConfiguration](https://developer.android.com/reference/android/hardware/usb/UsbConfiguration)` configuration) ` Sets the device's current[UsbConfiguration](https://developer.android.com/reference/android/hardware/usb/UsbConfiguration).                                         |
| ` boolean`                                                                               | ` `[setInterface](https://developer.android.com/reference/android/hardware/usb/UsbDeviceConnection#setInterface(android.hardware.usb.UsbInterface))`(`[UsbInterface](https://developer.android.com/reference/android/hardware/usb/UsbInterface)` intf) ` Sets the current[UsbInterface](https://developer.android.com/reference/android/hardware/usb/UsbInterface).                                                                                       |

|                                                                                                                   ### Protected methods                                                                                                                   ||
|---------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| ` void` | ` `[finalize](https://developer.android.com/reference/android/hardware/usb/UsbDeviceConnection#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](https://developer.android.com/reference/java/lang/Object)` ` |--------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | ` `[Object](https://developer.android.com/reference/java/lang/Object)          | ` `[clone](https://developer.android.com/reference/java/lang/Object#clone())`() ` Creates and returns a copy of this object.                                                                                                                                                      | | ` boolean`                                                                     | ` `[equals](https://developer.android.com/reference/java/lang/Object#equals(java.lang.Object))`(`[Object](https://developer.android.com/reference/java/lang/Object)` obj) ` Indicates whether some other object is "equal to" this one.                                           | | ` void`                                                                        | ` `[finalize](https://developer.android.com/reference/java/lang/Object#finalize())`() ` Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.                                                          | | ` final `[Class](https://developer.android.com/reference/java/lang/Class)`<?>` | ` `[getClass](https://developer.android.com/reference/java/lang/Object#getClass())`() ` Returns the runtime class of this`Object`.                                                                                                                                                | | ` int`                                                                         | ` `[hashCode](https://developer.android.com/reference/java/lang/Object#hashCode())`() ` Returns a hash code value for the object.                                                                                                                                                 | | ` final void`                                                                  | ` `[notify](https://developer.android.com/reference/java/lang/Object#notify())`() ` Wakes up a single thread that is waiting on this object's monitor.                                                                                                                            | | ` final void`                                                                  | ` `[notifyAll](https://developer.android.com/reference/java/lang/Object#notifyAll())`() ` Wakes up all threads that are waiting on this object's monitor.                                                                                                                         | | ` `[String](https://developer.android.com/reference/java/lang/String)          | ` `[toString](https://developer.android.com/reference/java/lang/Object#toString())`() ` Returns a string representation of the object.                                                                                                                                            | | ` final void`                                                                  | ` `[wait](https://developer.android.com/reference/java/lang/Object#wait(long,%20int))`(long timeoutMillis, int nanos) ` Causes the current thread to wait until it is awakened, typically by being*notified* or*interrupted*, or until a certain amount of real time has elapsed. | | ` final void`                                                                  | ` `[wait](https://developer.android.com/reference/java/lang/Object#wait(long))`(long timeoutMillis) ` Causes the current thread to wait until it is awakened, typically by being*notified* or*interrupted*, or until a certain amount of real time has elapsed.                   | | ` final void`                                                                  | ` `[wait](https://developer.android.com/reference/java/lang/Object#wait())`() ` Causes the current thread to wait until it is awakened, typically by being*notified* or*interrupted*.                                                                                             | ||

## Public methods

### bulkTransfer

Added in[API level 18](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public 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.

<br />

|                                                                                                                                                                                                                            Parameters                                                                                                                                                                                                                             ||
|------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `endpoint` | `UsbEndpoint`: the endpoint for this transaction <br />                                                                                                                                                                                                                                                                                                                                                                                               |
| `buffer`   | `byte`: buffer for data to send or receive <br />                                                                                                                                                                                                                                                                                                                                                                                                     |
| `offset`   | `int`: the index of the first byte in the buffer to send or receive <br />                                                                                                                                                                                                                                                                                                                                                                            |
| `length`   | `int`: the length of the data to send or receive. Before[android.os.Build.VERSION_CODES.P Build.VERSION_CODES.P](https://developer.android.com/reference/android/os/Build.VERSION_CODES#P), a value larger than 16384 bytes would be truncated down to 16384. In API[android.os.Build.VERSION_CODES.P Build.VERSION_CODES.P](https://developer.android.com/reference/android/os/Build.VERSION_CODES#P)and after, any value of length is valid. <br /> |
| `timeout`  | `int`: in milliseconds, 0 is infinite <br />                                                                                                                                                                                                                                                                                                                                                                                                          |

|                                            Returns                                            ||
|-------|----------------------------------------------------------------------------------------|
| `int` | length of data transferred (or zero) for success, or negative value for failure <br /> |

### bulkTransfer

Added in[API level 12](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public 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(android.hardware.usb.UsbEndpoint, byte[], int, int, int)](https://developer.android.com/reference/android/hardware/usb/UsbDeviceConnection#bulkTransfer(android.hardware.usb.UsbEndpoint,%20byte[],%20int,%20int,%20int)).

<br />

<br />

|                                                                                                                                                                                                                            Parameters                                                                                                                                                                                                                             ||
|------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `endpoint` | `UsbEndpoint`: the endpoint for this transaction <br />                                                                                                                                                                                                                                                                                                                                                                                               |
| `buffer`   | `byte`: buffer for data to send or receive; can be`null`to wait for next transaction without reading data <br />                                                                                                                                                                                                                                                                                                                                      |
| `length`   | `int`: the length of the data to send or receive. Before[android.os.Build.VERSION_CODES.P Build.VERSION_CODES.P](https://developer.android.com/reference/android/os/Build.VERSION_CODES#P), a value larger than 16384 bytes would be truncated down to 16384. In API[android.os.Build.VERSION_CODES.P Build.VERSION_CODES.P](https://developer.android.com/reference/android/os/Build.VERSION_CODES#P)and after, any value of length is valid. <br /> |
| `timeout`  | `int`: in milliseconds, 0 is infinite <br />                                                                                                                                                                                                                                                                                                                                                                                                          |

|                                            Returns                                            ||
|-------|----------------------------------------------------------------------------------------|
| `int` | length of data transferred (or zero) for success, or negative value for failure <br /> |

### claimInterface

Added in[API level 12](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public boolean claimInterface (UsbInterface intf, 
                boolean force)
```

Claims exclusive access to a[UsbInterface](https://developer.android.com/reference/android/hardware/usb/UsbInterface). This must be done before sending or receiving data on any[UsbEndpoint](https://developer.android.com/reference/android/hardware/usb/UsbEndpoint)s belonging to the interface.

<br />

|                                Parameters                                ||
|---------|-----------------------------------------------------------------|
| `intf`  | `UsbInterface`: the interface to claim <br />                   |
| `force` | `boolean`: true to disconnect kernel driver if necessary <br /> |

|                             Returns                              ||
|-----------|-------------------------------------------------------|
| `boolean` | true if the interface was successfully claimed <br /> |

### close

Added in[API level 12](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public void close ()
```

Releases all system resources related to the device. Once the object is closed it cannot be used again. The client must call[UsbManager.openDevice](https://developer.android.com/reference/android/hardware/usb/UsbManager#openDevice(android.hardware.usb.UsbDevice))again to retrieve a new instance to reestablish communication with the device.

<br />

### controlTransfer

Added in[API level 18](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public 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 \&[UsbConstants.USB_ENDPOINT_DIR_MASK](https://developer.android.com/reference/android/hardware/usb/UsbConstants#USB_ENDPOINT_DIR_MASK)is[UsbConstants.USB_DIR_OUT](https://developer.android.com/reference/android/hardware/usb/UsbConstants#USB_DIR_OUT), then the transfer is a write, and if it is[UsbConstants.USB_DIR_IN](https://developer.android.com/reference/android/hardware/usb/UsbConstants#USB_DIR_IN), then the transfer is a read.

<br />

|                                                      Parameters                                                       ||
|---------------|--------------------------------------------------------------------------------------------------------|
| `requestType` | `int`: request type for this transaction <br />                                                        |
| `request`     | `int`: request ID for this transaction <br />                                                          |
| `value`       | `int`: value field for this transaction <br />                                                         |
| `index`       | `int`: index field for this transaction <br />                                                         |
| `buffer`      | `byte`: buffer for data portion of transaction, or null if no data needs to be sent or received <br /> |
| `offset`      | `int`: the index of the first byte in the buffer to send or receive <br />                             |
| `length`      | `int`: the length of the data to send or receive <br />                                                |
| `timeout`     | `int`: in milliseconds <br />                                                                          |

|                                            Returns                                            ||
|-------|----------------------------------------------------------------------------------------|
| `int` | length of data transferred (or zero) for success, or negative value for failure <br /> |

### controlTransfer

Added in[API level 12](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public 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 \&[UsbConstants.USB_ENDPOINT_DIR_MASK](https://developer.android.com/reference/android/hardware/usb/UsbConstants#USB_ENDPOINT_DIR_MASK)is[UsbConstants.USB_DIR_OUT](https://developer.android.com/reference/android/hardware/usb/UsbConstants#USB_DIR_OUT), then the transfer is a write, and if it is[UsbConstants.USB_DIR_IN](https://developer.android.com/reference/android/hardware/usb/UsbConstants#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)](https://developer.android.com/reference/android/hardware/usb/UsbDeviceConnection#controlTransfer(int,%20int,%20int,%20int,%20byte[],%20int,%20int,%20int)).

<br />

<br />

|                                                      Parameters                                                       ||
|---------------|--------------------------------------------------------------------------------------------------------|
| `requestType` | `int`: request type for this transaction <br />                                                        |
| `request`     | `int`: request ID for this transaction <br />                                                          |
| `value`       | `int`: value field for this transaction <br />                                                         |
| `index`       | `int`: index field for this transaction <br />                                                         |
| `buffer`      | `byte`: buffer for data portion of transaction, or null if no data needs to be sent or received <br /> |
| `length`      | `int`: the length of the data to send or receive <br />                                                |
| `timeout`     | `int`: in milliseconds <br />                                                                          |

|                                            Returns                                            ||
|-------|----------------------------------------------------------------------------------------|
| `int` | length of data transferred (or zero) for success, or negative value for failure <br /> |

### getFileDescriptor

Added in[API level 12](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public 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.

<br />

|                 Returns                  ||
|-------|-----------------------------------|
| `int` | the native file descriptor <br /> |

### getRawDescriptors

Added in[API level 13](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public 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.

<br />

|               Returns                ||
|----------|----------------------------|
| `byte[]` | raw USB descriptors <br /> |

### getSerial

Added in[API level 12](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public String getSerial ()
```

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

<br />

|                                               Returns                                               ||
|--------------------------------------------------------------------|---------------------------------|
| [String](https://developer.android.com/reference/java/lang/String) | the device serial number <br /> |

### releaseInterface

Added in[API level 12](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public boolean releaseInterface (UsbInterface intf)
```

Releases exclusive access to a[UsbInterface](https://developer.android.com/reference/android/hardware/usb/UsbInterface).

<br />

|          Parameters           ||
|--------|-----------------------|
| `intf` | `UsbInterface` <br /> |

|                              Returns                              ||
|-----------|--------------------------------------------------------|
| `boolean` | true if the interface was successfully released <br /> |

### requestWait

Added in[API level 26](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public UsbRequest requestWait (long timeout)
```

Waits for the result of a[UsbRequest.queue(ByteBuffer)](https://developer.android.com/reference/android/hardware/usb/UsbRequest#queue(java.nio.ByteBuffer))operation

Note that this may return requests queued on multiple[UsbEndpoint](https://developer.android.com/reference/android/hardware/usb/UsbEndpoint)s. When multiple endpoints are in use,[UsbRequest.getEndpoint()](https://developer.android.com/reference/android/hardware/usb/UsbRequest#getEndpoint())and[UsbRequest.getClientData()](https://developer.android.com/reference/android/hardware/usb/UsbRequest#getClientData())can be useful in determining how to process the result of this function.

Android processes[UsbRequests](https://developer.android.com/reference/android/hardware/usb/UsbRequest)asynchronously. Hence it is not guaranteed that[requestWait(0)](https://developer.android.com/reference/android/hardware/usb/UsbDeviceConnection#requestWait(long))returns a request that has been queued right before even if the request could have been processed immediately.

<br />

<br />

|                                     Parameters                                     ||
|-----------|-------------------------------------------------------------------------|
| `timeout` | `long`: timeout in milliseconds. If 0 this method does not wait. <br /> |

|                                                                       Returns                                                                       ||
|---------------------------------------------------------------------------------------|--------------------------------------------------------------|
| [UsbRequest](https://developer.android.com/reference/android/hardware/usb/UsbRequest) | a completed USB request, or`null`if an error occurred <br /> |

|                                                                                                                                                                                                 Throws                                                                                                                                                                                                 ||
|-----------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [BufferOverflowException](https://developer.android.com/reference/java/nio/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[UsbRequest.queue(ByteBuffer, int)](https://developer.android.com/reference/android/hardware/usb/UsbRequest#queue(java.nio.ByteBuffer,%20int)) |
| [TimeoutException](https://developer.android.com/reference/java/util/concurrent/TimeoutException)   | if no request was received in`timeout`milliseconds.                                                                                                                                                                                                                                               |

### requestWait

Added in[API level 12](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public UsbRequest requestWait ()
```

Waits for the result of a[UsbRequest.queue(ByteBuffer)](https://developer.android.com/reference/android/hardware/usb/UsbRequest#queue(java.nio.ByteBuffer))operation

Note that this may return requests queued on multiple[UsbEndpoint](https://developer.android.com/reference/android/hardware/usb/UsbEndpoint)s. When multiple endpoints are in use,[UsbRequest.getEndpoint()](https://developer.android.com/reference/android/hardware/usb/UsbRequest#getEndpoint())and[UsbRequest.getClientData()](https://developer.android.com/reference/android/hardware/usb/UsbRequest#getClientData())can be useful in determining how to process the result of this function.

<br />

<br />

|                                                                       Returns                                                                       ||
|---------------------------------------------------------------------------------------|--------------------------------------------------------------|
| [UsbRequest](https://developer.android.com/reference/android/hardware/usb/UsbRequest) | a completed USB request, or null if an error occurred <br /> |

|                                                                                                                                                                                                                                                                            Throws                                                                                                                                                                                                                                                                            ||
|--------------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [IllegalArgumentException](https://developer.android.com/reference/java/lang/IllegalArgumentException) | Before API[android.os.Build.VERSION_CODES.O Build.VERSION_CODES.O](https://developer.android.com/reference/android/os/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[UsbRequest.queue(ByteBuffer, int)](https://developer.android.com/reference/android/hardware/usb/UsbRequest#queue(java.nio.ByteBuffer,%20int))      |
| [BufferOverflowException](https://developer.android.com/reference/java/nio/BufferOverflowException)    | In API[android.os.Build.VERSION_CODES.O Build.VERSION_CODES.O](https://developer.android.com/reference/android/os/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[UsbRequest.queue(ByteBuffer, int)](https://developer.android.com/reference/android/hardware/usb/UsbRequest#queue(java.nio.ByteBuffer,%20int)) |

### setConfiguration

Added in[API level 21](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public boolean setConfiguration (UsbConfiguration configuration)
```

Sets the device's current[UsbConfiguration](https://developer.android.com/reference/android/hardware/usb/UsbConfiguration).

<br />

|                 Parameters                 ||
|-----------------|---------------------------|
| `configuration` | `UsbConfiguration` <br /> |

|                             Returns                              ||
|-----------|-------------------------------------------------------|
| `boolean` | true if the configuration was successfully set <br /> |

### setInterface

Added in[API level 21](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public boolean setInterface (UsbInterface intf)
```

Sets the current[UsbInterface](https://developer.android.com/reference/android/hardware/usb/UsbInterface). Used to select between two interfaces with the same ID but different alternate setting.

<br />

|          Parameters           ||
|--------|-----------------------|
| `intf` | `UsbInterface` <br /> |

|                              Returns                              ||
|-----------|--------------------------------------------------------|
| `boolean` | true if the interface was successfully selected <br /> |

## Protected methods

### finalize

Added in[API level 12](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
protected 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.

<br />

|                                   Throws                                    ||
|--------------------------------------------------------------------------|---|
| [Throwable](https://developer.android.com/reference/java/lang/Throwable) |   |