Client API. The Codename One framework your app is built on: this runs on the device, not in a backend.
public class UsbLink
A USB device’s bulk endpoints: a USB receipt printer, or a USB CDC
device – many payment terminals – whose serial line settings do not
matter. Needs Usb.isSupported() (Android) and permission from the user
(Usb.requestPermission(UsbDevice)) before open().
The link sends no control requests, so a USB serial chip that must be
told its baud rate (FTDI, CP210x, CH340) does not work through it; plug
those into a desktop, where the operating system drives the chip and
SerialLink opens it.
A read waits in steps of the platform’s transfer timeout, five seconds on Android, so a short timeout may wait longer.
Constructors
public UsbLink(UsbDevice device, int inEndpoint, int outEndpoint) |
Methods
public void open()
throws IOException | Connects. |
public boolean isOpen() | Whether open() succeeded and close() has not been called. |
public void write(byte[] data, int offset, int length)
throws IOException | Writes bytes, returning once they are handed to the device. |
public int read(byte[] buffer, int offset, int length, int timeout)
throws IOException | A printer with no IN endpoint (inEndpoint of -1) never answers, and a read reports nothing arrived. |
public void close() | Disconnects. |
Inherited methods
Constructor details
UsbLink
public UsbLink(UsbDevice device, int inEndpoint, int outEndpoint)Parameters
deviceUsbDevice- the device
inEndpointint- the address of its bulk IN endpoint (bit 7 set, as
0x81) outEndpointint- the address of its bulk OUT endpoint
Method details
open
public void open()
throws IOExceptionConnects. Does nothing when already open.
Throws
IOException- when the device cannot be reached
isOpen
public boolean isOpen()Returns
true while open
write
public void write(byte[] data, int offset, int length)
throws IOExceptionWrites bytes, returning once they are handed to the device.
Parameters
databyte[]- the buffer
offsetint- where to start in it
lengthint- how many bytes
Throws
IOException- when the link is closed or broken
read
public int read(byte[] buffer, int offset, int length, int timeout)
throws IOExceptionA printer with no IN endpoint (
inEndpoint of -1) never answers, and
a read reports nothing arrived.Parameters
bufferbyte[]- where to put the bytes
offsetint- where to start in it
lengthint- at most how many bytes
timeoutint- how long to wait, in milliseconds
Returns
the number of bytes read; 0 when nothing arrived in time
Throws
IOException- when the link is closed or broken, including when the other side closed it
close
public void close()Disconnects. Safe to call more than once, and from any thread; a read
blocked in another thread fails soon after.