UniPRT SDK  v2.0.0.1
com.UniPRT.Sdk.Comm.UsbConnection Class Reference

Class that implements the USB specific communication and extends the AComm class. More...

Inheritance diagram for com.UniPRT.Sdk.Comm.UsbConnection:
com.UniPRT.Sdk.Comm.AComm com.UniPRT.Sdk.Comm.IComm

Classes

class  DescriptorValidateResult
 

Public Member Functions

short V_ID ()
 Vendor ID. More...
 
short P_ID ()
 
int BytesAvailable ()
 
boolean Connected ()
 
String Descriptor ()
 Returns a string description of the USB connection. More...
 
 UsbConnection (Context context, String descriptorHint)
 Initializes a new instance of the class based on the descriptor hint. More...
 
 UsbConnection (Context context)
 Initialize a new class instance using default vendor IDs. The target is the first printer that matches one of the known vendor IDs. More...
 
 UsbConnection (Context context, short vendorId)
 Initialize a new class instance using the specified vendor ID. The target is the first printer that matches the vendor ID. More...
 
 UsbConnection (Context context, short vendorId, short productId)
 Initialize a new class instance using the specified vendor ID and product ID. The target is the first printer that matches both IDs. More...
 
void RequestUSBPermission (String ACTION_USB_PERMISSION)
 
void Open ()
 
void Close ()
 
byte[] Read ()
 
void Write (byte[] dataOut)
 
void startReceivingTask ()
 
- Public Member Functions inherited from com.UniPRT.Sdk.Comm.AComm
void Read (OutputStream binDataIn) throws IOException
 
void WaitForData (int msTimeOut)
 Waits until data available BytesAvailable in current thread. Current thread sleeps until data received or timeout reached. Blocking call. More...
 
void Write (InputStream binReader) throws IOException
 Write from input stream to output stream. More...
 
void WriteAndWaitForResponse (OutputStream binDataIn, InputStream binDataOut, int responseStartTimeOut, int responseEndTimeOut, String completetionToken) throws IOException
 Write binDataOut stream data to output stream and return data received in binDataIn stream. Data returned is any data received or up to completion token if received. Wait for response timeout before returning. More...
 
byte[] WriteAndWaitForResponse (byte[] dataOut, int responseStartTimeOut, int responseEndTimeOut, String completetionToken) throws IOException
 Write byte data to output stream and return data received. Data returned is any data received or up to completion token if received. Wait for response timeout before returning. More...
 

Static Public Member Functions

static DescriptorValidateResult DescriptorValidate (String descriptorHint)
 Validates a descriptor string from the descriptorHint. More...
 
static boolean contains (short[] array, short target)
 

Public Attributes

UsbManager mUsbManager
 
UsbDevice mUsbDevice
 

Static Public Attributes

static final short TSC_USB_VID = 0x1203
 
static final short PTX_USB_VID = 0x14AE
 

Private Member Functions

 UsbConnection (Context context, short[] vendorIds, short productId)
 

Private Attributes

short _v_id = 0x0000
 
short _p_id = 0x0000
 
boolean _is_connected = false
 
UsbInterface mUsbIntf
 
UsbDeviceConnection mUsbConnection
 
UsbEndpoint mUsbEndpointIn
 
UsbEndpoint mUsbEndpointOut
 
BroadcastReceiver usbPermissionReceiver
 
Context context
 
PendingIntent permissionIntent
 
final Semaphore semaphore = new Semaphore(1)
 
final ExecutorService executor = Executors.newSingleThreadExecutor()
 
Future<?> cancelFuture
 
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream()
 
final Object lock = new Object()
 

Static Private Attributes

static final int BLOCK_SIZE = 512
 

Detailed Description

Class that implements the USB specific communication and extends the AComm class.

Examples

