FileDocCategorySizeDatePackage
UsbDeviceConnection.javaAPI DocAndroid 5.1 API10508Thu Mar 12 22:22:10 GMT 2015android.hardware.usb

UsbDeviceConnection

public class UsbDeviceConnection extends Object
This class is used for sending and receiving data and control messages to a USB device. Instances of this class are created by {@link UsbManager#openDevice}.

Fields Summary
private static final String
TAG
private final UsbDevice
mDevice
private long
mNativeContext
Constructors Summary
public UsbDeviceConnection(UsbDevice device)
UsbDevice should only be instantiated by UsbService implementation

hide


                  
       
        mDevice = device;
    
Methods Summary
public intbulkTransfer(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 {@link #bulkTransfer(UsbEndpoint, byte[], int, int, int)}.

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

        return bulkTransfer(endpoint, buffer, 0, length, timeout);
    
public intbulkTransfer(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.

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

        checkBounds(buffer, offset, length);
        return native_bulk_request(endpoint.getAddress(), buffer, offset, length, timeout);
    
private static voidcheckBounds(byte[] buffer, int start, int length)

        final int bufferLength = (buffer != null ? buffer.length : 0);
        if (start < 0 || start + length > bufferLength) {
            throw new IllegalArgumentException("Buffer start or length out of bounds.");
        }
    
public booleanclaimInterface(UsbInterface intf, boolean force)
Claims exclusive access to a {@link android.hardware.usb.UsbInterface}. This must be done before sending or receiving data on any {@link android.hardware.usb.UsbEndpoint}s belonging to the interface.

param
intf the interface to claim
param
force true to disconnect kernel driver if necessary
return
true if the interface was successfully claimed

        return native_claim_interface(intf.getId(), force);
    
public voidclose()
Releases all system resources related to the device. Once the object is closed it cannot be used again. The client must call {@link UsbManager#openDevice} again to retrieve a new instance to reestablish communication with the device.

        native_close();
    
public intcontrolTransfer(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 & {@link UsbConstants#USB_ENDPOINT_DIR_MASK} is {@link UsbConstants#USB_DIR_OUT}, then the transfer is a write, and if it is {@link 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 {@link #controlTransfer(int, int, int, int, byte[], int, int, int)}.

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

        return controlTransfer(requestType, request, value, index, buffer, 0, length, timeout);
    
public intcontrolTransfer(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 & {@link UsbConstants#USB_ENDPOINT_DIR_MASK} is {@link UsbConstants#USB_DIR_OUT}, then the transfer is a write, and if it is {@link UsbConstants#USB_DIR_IN}, then the transfer is a read.

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

        checkBounds(buffer, offset, length);
        return native_control_request(requestType, request, value, index,
                buffer, offset, length, timeout);
    
public intgetFileDescriptor()
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.

return
the native file descriptor

        return native_get_fd();
    
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.

return
raw USB descriptors

        return native_get_desc();
    
public java.lang.StringgetSerial()
Returns the serial number for the device. This will return null if the device has not been opened.

return
the device serial number

        return native_get_serial();
    
private native intnative_bulk_request(int endpoint, byte[] buffer, int offset, int length, int timeout)

private native booleannative_claim_interface(int interfaceID, boolean force)

private native voidnative_close()

private native intnative_control_request(int requestType, int request, int value, int index, byte[] buffer, int offset, int length, int timeout)

private native byte[]native_get_desc()

private native intnative_get_fd()

private native java.lang.Stringnative_get_serial()

private native booleannative_open(java.lang.String deviceName, java.io.FileDescriptor pfd)

private native booleannative_release_interface(int interfaceID)

private native UsbRequestnative_request_wait()

private native booleannative_set_configuration(int configurationID)

private native booleannative_set_interface(int interfaceID, int alternateSetting)

booleanopen(java.lang.String name, android.os.ParcelFileDescriptor pfd)

        return native_open(name, pfd.getFileDescriptor());
    
public booleanreleaseInterface(UsbInterface intf)
Releases exclusive access to a {@link android.hardware.usb.UsbInterface}.

return
true if the interface was successfully released

        return native_release_interface(intf.getId());
    
public UsbRequestrequestWait()
Waits for the result of a {@link android.hardware.usb.UsbRequest#queue} operation Note that this may return requests queued on multiple {@link android.hardware.usb.UsbEndpoint}s. When multiple endpoints are in use, {@link android.hardware.usb.UsbRequest#getEndpoint} and {@link android.hardware.usb.UsbRequest#getClientData} can be useful in determining how to process the result of this function.

return
a completed USB request, or null if an error occurred

        UsbRequest request = native_request_wait();
        if (request != null) {
            request.dequeue();
        }
        return request;
    
public booleansetConfiguration(UsbConfiguration configuration)
Sets the device's current {@link android.hardware.usb.UsbConfiguration}.

return
true if the configuration was successfully set

        return native_set_configuration(configuration.getId());
    
public booleansetInterface(UsbInterface intf)
Sets the current {@link android.hardware.usb.UsbInterface}. Used to select between two interfaces with the same ID but different alternate setting.

return
true if the interface was successfully selected

        return native_set_interface(intf.getId(), intf.getAlternateSetting());