public class ConnectionBuilder extends Object
The description string may be of the explicit forms: "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: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 "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_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 "BT:11:22:33:44:55:66" -- creates a Bluetooth connection to the device using '11:22:33:44:55:66' as the MAC address "BT_MULTI:11:22:33:44:55:66" -- creates a multichannel Bluetooth connection to the device using '11:22:33:44:55:66' as the MAC address. (Link-OS 2.5 or higher for the status channel) "BT_STATUS:11:22:33:44:55:66" -- creates a status only Bluetooth connection to the device using '11:22:33:44:55:66' as the MAC address. (Link-OS 2.5 or higher)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:
package test.zebra.sdk.comm.examples;
import com.zebra.sdk.comm.Connection;
import com.zebra.sdk.comm.ConnectionBuilder;
import com.zebra.sdk.comm.ConnectionException;
import com.zebra.sdk.printer.PrinterStatus;
import com.zebra.sdk.printer.ZebraPrinterFactory;
import com.zebra.sdk.printer.ZebraPrinterLinkOs;
public class ConnectionBuilderExample {
public static void main(String[] args) {
new ConnectionBuilderExample().nonBlockingStatusReportingOverMultichannel("1.2.3.4");
new ConnectionBuilderExample().sendZplOverTcp("1.2.3.4");
new ConnectionBuilderExample().sendZplOverBluetooth("11:22:33:44:55:66");
}
private void nonBlockingStatusReportingOverMultichannel(String theIpAddress) {
try {
// Instantiate Multichannel connection for simultaneous printing and status reporting at given address
Connection thePrinterConn = ConnectionBuilder.build("TCP_MULTI:" + theIpAddress + ":9100:9200");
// 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);
// Close the connection to release resources.
thePrinterConn.close();
} catch (ConnectionException e) {
// Handle communications error here.
e.printStackTrace();
}
}
private void sendZplOverTcp(String theIpAddress) {
try {
// Instantiate TCP connection
Connection thePrinterConn = ConnectionBuilder.build("TCP:" + theIpAddress + ":9100");
// 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());
// Close the connection to release resources.
thePrinterConn.close();
} catch (ConnectionException e) {
// Handle communications error here.
e.printStackTrace();
}
}
private void sendZplOverBluetooth(String btMacAddress) {
try {
// Instantiate a Bluetooth connection
Connection thePrinterConn = ConnectionBuilder.build("BT:" + btMacAddress);
// 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());
// Close the connection to release resources.
thePrinterConn.close();
} catch (ConnectionException e) {
// Handle communications error here.
e.printStackTrace();
}
}
}
Modifier and Type | Method and Description |
---|---|
static void |
addConnectionType(Class<? extends Connection> c)
Add a connection type to the ConnectionBuilder
|
static Connection |
build(String descriptionString)
Creates a Connection type based on the contents of
descriptionString . |
public static void addConnectionType(Class<? extends Connection> c)
c
- connection classpublic static Connection build(String descriptionString) throws ConnectionException
descriptionString
. descriptionString
- The format of the input string is: [prefix:] address [: port_number(s)]
ConnectionException
- if a connection could not be established for the given descriptionString.
© 2015 ZIH Corp. All Rights Reserved.