Class UsbLink

java.lang.Object
com.codename1.io.link.ByteLink
com.codename1.io.link.UsbLink

public class UsbLink extends ByteLink

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.

  • Constructor Details

    • UsbLink

      public UsbLink(UsbDevice device, int inEndpoint, int outEndpoint)
      Parameters
      • device: the device

      • inEndpoint: the address of its bulk IN endpoint (bit 7 set, as 0x81)

      • outEndpoint: the address of its bulk OUT endpoint

  • Method Details

    • open

      public void open() throws IOException
      Description copied from class: ByteLink

      Connects. Does nothing when already open.

      Throws
      • IOException: when the device cannot be reached
      Specified by:
      open in class ByteLink
      Throws:
      IOException
    • isOpen

      public boolean isOpen()
      Description copied from class: ByteLink

      Whether ByteLink.open() succeeded and ByteLink.close() has not been called.

      Returns

      true while open

      Specified by:
      isOpen in class ByteLink
    • write

      public void write(byte[] data, int offset, int length) throws IOException
      Description copied from class: ByteLink

      Writes bytes, returning once they are handed to the device.

      Parameters
      • data: the buffer

      • offset: where to start in it

      • length: how many bytes

      Throws
      • IOException: when the link is closed or broken
      Specified by:
      write in class ByteLink
      Throws:
      IOException
    • read

      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.
      Specified by:
      read in class ByteLink
      Throws:
      IOException
    • close

      public void close()
      Description copied from class: ByteLink
      Disconnects. Safe to call more than once, and from any thread; a read blocked in another thread fails soon after.
      Specified by:
      close in class ByteLink