Class Usb
java.lang.Object
com.codename1.io.usb.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.
-
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 InputStreamopenInputStream(UsbDevice device, int endpointAddress) Opens a bulk-transfer endpoint on the device.static OutputStreamopenOutputStream(UsbDevice device, int endpointAddress) static voidremoveDeviceListener(UsbDeviceListener listener) static voidrequestPermission(UsbDevice device) Requests permission from the user to talk todevice.
-
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
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:
IOException
-
openOutputStream
public static OutputStream openOutputStream(UsbDevice device, int endpointAddress) throws IOException - Throws:
IOException
-