Class UsbCloverManager<T>

java.lang.Object
com.clover.remote.client.transport.usb.UsbCloverManager<T>
Direct Known Subclasses:
RemoteUsbManager, UsbAccessorySetupUsbManager

public abstract class UsbCloverManager<T> extends Object
This class is designed to be a reusable USB communication manager for devices that bulk transfer data. Instances of this class should be singletons.
  • Field Details

    • VERBOSE

      protected static final boolean VERBOSE
      See Also:
    • CONNECTION_TIMEOUT_MS

      protected static final int CONNECTION_TIMEOUT_MS
      See Also:
    • CONNECTION_RETRY_WAIT_MS

      protected static final int CONNECTION_RETRY_WAIT_MS
      See Also:
    • SETUP_TIMEOUT_MS

      protected static final int SETUP_TIMEOUT_MS
      See Also:
    • SETUP_RETRY_WAIT_MS

      protected static final int SETUP_RETRY_WAIT_MS
      See Also:
    • READ_TIMEOUT_MS

      protected static final int READ_TIMEOUT_MS
      See Also:
    • WRITE_TIMEOUT_MS

      protected static final int WRITE_TIMEOUT_MS
      See Also:
    • mConnection

      protected android.hardware.usb.UsbDeviceConnection mConnection
  • Constructor Details

    • UsbCloverManager

      public UsbCloverManager(android.content.Context context)
  • Method Details

    • open

      public void open() throws UsbCloverManager.UsbConnectException
      Open a connection to the USB device and begin a session. Only one session may be opened at a time.
      Throws:
      UsbCloverManager.UsbConnectException - if connecting to the device failed
    • getReadSize

      protected abstract int getReadSize()
    • getMaxWriteDataSize

      protected abstract int getMaxWriteDataSize()
    • onPreConnect

      protected void onPreConnect()
      Called before a connection is attempted, does nothing by default and may be overridden.
    • findDevice

      public static android.hardware.usb.UsbDevice findDevice(android.hardware.usb.UsbManager usbManager, android.util.Pair<Integer,Integer>[] vendorProductIds)
    • getVendorProductIds

      protected abstract android.util.Pair<Integer,Integer>[] getVendorProductIds()
    • isMatch

      public static boolean isMatch(android.hardware.usb.UsbDevice device, android.util.Pair<Integer,Integer>[] vendorProductIds)
    • isBulkInterface

      protected boolean isBulkInterface()
    • isInterfaceMatch

      protected boolean isInterfaceMatch(android.hardware.usb.UsbInterface usbInterface)
    • disconnect

      public void disconnect()
    • onPostDisconnect

      protected void onPostDisconnect()
      Called after disconnect is complete, does nothing by default and may be overridden.
    • isConnected

      public final boolean isConnected()
    • write

      public int write(byte[] outputData, T params) throws IOException, InterruptedException
      Throws:
      IOException
      InterruptedException
    • read

      public byte[] read(T params) throws IOException, InterruptedException
      Throws:
      IOException
      InterruptedException
    • onTransferError

      protected void onTransferError()
      Called when a transfer error occurs, does nothing by default and may be overridden.
    • wrapWritePacket

      protected byte[] wrapWritePacket(ByteBuffer outDataBuffer, int numBytesToTransfer)
    • unwrapReadPacket

      protected byte[] unwrapReadPacket(ByteBuffer inDataBuffer)
    • getReadTimeOut

      protected int getReadTimeOut()
      Returns the amount of time to wait before read fails, 0 means wait forever.
    • processOutputData

      protected byte[] processOutputData(byte[] outputData, T params)
      Optionally override this method to perform data processing on the output bytes before they are sent. The returned byte array is used for the data transfer, the implementation may choose to modify the byte array parameter or return a new byte array.
    • processInputData

      protected UsbCloverManager.InputResult processInputData(byte[] inputData, ByteArrayOutputStream outputStream, T params)
      Optionally override this method to perform data processing on the input data bytes before they are returned. The implementation must write the desired output bytes to the ByteArrayOutputStream. Unlike processOutputData(byte[], T) this function may be called multiple times in a single transfer depending on the return value of this function
      Parameters:
      inputData - buffer containing bytes read from the USB device
      outputStream - stream containing bytes that will be returned to the caller
      params - additional parameters which may allow the implementation to modify the way the byte array is interpreted
      Returns:
      CONTINUE to request more USB device data, COMPLETE to return the received data or ERROR if something is wrong
    • postStatusChange

      protected final com.clover.remote.client.transport.usb.UsbCloverManager.DeviceStatus postStatusChange(com.clover.remote.client.transport.usb.UsbCloverManager.DeviceStatus status)