package com.test.UniPRTSdk;
import android.content.Context;
import java.io.*;
import java.util.List;
public class commUsbSnippet {
private static String ACTION_USB_PERMISSION = "com.example.UniPRTsdk_test.USB_PERMISSION";
static Context ctx;
public static void test(Context _ctx) {
ctx = _ctx;
sendPrintFile(); // send file over default printer data port
sendPrintString(); // send print data over default printer data port
}
public static void sendPrintFile() {
String fileName = "C:\\testFiles\\Hello.pgl";
UsbConnection ptrUsbComm = new UsbConnection(ctx); // sending through default data port
ptrUsbComm.RequestUSBPermission(ACTION_USB_PERMISSION);
try {
ptrUsbComm.Open();
if (new File(fileName).exists()) {
try (InputStream binReader = new BufferedInputStream(new FileInputStream(fileName))) {
System.out.println("Sending \"" + fileName + "\" to printer");
byte[] buffer = new byte[1024];
while ((binReader.read(buffer)) != -1) {
ptrUsbComm.Write(buffer);
}
}
} else {
System.out.println("File \"" + fileName + "\" not found");
}
} catch (Exception e) {
System.out.println("Exception Msg: " + e.getMessage());
} finally {
ptrUsbComm.Close();
}
}
public static void sendPrintString() {
String dataToPrint =
"~CREATE;C39;72\n" +
"SCALE;DOT\n" +
"PAGE;30;40\n" +
"ALPHA\n" +
"C10;1;33;0;0;@HELLO@\n" +
"C16;54;37;0;0;@*World*@\n" +
"STOP\n" +
"BARCODE\n" +
"C128C;XRD3:3:6:6:9:9:12:12;H6;10;32\n" +
"@World@\n" +
"STOP\n" +
"END\n" +
"~EXECUTE;C39\n" +
"~NORMAL\n";
UsbConnection ptrUsbComm = new UsbConnection(ctx); // sending through default data port
ptrUsbComm.RequestUSBPermission(ACTION_USB_PERMISSION);
try {
ptrUsbComm.Open();
if (ptrUsbComm.Connected()) {
byte[] outBytes = dataToPrint.getBytes("US-ASCII");
ptrUsbComm.Write(outBytes);
} else {
System.out.println("Not connected to printer");
}
} catch (Exception e) {
System.out.println("Exception Msg: " + e.getMessage());
} finally {
ptrUsbComm.Close();
}
}
}

Constructor & Destructor Documentation

◆ UsbConnection() [1/5]

com.UniPRT.Sdk.Comm.UsbConnection.UsbConnection ( Context  context,
String  descriptorHint 
)

Initializes a new instance of the class based on the descriptor hint.

Parameters
contextThe application context.
descriptorHintThe descriptor hint string.

References com.UniPRT.Sdk.Comm.UsbConnection.context, and com.UniPRT.Sdk.Comm.UsbConnection.DescriptorValidate().

◆ UsbConnection() [2/5]

com.UniPRT.Sdk.Comm.UsbConnection.UsbConnection ( Context  context)

Initialize a new class instance using default vendor IDs. The target is the first printer that matches one of the known vendor IDs.

Parameters
contextThe software context.

References com.UniPRT.Sdk.Comm.UsbConnection.context, com.UniPRT.Sdk.Comm.UsbConnection.PTX_USB_VID, and com.UniPRT.Sdk.Comm.UsbConnection.TSC_USB_VID.

◆ UsbConnection() [3/5]

com.UniPRT.Sdk.Comm.UsbConnection.UsbConnection ( Context  context,
short  vendorId 
)

Initialize a new class instance using the specified vendor ID. The target is the first printer that matches the vendor ID.

Parameters
contextThe software context.
vendorIdThe vendor ID of the target printer (e.g., TSC or PRINTRONIX).

References com.UniPRT.Sdk.Comm.UsbConnection.context.

◆ UsbConnection() [4/5]

com.UniPRT.Sdk.Comm.UsbConnection.UsbConnection ( Context  context,
short  vendorId,
short  productId 
)

Initialize a new class instance using the specified vendor ID and product ID. The target is the first printer that matches both IDs.

Parameters
contextThe software context.
vendorIdThe vendor ID of the target printer (e.g., TSC or PRINTRONIX).
productIdThe product ID of the target printer.

References com.UniPRT.Sdk.Comm.UsbConnection.context.

◆ UsbConnection() [5/5]

Member Function Documentation

◆ BytesAvailable()

int com.UniPRT.Sdk.Comm.UsbConnection.BytesAvailable ( )

Get the number of bytes available to read.

Returns
Number of bytes available to read.

Implements com.UniPRT.Sdk.Comm.IComm.

References com.UniPRT.Sdk.Comm.UsbConnection.byteArrayOutputStream.

◆ Close()

◆ Connected()

boolean com.UniPRT.Sdk.Comm.UsbConnection.Connected ( )

Check if connection is established.

Returns
true if connection is established, otherwise false.

Implements com.UniPRT.Sdk.Comm.IComm.

References com.UniPRT.Sdk.Comm.UsbConnection._is_connected.

◆ contains()

static boolean com.UniPRT.Sdk.Comm.UsbConnection.contains ( short[]  array,
short  target 
)
static

◆ Descriptor()

String com.UniPRT.Sdk.Comm.UsbConnection.Descriptor ( )

Returns a string description of the USB connection.

Format returned:

  • USB:V_ID:<v_id>; P_ID:<p_id>
  • p_id and v_id are short numbers. e.g. V_ID:4611; P_ID:1234

Implements com.UniPRT.Sdk.Comm.IComm.

