public final class Usb

  1. Object
  2. Usb

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.

Methods

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

Inherited methods

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.

openOutputStream

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