Android Programming Press on the image to return to the main documentation page.

USB

A complete working example with a tutorial is available
here.

List of types:

MtpDevice
UsbAccessory
UsbDevice
UsbDeviceConnection
UsbEndpoint
UsbInterface
UsbManager
UsbRequest

MtpDevice


Events:

None

Members:


  Close

  Initialize (EventName As String, UsbDevice As android.hardware.usb.UsbDevice)

  IsInitialized As Boolean

  Open (Connection As UsbDeviceConnection)

  test

Members description:

Close
Initialize (EventName As String, UsbDevice As android.hardware.usb.UsbDevice)
IsInitialized As Boolean
Open (Connection As UsbDeviceConnection)
test

UsbAccessory

Represents a Usb accessory.

Events:

None

Members:


  Close

  Description As String [read only]

  InputStream As InputStreamWrapper [read only]

  Manufacturer As String [read only]

  Model As String [read only]

  OutputStream As OutputStreamWrapper [read only]

  Serial As String [read only]

  Uri As String [read only]

  Version As String [read only]

Members description:

Close
Closes the accessory.
The accessory input and output streams should be individually closed first.
Description As String [read only]
Gets the description of the accessory.
InputStream As InputStreamWrapper [read only]
Gets the input stream for the accessory.
When reading data from an accessory ensure that the buffer that you use is
big enough to store the USB packet data. The Android accessory protocol supports
packet buffers up to 16384 bytes, so you can choose to always declare your buffer
to be of this size for simplicity.
Manufacturer As String [read only]
Gets the manufacturer of the accessory.
Model As String [read only]
Gets the model name of the accessory.
OutputStream As OutputStreamWrapper [read only]
Gets the output stream for the accessory.
Serial As String [read only]
Gets the unique serial number for the accessory.
Uri As String [read only]
Gets the URI for the website of the accessory.
Version As String [read only]
Gets the version of the accessory.

UsbDevice

Represents a Usb device.

Events:

None

Members:


  DeviceClass As Int [read only]

  DeviceId As Int [read only]

  DeviceName As String [read only]

  DeviceSubclass As Int [read only]

  GetInterface (Index As Int) As android.hardware.usb.UsbInterface

  InterfaceCount As Int [read only]

  IsInitialized As Boolean

  ProductId As Int [read only]

  VendorId As Int [read only]

Members description:

DeviceClass As Int [read only]
Gets the device class.
DeviceId As Int [read only]
Gets the device id.
DeviceName As String [read only]
Gets the device name.
DeviceSubclass As Int [read only]
Gets the device subclass.
GetInterface (Index As Int) As android.hardware.usb.UsbInterface
Gets the interface at the given index.
InterfaceCount As Int [read only]
Gets the number of interfaces.
IsInitialized As Boolean
ProductId As Int [read only]
Gets the product id.
VendorId As Int [read only]
Gets the vendor id.

UsbDeviceConnection

Represents a connection between the host and a client.
UsbDeviceConnection is created by calling UsbManager.OpenDevice.
Once connected you should call StartListening to start listening for completed requests.
Sending requests is done with UsbRequest.Queue.
The NewData event is raised when a request completes. The request is passed as a parameter.
You should call ContinueListening to allow the listener to listen to the next completed request (after another IN request is sent).
Calling StopListening will close the connection.
ControlTransfer method sends requests to endpoint zero which is the control endpoint.
ControlTransfer is a blocking method (unlike UsbRequest.Queue which is asynchronous).

Events:

NewData (Request As UsbRequest, InDirection As Boolean)

Members:


  BulkTransfer (Endpoint As android.hardware.usb.UsbEndpoint, Buffer() As Byte, Length As Int, Timeout As Int) As Int

  CloseSynchronous

  ContinueListening

  ControlTransfer (RequestType As Int, Request As Int, Value As Int, Index As Int, Buffer() As Byte, Length As Int, Timeout As Int) As Int

  GetRawDescriptors As Byte()

  IsInitialized As Boolean

  Serial As String [read only]

  StartListening (EventName As String)

  StopListening

Members description:

