Class ConnectionBuilder

Object
com.zebra.sdk.comm.ConnectionBuilder

public class ConnectionBuilder extends Object
Builds a Connection from a description string. The description string is used to specify a connection to a specific device over TCP, TLS, Remote, or USB.
 
 The description string may be of the explicit forms: 
 
 TCP Connections:
 "TCP:192.168.1.4:6101" -- creates a TCP connection to the device with IP address 192.168.1.4 on port 6101
 "TCP:192.168.1.4" -- creates a TCP connection to the device with IP address 192.168.1.4 on default port 9100
 "TCP:fe80:0:0:0:6295:32ff:fe13:9cd8%17" -- creates a TCP connection with IPv6 link-local address with zone ID using default port 9100
 "TCP:fe80:0:0:0:6295:32ff:fe13:9cd8" -- creates a TCP connection with IPv6 link-local address without zone ID using default port 9100
 "TCP:fe80:0000:0000:0000:6295:32ff:fe13:9cd8" -- creates a TCP connection with IPv6 link-local uncompressed format using default port 9100
 "TCP:fe80::6295:32ff:fe13:9cd8" -- creates a TCP connection with IPv6 link-local compressed format using default port 9100
 "TCP:[fe80:0:0:0:6295:32ff:fe13:9cd8]:9100" -- creates a TCP connection with IPv6 link-local address using port 9100
 "TCP:dnsName:6101" -- creates a TCP connection to the device with 'dnsName' on port 6101
 "TCP:dnsName" -- creates a TCP connection to the device with 'dnsName' on default port 9100
 
 TLS Connections:
 "TLS:192.168.1.4:9143" -- creates a TLS connection to the device with IP address 192.168.1.4 on port 9143
 "TLS:192.168.1.4" -- creates a TLS connection to the device with IP address 192.168.1.4 on default port 9143
 "TLS:fe80:0:0:0:6295:32ff:fe13:9cd8%17" -- creates a TLS connection with IPv6 link-local address with zone ID using default port 9143
 "TLS:fe80:0:0:0:6295:32ff:fe13:9cd8" -- creates a TLS connection with IPv6 link-local address without zone ID using default port 9143
 "TLS:fe80:0000:0000:0000:6295:32ff:fe13:9cd8" -- creates a TLS connection with IPv6 link-local uncompressed format using default port 9143
 "TLS:fe80::6295:32ff:fe13:9cd8" -- creates a TLS connection with IPv6 link-local compressed format using default port 9143
 "TLS:[fe80:0:0:0:6295:32ff:fe13:9cd8]:9143" -- creates a TLS connection with IPv6 link-local address using port 9143
 "TLS:dnsName:9143" -- creates a TLS connection to the device with 'dnsName' on port 9143
 "TLS:dnsName" -- creates a TLS connection to the device with 'dnsName' on default port 9143
 
 Multichannel TCP Connections:
 "TCP_MULTI:192.168.1.4" -- creates a Multichannel TCP connection to the device with '192.168.1.4' using the default ports for both the printing channel (9100) and the status channel(9200)
 "TCP_MULTI:192.168.1.4:1234" -- creates a Multichannel TCP connection to the device with '192.168.1.4' using the given port for the printing channel (1234) and the default port for the status channel(9200)
 "TCP_MULTI:192.168.1.4:1234:5678" -- creates a Multichannel TCP connection to the device with '192.168.1.4' using the given ports for the printing channel (1234) and the status channel(5678)
 "TCP_MULTI:dnsName:1234:5678" -- creates a Multichannel TCP connection to the device with 'dnsName' using the given ports for the printing channel (1234) and the status channel(5678)
 "TCP_MULTI:fe80:0:0:0:6295:32ff:fe13:9cd8%17" -- creates a Multichannel TCP connection with IPv6 link-local address with zone ID using default port 9100
 "TCP_MULTI:fe80:0:0:0:6295:32ff:fe13:9cd8" -- creates a Multichannel TCP connection with IPv6 link-local address without zone ID using default port 9100
 "TCP_MULTI:fe80:0000:0000:0000:6295:32ff:fe13:9cd8" -- creates a Multichannel TCP connection with IPv6 link-local uncompressed format using default port 9100
 "TCP_MULTI:fe80::6295:32ff:fe13:9cd8" -- creates a Multichannel TCP connection with IPv6 link-local compressed format using default port 9100
 "TCP_MULTI:[fe80:0:0:0:6295:32ff:fe13:9cd8]:9100" -- creates a Multichannel TCP connection with IPv6 link-local address using port 9100
 
 Multichannel TLS Connections:
 "TLS_MULTI:192.168.1.4" -- creates a Multichannel TLS connection to the device with '192.168.1.4' using the default ports for both the printing channel (9143) and the status channel(9243)
 "TLS_MULTI:192.168.1.4:1234" -- creates a Multichannel TLS connection to the device with '192.168.1.4' using the given port for the printing channel (1234) and the default port for the status channel(9243)
 "TLS_MULTI:192.168.1.4:1234:5678" -- creates a Multichannel TLS connection to the device with '192.168.1.4' using the given ports for the printing channel (1234) and the status channel(5678)
 "TLS_MULTI:dnsName:1234:5678" -- creates a Multichannel TLS connection to the device with 'dnsName' using the given ports for the printing channel (1234) and the status channel(5678)
 "TLS_MULTI:fe80:0:0:0:6295:32ff:fe13:9cd8%17" -- creates a Multichannel TLS connection with IPv6 link-local address with zone ID using default port 9143
 "TLS_MULTI:fe80:0:0:0:6295:32ff:fe13:9cd8" -- creates a Multichannel TLS connection with IPv6 link-local address without zone ID using default port 9143
 "TLS_MULTI:fe80:0000:0000:0000:6295:32ff:fe13:9cd8" -- creates a Multichannel TLS connection with IPv6 link-local uncompressed format using default port 9143
 "TLS_MULTI:fe80::6295:32ff:fe13:9cd8" -- creates a Multichannel TLS connection with IPv6 link-local compressed format using default port 9143
 "TLS_MULTI:[fe80:0:0:0:6295:32ff:fe13:9cd8]:9143" -- creates a Multichannel TLS connection with IPv6 link-local address using port 9143
 
 TCP Status-only Connections:
 "TCP_STATUS:192.168.1.4:1234" -- creates a TCP status only connection to the device with IP address 192.168.1.4 on port 1234
 "TCP_STATUS:192.168.1.4" -- creates a TCP status only connection to the device with IP address 192.168.1.4 on the default status port 9200
 "TCP_STATUS:fe80:0:0:0:6295:32ff:fe13:9cd8%17" -- creates a TCP status connection with IPv6 link-local address with zone ID using default port 9200
 "TCP_STATUS:fe80:0:0:0:6295:32ff:fe13:9cd8" -- creates a TCP status connection with IPv6 link-local address without zone ID using default port 9200
 "TCP_STATUS:fe80:0000:0000:0000:6295:32ff:fe13:9cd8" -- creates a TCP status connection with IPv6 link-local uncompressed format using default port 9200
 "TCP_STATUS:fe80::6295:32ff:fe13:9cd8" -- creates a TCP status connection with IPv6 link-local compressed format using default port 9200
 "TCP_STATUS:[fe80:0:0:0:6295:32ff:fe13:9cd8]:9200" -- creates a TCP status connection with IPv6 link-local address using port 9200

 TLS Status-only Connections:
 "TLS_STATUS:192.168.1.4:1234" -- creates a TLS status only connection to the device with IP address 192.168.1.4 on port 1234
 "TLS_STATUS:192.168.1.4" -- creates a TLS status only connection to the device with IP address 192.168.1.4 on the default status port 9243
 "TLS_STATUS:fe80:0:0:0:6295:32ff:fe13:9cd8%17" -- creates a TLS status connection with IPv6 link-local address with zone ID using default port 9243
 "TLS_STATUS:fe80:0:0:0:6295:32ff:fe13:9cd8" -- creates a TLS status connection with IPv6 link-local address without zone ID using default port 9243
 "TLS_STATUS:fe80:0000:0000:0000:6295:32ff:fe13:9cd8" -- creates a TLS status connection with IPv6 link-local uncompressed format using default port 9243
 "TLS_STATUS:fe80::6295:32ff:fe13:9cd8" -- creates a TLS status connection with IPv6 link-local compressed format using default port 9243
 "TLS_STATUS:[fe80:0:0:0:6295:32ff:fe13:9cd8]:9243" -- creates a TLS status connection with IPv6 link-local address using port 9243
 
 Remote Connections:
 "REMOTE:serialNumber" -- creates a remote connection to the device with 'serialNumber' which is accessible via the RMI Server running on the default RMI port (11995).  The printer must be registered for use with a Zebra Weblink server. 
 "REMOTE:serialNumber:rmiPort" -- creates a remote connection to the device with 'serialNumber' which is accessible via the RMI Server running on port 'rmiPort'.  The printer must be registered for use with a Zebra Weblink server. 
 
 Multichannel Remote Connections:
 "REMOTE_MULTI:serialNumber:rmiPort" -- creates a multichannel remote connection to the device with 'serialNumber' which is accessible via the RMI Server running on port 'rmiPort'.  The printer must be registered for use with a Zebra Weblink server. 
 "REMOTE_STATUS:serialNumber:rmiPort" -- creates a status only remote connection to the device with 'serialNumber' which is accessible via the RMI Server running on port 'rmiPort'.  The printer must be registered for use with a Zebra Weblink server. 
 
 USB Connections:
 "USB:deviceName" -- creates a USB connection (through the ZebraDesigner driver) to the device with printer name 'deviceName'
 "USB_DIRECT:deviceName" -- creates a USB connection to the device with 'device.unique_id' or 'device.product_name' equal to 'deviceName'
 
 Note: When specifying an IPv6 address with a port, the address must be enclosed in square brackets [ ].
 
