Skip to content

Commit 0ee3cb3

Browse files
committed
Added USB Printer Handling
1 parent eedcba5 commit 0ee3cb3

11 files changed

+64
-69
lines changed

.idea/deploymentTargetSelector.xml

+10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

PrintWrapper/build.gradle

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ plugins {
88
ext {
99
PUBLISH_GROUP_ID = 'com.zebra.printwrapper'
1010
PUBLISH_ARTIFACT_ID = 'printwrapper'
11-
PUBLISH_VERSION = '1.17'
11+
PUBLISH_VERSION = '1.19'
1212
}
1313

1414
android {
@@ -20,8 +20,8 @@ android {
2020
defaultConfig {
2121
minSdkVersion 24
2222
targetSdkVersion 34
23-
versionCode 17
24-
versionName "1.17"
23+
versionCode 19
24+
versionName "1.19"
2525

2626
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
2727
consumerProguardFiles "consumer-rules.pro"

PrintWrapper/src/main/java/com/zebra/printwrapper/PrintWrapperHelpers.java

+11
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.zebra.sdk.printer.discovery.DiscoveredPrinter;
44
import com.zebra.sdk.printer.discovery.DiscoveredPrinterBluetooth;
55
import com.zebra.sdk.printer.discovery.DiscoveredPrinterNetwork;
6+
import com.zebra.sdk.printer.discovery.DiscoveredPrinterUsb;
67

78
import java.util.Map;
89

@@ -28,6 +29,16 @@ public static boolean isNetworkPrinter(DiscoveredPrinter printer)
2829
return printer instanceof DiscoveredPrinterNetwork;
2930
}
3031

32+
/**
33+
* Verify if printer is a usb printer
34+
* @param printer
35+
* @return true if printer is a network printer
36+
*/
37+
public static boolean isUSBPrinter(DiscoveredPrinter printer)
38+
{
39+
return printer instanceof DiscoveredPrinterUsb;
40+
}
41+
3142
/**
3243
* Get network printer port
3344
* @param printer

PrintWrapper/src/main/java/com/zebra/printwrapper/SGDHelper.java

+32-3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import com.zebra.sdk.printer.discovery.DiscoveredPrinter;
1414
import com.zebra.sdk.printer.discovery.DiscoveredPrinterBluetooth;
1515
import com.zebra.sdk.printer.discovery.DiscoveredPrinterNetwork;
16+
import com.zebra.sdk.printer.discovery.DiscoveredPrinterUsb;
1617

1718
import java.io.IOException;
1819
import java.io.UnsupportedEncodingException;
@@ -90,7 +91,7 @@ public static Connection connectToPrinter(DiscoveredPrinter printer, SGDHelperCa
9091
if (callback != null) {
9192
callback.onMessage("Connecting to bluetooth Printer: " + printer.address);
9293
}
93-
mConnection = new BluetoothConnection(printer.address);
94+
mConnection = printer.getConnection();
9495
try {
9596
if (callback != null) {
9697
callback.onMessage("Opening connection.");
@@ -132,7 +133,7 @@ public static Connection connectToPrinter(DiscoveredPrinter printer, SGDHelperCa
132133
callback.onMessage("Connecting to Network Printer: " + printer.address);
133134
}
134135
try {
135-
mConnection = new TcpConnection(printer.address, PrintWrapperHelpers.getNetworkPrinterPort(printer));
136+
mConnection = printer.getConnection();
136137

137138
if (callback != null) {
138139
callback.onMessage("Opening connection.");
@@ -166,9 +167,37 @@ public static Connection connectToPrinter(DiscoveredPrinter printer, SGDHelperCa
166167
mConnection = null;
167168
}
168169
}
170+
169171
throw new PrinterWrapperException(new Exception("SGDHelper.connectToPrinter: Could not establish connection with Network Printer."));
170172
}
171-
throw new PrinterWrapperException(new Exception("SGDHelper.connectToPrinter: Printer connexion type not supported. USB ?"));
173+
else if(printer instanceof DiscoveredPrinterUsb) {
174+
try {
175+
mConnection = printer.getConnection();
176+
mConnection.open();
177+
if (mConnection.isConnected() == true) {
178+
if (callback != null) {
179+
callback.onMessage("Connection opened with success.");
180+
}
181+
return mConnection;
182+
} else {
183+
if (callback != null) {
184+
callback.onMessage("Error:Could not connect to printer.");
185+
}
186+
187+
}
188+
} catch (Exception e) {
189+
e.printStackTrace();
190+
try {
191+
mConnection.close();
192+
mConnection = null;
193+
} catch (ConnectionException ex) {
194+
ex.printStackTrace();
195+
mConnection = null;
196+
}
197+
}
198+
throw new PrinterWrapperException(new Exception("SGDHelper.connectToPrinter: Could not establish connection with USB Printer."));
199+
}
200+
throw new PrinterWrapperException(new Exception("SGDHelper.connectToPrinter: Printer connexion type not supported."));
172201
}
173202

174203
public static String GET(String propertyName, DiscoveredPrinter discoveredPrinter, SGDHelperCallback callback) throws PrinterWrapperException {

PrintWrapper/src/main/java/com/zebra/printwrapper/SendCPCLTask.java

+1-9
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,7 @@ private void sendPrint() {
6666
return;
6767
}
6868

69-
Connection connection = null;
70-
if(PrintWrapperHelpers.isBluetoothPrinter(printer))
71-
{
72-
connection = new BluetoothConnection(printer.address);
73-
}
74-
else
75-
{
76-
connection = new TcpConnection(printer.address, PrintWrapperHelpers.getNetworkPrinterPort(printer));
77-
}
69+
Connection connection = printer.getConnection();
7870

7971
try {
8072
connection.open();

PrintWrapper/src/main/java/com/zebra/printwrapper/SendDirectTask.java

+1-9
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,7 @@ private void sendPrint() {
6363
return;
6464
}
6565

66-
Connection connection = null;
67-
if(PrintWrapperHelpers.isBluetoothPrinter(printer))
68-
{
69-
connection = new BluetoothConnection(printer.address);
70-
}
71-
else
72-
{
73-
connection = new TcpConnection(printer.address, PrintWrapperHelpers.getNetworkPrinterPort(printer));
74-
}
66+
Connection connection = printer.getConnection();
7567

7668
try {
7769
connection.open();

PrintWrapper/src/main/java/com/zebra/printwrapper/SendFileTask.java

+1-9
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,7 @@ private void sendPrint() {
7171
return;
7272
}
7373

74-
Connection connection = null;
75-
if(PrintWrapperHelpers.isBluetoothPrinter(printer))
76-
{
77-
connection = new BluetoothConnection(printer.address);
78-
}
79-
else
80-
{
81-
connection = new TcpConnection(printer.address, PrintWrapperHelpers.getNetworkPrinterPort(printer));
82-
}
74+
Connection connection = printer.getConnection();
8375

8476
try {
8577
connection.open();

PrintWrapper/src/main/java/com/zebra/printwrapper/SendImageTask.java

+1-9
Original file line numberDiff line numberDiff line change
@@ -80,15 +80,7 @@ private void sendPrint() {
8080
return;
8181
}
8282

83-
Connection connection = null;
84-
if(PrintWrapperHelpers.isBluetoothPrinter(printer))
85-
{
86-
connection = new BluetoothConnection(printer.address);
87-
}
88-
else
89-
{
90-
connection = new TcpConnection(printer.address, PrintWrapperHelpers.getNetworkPrinterPort(printer));
91-
}
83+
Connection connection = printer.getConnection();
9284

9385
try {
9486
connection.open();

PrintWrapper/src/main/java/com/zebra/printwrapper/SendLinePrintTask.java

+1-9
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,7 @@ private void sendPrint() {
6666
return;
6767
}
6868

69-
Connection connection = null;
70-
if(PrintWrapperHelpers.isBluetoothPrinter(printer))
71-
{
72-
connection = new BluetoothConnection(printer.address);
73-
}
74-
else
75-
{
76-
connection = new TcpConnection(printer.address, PrintWrapperHelpers.getNetworkPrinterPort(printer));
77-
}
69+
Connection connection = printer.getConnection();
7870

7971
try {
8072
connection.open();

PrintWrapper/src/main/java/com/zebra/printwrapper/SendPDFTask.java

+1-9
Original file line numberDiff line numberDiff line change
@@ -120,15 +120,7 @@ private void sendPrint() {
120120
return;
121121
}
122122

123-
Connection connection = null;
124-
if(PrintWrapperHelpers.isBluetoothPrinter(printer))
125-
{
126-
connection = new BluetoothConnection(printer.address);
127-
}
128-
else
129-
{
130-
connection = new TcpConnection(printer.address, PrintWrapperHelpers.getNetworkPrinterPort(printer));
131-
}
123+
Connection connection = printer.getConnection();
132124

133125
try {
134126
connection.open();

PrintWrapper/src/main/java/com/zebra/printwrapper/SendZPLTask.java

+2-9
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,8 @@ private void sendPrint() {
6868
return;
6969
}
7070

71-
Connection connection = null;
72-
if(PrintWrapperHelpers.isBluetoothPrinter(printer))
73-
{
74-
connection = new BluetoothConnection(printer.address);
75-
}
76-
else
77-
{
78-
connection = new TcpConnection(printer.address, PrintWrapperHelpers.getNetworkPrinterPort(printer));
79-
}
71+
Connection connection = printer.getConnection();
72+
8073

8174
try {
8275
connection.open();

0 commit comments

Comments
 (0)