BulkTransfer (Endpoint As android.hardware.usb.UsbEndpoint, Buffer() As Byte, Length As Int, Timeout As Int) As Int
Sends a synchronous request.
Endpoint - The endpoint for this transaction. The transfer direction is determined by this endpoint.
Buffer - Buffer for data to send or receive.
Length - The length of the data.
Timeout - Request timeout in milliseconds.
CloseSynchronous
Closes the connection. StopListening also closes the connection.
This method should only be used when the asynchronous listener was not started.
ContinueListening
Notifies the listener to continue listening for completed requests.
ControlTransfer (RequestType As Int, Request As Int, Value As Int, Index As Int, Buffer() As Byte, Length As Int, Timeout As Int) As Int
Performs a control transaction on endpoint zero. Returns the number of bytes transferred.
RequestType - The request type. It should combine USB_DIR_IN or USB_DIR_OUT to set the request direction.
Request - Request Id.
Value - Value field.
Index - Index field.
Buffer - Buffer for data portion. Pass Null if not needed.
Length - The length of the data to send or receive.
Timeout - Timeout in milliseconds.
GetRawDescriptors As Byte()
Returns the raw descriptors as an array of bytes.
This method is only available in Android 3.2 or above.It will return an empty array in Android 3.1.
IsInitialized As Boolean
Tests whether the object was initialized.
Serial As String [read only]
Returns the connected device serial number.
StartListening (EventName As String)
Starts listening for completed requests. When such are available the NewData event will be raised.
EventName - The name of the sub that will handle the events.
StopListening
Stops listening to requests and closes the connection.

UsbEndpoint

Represents an endpoint in a specific interface.

Events:

None

Members:


  Address As Int [read only]

  Attributes As Int [read only]

  Direction As Int [read only]

  EndpointNumber As Int [read only]

  Interval As Int [read only]

  IsInitialized As Boolean

  MaxPacketSize As Int [read only]

  Type As Int [read only]

Members description:

Address As Int [read only]
Gets the endpoint address.
Attributes As Int [read only]
Gets the endpoint attributes.
Direction As Int [read only]
Gets the endpoint direction. Can be UsbManager.USB_DIR_IN or UsbManager.USB_DIR_OUT.
EndpointNumber As Int [read only]
Gets the endpoint number.
Interval As Int [read only]
Gets the interval field.
IsInitialized As Boolean
MaxPacketSize As Int [read only]
Gets the maximum packet size.
Type As Int [read only]
Gets the endpoint type.

UsbInterface

Represents a Usb interface on a specific device.

Events:

None

Members:


  EndpointCount As Int [read only]

  GetEndpoint (Index As Int) As UsbEndpoint

  InterfaceClass As Int [read only]

  InterfaceProtocol As Int [read only]

  InterfaceSubclass As Int [read only]

  IsInitialized As Boolean

Members description:

EndpointCount As Int [read only]
Gets the number of endpoints available in this interface.
GetEndpoint (Index As Int) As UsbEndpoint
Gets the endpoint at the given index.
InterfaceClass As Int [read only]
Gets the interface class.
InterfaceProtocol As Int [read only]
Gets the interface protocol.
InterfaceSubclass As Int [read only]
Gets the interface subclass.
IsInitialized As Boolean

UsbManager

UsbManager gives access to the connected Usb devices. It also holds the related constants.
This library requires Android SDK 12 or above (Android 3.1 or above).
You should configure Basic4android to use android.jar from android-12 or above.

Events:

None

Members:


  GetAccessories As UsbAccessory()

  GetDevices As UsbDevice()

  HasAccessoryPermission (Accessory As UsbAccessory) As Boolean

  HasPermission (Device As android.hardware.usb.UsbDevice) As Boolean

  Initialize

  OpenAccessory (Accessory As UsbAccessory)

  OpenDevice (Device As android.hardware.usb.UsbDevice, Interface As android.hardware.usb.UsbInterface, ForceClaim As Boolean) As UsbDeviceConnection

  RequestAccessoryPermission (Accessory As UsbAccessory)

  RequestPermission (Device As android.hardware.usb.UsbDevice)

  USB_CLASS_APP_SPEC As Int

  USB_CLASS_AUDIO As Int

  USB_CLASS_CDC_DATA As Int

  USB_CLASS_COMM As Int

  USB_CLASS_CONTENT_SEC As Int

  USB_CLASS_CSCID As Int

  USB_CLASS_HID As Int

  USB_CLASS_HUB As Int

  USB_CLASS_MASS_STORAGE As Int

  USB_CLASS_MISC As Int

  USB_CLASS_PER_INTERFACE As Int

  USB_CLASS_PHYSICA As Int

  USB_CLASS_PRINTER As Int

  USB_CLASS_STILL_IMAGE As Int

  USB_CLASS_VENDOR_SPEC As Int

  USB_CLASS_VIDEO As Int

  USB_CLASS_WIRELESS_CONTROLLER As Int

  USB_DIR_IN As Int

  USB_DIR_OUT As Int

  USB_ENDPOINT_DIR_MASK As Int

  USB_ENDPOINT_NUMBER_MASK As Int

  USB_ENDPOINT_XFER_BULK As Int

  USB_ENDPOINT_XFER_CONTROL As Int

  USB_ENDPOINT_XFER_INT As Int

  USB_ENDPOINT_XFER_ISOC As Int

  USB_ENDPOINT_XFERTYPE_MASK As Int

  USB_INTERFACE_SUBCLASS_BOOT As Int

  USB_SUBCLASS_VENDOR_SPEC As Int

  USB_TYPE_CLASS As Int

  USB_TYPE_MASK As Int

  USB_TYPE_RESERVED As Int

  USB_TYPE_STANDARD As Int

  USB_TYPE_VENDOR As Int

