# Class Usb

java.lang.Object
com.codename1.io.usb.Usb

public final class Usb extends Object

USB Host API.

Lets the device act as a USB host and talk to attached peripherals -- a barcode scanner, a serial-over-USB device, a microcontroller. This is Android-only in practice; iOS doesn't expose third-party USB host access and the simulator/JavaSE port stubs everything out.

##### Android specifics

The build pipeline adds the `android.hardware.usb.host` feature and a `<uses-feature>` declaration whenever `Usb` is referenced. To launch your app automatically when a device is plugged in, declare a `USB_DEVICE_ATTACHED` intent filter in `android.xintent_filter` using a `device_filter.xml` resource you ship in `native/android/res/xml/`. See `Developer Guide -> Network Connectivity -> USB`.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `static void`
  `addDeviceListener(UsbDeviceListener listener)`
  Subscribes `listener` to attach / detach events.
  `static boolean`
  `hasPermission(UsbDevice device)`
  `true` if the user has granted access to `device`.
  `static boolean`
  `isSupported()`
  `true` if the current platform implements USB host access.
  `static UsbDevice[]`
  `listDevices()`
  All currently-attached USB devices.
  `static InputStream`
  `openInputStream(UsbDevice device, int endpointAddress)`
  Opens a bulk-transfer endpoint on the device.
  `static OutputStream`
  `openOutputStream(UsbDevice device, int endpointAddress)`
  `static void`
  `removeDeviceListener(UsbDeviceListener listener)`
  `static void`
  `requestPermission(UsbDevice device)`
  Requests permission from the user to talk to `device`.

  ### Methods inherited from class Object

  `clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- ## Method Details

  - ### isSupported

    public static boolean isSupported()
    `true` if the current platform implements USB host access.

  - ### listDevices

    public static UsbDevice[] listDevices()
    All currently-attached USB devices.

  - ### addDeviceListener

    public static void addDeviceListener(UsbDeviceListener listener)
    Subscribes `listener` to attach / detach events. Returns immediately. Calls on the EDT.

  - ### removeDeviceListener

    public static void removeDeviceListener(UsbDeviceListener listener)

  - ### requestPermission

    public static void requestPermission(UsbDevice device)
    Requests permission from the user to talk to `device`. The result is reported asynchronously via `UsbDeviceListener.onPermissionResult`.

  - ### hasPermission

    public static boolean hasPermission(UsbDevice device)
    `true` if the user has granted access to `device`.

  - ### openInputStream

    public static InputStream openInputStream(UsbDevice device, int endpointAddress) throws IOException
    Opens a bulk-transfer endpoint on the device. `endpointAddress` matches the USB endpoint address from the device's descriptor. The caller must have called `requestPermission` and received approval first.
    Throws:
      `IOException`

  - ### openOutputStream

    public static OutputStream openOutputStream(UsbDevice device, int endpointAddress) throws IOException
    Throws:
      `IOException`