Class Usb
java.lang.Object
com.codename1.io.usb.Usb
public final class Usb
extends java.lang.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 TypeMethodDescriptionstatic voidaddDeviceListener(UsbDeviceListener listener) Subscribeslistenerto attach / detach events.static booleanhasPermission(UsbDevice device) trueif the user has granted access todevice.static booleantrueif the current platform implements USB host access.static UsbDevice[]All currently-attached USB devices.static java.io.InputStreamopenInputStream(UsbDevice device, int endpointAddress) Opens a bulk-transfer endpoint on the device.static java.io.OutputStreamopenOutputStream(UsbDevice device, int endpointAddress) static voidremoveDeviceListener(UsbDeviceListener listener) static voidrequestPermission(UsbDevice device) Requests permission from the user to talk todevice.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Method Details
-
isSupported
public static boolean isSupported()trueif the current platform implements USB host access. -
listDevices
All currently-attached USB devices. -
addDeviceListener
Subscribeslistenerto attach / detach events. Returns immediately. Calls on the EDT. -
removeDeviceListener
-
requestPermission
Requests permission from the user to talk todevice. The result is reported asynchronously viaUsbDeviceListener.onPermissionResult. -
hasPermission
trueif the user has granted access todevice. -
openInputStream
public static java.io.InputStream openInputStream(UsbDevice device, int endpointAddress) throws java.io.IOException Opens a bulk-transfer endpoint on the device.endpointAddressmatches the USB endpoint address from the device's descriptor. The caller must have calledrequestPermissionand received approval first.- Throws:
java.io.IOException
-
openOutputStream
public static java.io.OutputStream openOutputStream(UsbDevice device, int endpointAddress) throws java.io.IOException - Throws:
java.io.IOException
-