Members description:

GetAccessories As UsbAccessory()
Returns an array of UsbAccessories with all the connected USB accessories.
GetDevices As UsbDevice()
Returns an array of UsbDevices with all the connected USB devices.
HasAccessoryPermission (Accessory As UsbAccessory) As Boolean
Tests whether your application has permission to access this accessory.
Call RequestAccessoryPermission to receive such permission.
HasPermission (Device As android.hardware.usb.UsbDevice) As Boolean
Tests whether your application has permission to access this device.
Call RequestPermission to receive such permission.
Initialize
Initializes the object.
OpenAccessory (Accessory As UsbAccessory)
Connects to the given accessory
OpenDevice (Device As android.hardware.usb.UsbDevice, Interface As android.hardware.usb.UsbInterface, ForceClaim As Boolean) As UsbDeviceConnection
Connects to the given device and claims exclusive access to the given interface.
ForceClaim - Whether the system should disconnect kernel drivers if necessary.
RequestAccessoryPermission (Accessory As UsbAccessory)
Shows a dialog that asks the user to allow your application to access the USB accessory.
RequestPermission (Device As android.hardware.usb.UsbDevice)
Shows a dialog that asks the user to allow your application to access the USB device.
USB_CLASS_APP_SPEC As Int
USB_CLASS_AUDIO As Int
USB_CLASS_CDC_DATA As Int
USB_CLASS_COMM As Int
USB_CLASS_CONTENT_SEC As Int
USB_CLASS_CSCID As Int
USB_CLASS_HID As Int
USB_CLASS_HUB As Int
USB_CLASS_MASS_STORAGE As Int
USB_CLASS_MISC As Int
USB_CLASS_PER_INTERFACE As Int
USB_CLASS_PHYSICA As Int
USB_CLASS_PRINTER As Int
USB_CLASS_STILL_IMAGE As Int
USB_CLASS_VENDOR_SPEC As Int
USB_CLASS_VIDEO As Int
USB_CLASS_WIRELESS_CONTROLLER As Int
USB_DIR_IN As Int
USB_DIR_OUT As Int
USB_ENDPOINT_DIR_MASK As Int
USB_ENDPOINT_NUMBER_MASK As Int
USB_ENDPOINT_XFER_BULK As Int
USB_ENDPOINT_XFER_CONTROL As Int
USB_ENDPOINT_XFER_INT As Int
USB_ENDPOINT_XFER_ISOC As Int
USB_ENDPOINT_XFERTYPE_MASK As Int
USB_INTERFACE_SUBCLASS_BOOT As Int
USB_SUBCLASS_VENDOR_SPEC As Int
USB_TYPE_CLASS As Int
USB_TYPE_MASK As Int
USB_TYPE_RESERVED As Int
USB_TYPE_STANDARD As Int
USB_TYPE_VENDOR As Int

UsbRequest

This object represents a USB request packet.
Queue method sends the request.

Events:

None

Members:


  Buffer() As Byte [read only]

  Initialize (Connection As UsbDeviceConnection, Endpoint As android.hardware.usb.UsbEndpoint)

  IsInitialized As Boolean

  Name As String

  Queue (Buffer() As Byte, Length As Int)

  UsbEndpoint As UsbEndpoint [read only]

Members description:

Buffer() As Byte [read only]
Returns the buffer associated with the request.
Initialize (Connection As UsbDeviceConnection, Endpoint As android.hardware.usb.UsbEndpoint)
Initializes the request. The request will be binded to the given connection and endpoint.
Note that for control transactions you should use UsbDeviceConnection.ControlTransfer.
IsInitialized As Boolean
Name As String
Gets or sets an arbitrary string that can be used to identify the request.
Queue (Buffer() As Byte, Length As Int)
Queues the request for sending. UsbDeviceConnection_NewData event will be raised when the transaction completes.
UsbEndpoint As UsbEndpoint [read only]

Top