References com.UniPRT.Sdk.Comm.UsbConnection._p_id, and com.UniPRT.Sdk.Comm.UsbConnection._v_id.

◆ DescriptorValidate()

static DescriptorValidateResult com.UniPRT.Sdk.Comm.UsbConnection.DescriptorValidate ( String  descriptorHint)
static

Validates a descriptor string from the descriptorHint.

descriptorHint accepted values:

  • "USB"
  • "USB:V_ID:4611"
  • "USB:V_ID:4611; P_ID:1234"
  • V_ID/P_ID may be decimal or hexadecimal (e.g. 0x1203)

Referenced by com.UniPRT.Sdk.Comm.UsbConnection.UsbConnection().

◆ Open()

◆ P_ID()

short com.UniPRT.Sdk.Comm.UsbConnection.P_ID ( )

◆ Read()

byte [] com.UniPRT.Sdk.Comm.UsbConnection.Read ( )

Read all available bytes.

Returns
Array of bytes read.

Implements com.UniPRT.Sdk.Comm.IComm.

References com.UniPRT.Sdk.Comm.UsbConnection.byteArrayOutputStream, and com.UniPRT.Sdk.Comm.UsbConnection.semaphore.

◆ RequestUSBPermission()

◆ startReceivingTask()

◆ V_ID()

short com.UniPRT.Sdk.Comm.UsbConnection.V_ID ( )

◆ Write()

void com.UniPRT.Sdk.Comm.UsbConnection.Write ( byte[]  dataOut)

Write all bytes from the array passed in.

Parameters
dataOutByte array to write.

Implements com.UniPRT.Sdk.Comm.IComm.

References com.UniPRT.Sdk.Comm.UsbConnection.lock, com.UniPRT.Sdk.Comm.UsbConnection.mUsbConnection, and com.UniPRT.Sdk.Comm.UsbConnection.mUsbEndpointOut.

Member Data Documentation

◆ _is_connected

boolean com.UniPRT.Sdk.Comm.UsbConnection._is_connected = false
private

◆ _p_id

◆ _v_id

◆ BLOCK_SIZE

final int com.UniPRT.Sdk.Comm.UsbConnection.BLOCK_SIZE = 512
staticprivate

◆ byteArrayOutputStream

ByteArrayOutputStream com.UniPRT.Sdk.Comm.UsbConnection.byteArrayOutputStream = new ByteArrayOutputStream()
private

◆ cancelFuture

Future<?> com.UniPRT.Sdk.Comm.UsbConnection.cancelFuture
private

◆ context

Context com.UniPRT.Sdk.Comm.UsbConnection.context
private

◆ executor

final ExecutorService com.UniPRT.Sdk.Comm.UsbConnection.executor = Executors.newSingleThreadExecutor()
private

◆ lock

final Object com.UniPRT.Sdk.Comm.UsbConnection.lock = new Object()
private

◆ mUsbConnection

◆ mUsbDevice

◆ mUsbEndpointIn

UsbEndpoint com.UniPRT.Sdk.Comm.UsbConnection.mUsbEndpointIn
private

◆ mUsbEndpointOut

UsbEndpoint com.UniPRT.Sdk.Comm.UsbConnection.mUsbEndpointOut
private

◆ mUsbIntf

UsbInterface com.UniPRT.Sdk.Comm.UsbConnection.mUsbIntf
private

◆ mUsbManager

◆ permissionIntent

PendingIntent com.UniPRT.Sdk.Comm.UsbConnection.permissionIntent
private

◆ PTX_USB_VID

final short com.UniPRT.Sdk.Comm.UsbConnection.PTX_USB_VID = 0x14AE
static

◆ semaphore

final Semaphore com.UniPRT.Sdk.Comm.UsbConnection.semaphore = new Semaphore(1)
private

◆ TSC_USB_VID

final short com.UniPRT.Sdk.Comm.UsbConnection.TSC_USB_VID = 0x1203
static

◆ usbPermissionReceiver

BroadcastReceiver com.UniPRT.Sdk.Comm.UsbConnection.usbPermissionReceiver
private

The documentation for this class was generated from the following file:
com.UniPRT.Sdk.Comm
Definition: AComm.java:1
com.UniPRT.Sdk.Utilities.Tuple
Definition: Tuple.java:3
com.UniPRT
com.UniPRT.Sdk.Comm.UsbConnection
Class that implements the USB specific communication and extends the AComm class.
Definition: UsbConnection.java:46
com.UniPRT.Sdk.Utilities
Definition: Tuple.java:1
com.UniPRT.Sdk.Comm.UsbConnection.UsbConnection
UsbConnection(Context context, String descriptorHint)
Initializes a new instance of the class based on the descriptor hint.
Definition: UsbConnection.java:216
com.UniPRT.Sdk
com