Generic text may also be used to attempt to specify a device. For example a description string of "genericText" will attempt to connect to a device using the following priority:
  • TCP_MULTI
  • TCP
  • TCP_STATUS
  • TLS_MULTI
  • TLS
  • TLS_STATUS
  • USB
  • USB_DIRECT
  • REMOTE_MULTI
  • REMOTE
  • REMOTE_STATUS
e.g. If you supply the string 'MyString'. This could be interpreted to be either a DNS name, a USB device name, or a Remote connection unique ID. ConnectionBuilder will attempt to connect to this string given the above priority order. If you supply a more specific string, such as '192.168.2.3', ConnectionBuilder will more efficiently interpret this string as being an IP address and, therefore, only attempt the TCP connections.
Note: Colon (':') characters are not supported in dnsName, friendlyName, uniqueId, deviceName, or genericText fields. The following is an example of building a connection from a string.

package test.zebra.sdk.comm.examples;
 
 import com.zebra.sdk.comm.*;
 import com.zebra.sdk.printer.*;
 import com.zebra.sdk.printer.discovery.*;
 
 public class ConnectionBuilderExample {
 
     public static void usbDriverlessTest() throws ConnectionException {
         System.out.println("Discovered USB printer list:\r\n");
         for (DiscoveredUsbPrinter printer : UsbDiscoverer.getZebraUsbPrinters(new ZebraPrinterFilter())) {
             System.out.println(printer);
         }
         System.out.println("End USB printer list\r\n");
 
         Connection imz = ConnectionBuilder.build("\\\\?\\usb#vid_0a5f&pid_00f2#imz220#...");
         try {
             imz.open();
             byte[] hi_return = imz.sendAndWaitForResponse("~HI".getBytes(), 5000, 10000, "V");
 
             System.out.println(new String(hi_return));
         } finally {
             imz.close();
         }
     }
 
     public static void main(String[] args) throws Exception {
         usbDriverlessTest();
 
         new ConnectionBuilderExample().nonBlockingStatusReportingOverMultichannel("1.2.3.4");
         new ConnectionBuilderExample().sendZplOverTcp("1.2.3.4");
         new ConnectionBuilderExample().sendZplOverUsb("ZDesigner GK420t");
     }
 
     private void nonBlockingStatusReportingOverMultichannel(String theIpAddress) throws ConnectionException {
         // Instantiate Multichannel connection for simultaneous printing and status reporting at given address
         Connection thePrinterConn = ConnectionBuilder.build("TCP_MULTI:" + theIpAddress + ":9100:9200");
 
         try {
             // Opens the connection - physical connection is established here.
             thePrinterConn.open();
 
             // Creates a Link-OS printing with the given connection
             ZebraPrinterLinkOs linkOsPrinter = ZebraPrinterFactory.getLinkOsPrinter(thePrinterConn);
 
             // This is sent over the printing channel (9100 by default) and will block the printing channel until the
             // label format is completely sent.
             String labelFormatStartCommand = "^XA";
             linkOsPrinter.sendCommand(labelFormatStartCommand);
 
             String labelBody = "^FO20,20^A0N,25,25^FDThis is a ZPL test.^FS";
             linkOsPrinter.sendCommand(labelBody);
 
             // This is sent over the status channel (9200 by default) and will return immediately even though the
             // printing channel is in use.
             // If a TcpConnection were used instead of a MultichannelTcpConnection, this would not be possible.
             PrinterStatus status = linkOsPrinter.getCurrentStatus();
 
             System.out.println("The printer PAUSED state is : " + status.isPaused);
 
             // Send the end of label command to finish and print the label.
             String labelFormatEndCommand = "^XZ";
             linkOsPrinter.sendCommand(labelFormatEndCommand);
         } catch (ConnectionException e) {
             // Handle communications error here.
             e.printStackTrace();
         } finally {
             // Close the connection to release resources.
             thePrinterConn.close();
         }
     }
 
     private void sendZplOverTcp(String theIpAddress) throws ConnectionException {
         // Instantiate connection for ZPL TCP port at given address
         Connection thePrinterConn = ConnectionBuilder.build("TCP:" + theIpAddress + ":9100");
 
         try {
             // Open the connection - physical connection is established here.
             thePrinterConn.open();
 
             // This example prints "This is a ZPL test." near the top of the label.
             String zplData = "^XA^FO20,20^A0N,25,25^FDThis is a ZPL test.^FS^XZ";
 
             // Send the data to printer as a byte array.
             thePrinterConn.write(zplData.getBytes());
         } catch (ConnectionException e) {
             // Handle communications error here.
             e.printStackTrace();
         } finally {
             // Close the connection to release resources.
             thePrinterConn.close();
         }
     }
 
     private void sendZplOverUsb(String usbDriverName) throws ConnectionException {
         // Instantiate USB connection for ZPL printer through its driver
         Connection thePrinterConn = ConnectionBuilder.build("USB:" + usbDriverName);
 
         try {
             // Open the connection - physical connection is established here.
             thePrinterConn.open();
 
             // This example prints "This is a ZPL test." near the top of the label.
             String zplData = "^XA^FO20,20^A0N,25,25^FDThis is a ZPL test.^FS^XZ";
 
             // Send the data to printer as a byte array.
             thePrinterConn.write(zplData.getBytes());
         } catch (ConnectionException e) {
             // Handle communications error here.
             e.printStackTrace();
         } finally {
             // Close the connection to release resources.
             thePrinterConn.close();
         }
     }
 }
 
  • Method Details

    • addConnectionType

      public static void addConnectionType(Class<? extends Connection> c)
      Add a connection type to the ConnectionBuilder
      Parameters:
      c - connection class
    • build

      public static Connection build(String descriptionString) throws ConnectionException
      Creates a Connection type based on the contents of descriptionString.

      Example:
      "TCP:192.168.1.2:9100"
      Parameters:
      descriptionString - The format of the input string is: [prefix:] address [: port_number(s)]
      • Prefix is either TCP_MULTI, TCP, TCP_STATUS, TLS_MULTI, TLS, TLS_STATUS, USB (if applicable), REMOTE_MULTI, REMOTE, or REMOTE_STATUS
      • The format of address depends on the prefix
        • USB : address is the printer driver name.
        • TCP : address is either a DNS name or an IPv4 address or an IPv6 address.
        • TLS : address is either a DNS name or an IPv4 address or an IPv6 address.
        • REMOTE : address is the printer's serial number.
      • port_number(s) is optional, and only applicable for TCP, TLS or remote connections.
      • Examples :
        • TCP:ZBR3054027:9100
        • TCP_MULTI:ZBR3054027:9100:9200
        • REMOTE:XXJK123456
        • REMOTE:XXJK123456:11995
        • 10.1.2.3
      Returns:
      Connection derived from the contents of descriptionString
      Throws:
      ConnectionException - if a connection could not be established for the given descriptionString.