|
13 | 13 | import com.zebra.sdk.printer.discovery.DiscoveredPrinter;
|
14 | 14 | import com.zebra.sdk.printer.discovery.DiscoveredPrinterBluetooth;
|
15 | 15 | import com.zebra.sdk.printer.discovery.DiscoveredPrinterNetwork;
|
| 16 | +import com.zebra.sdk.printer.discovery.DiscoveredPrinterUsb; |
16 | 17 |
|
17 | 18 | import java.io.IOException;
|
18 | 19 | import java.io.UnsupportedEncodingException;
|
@@ -90,7 +91,7 @@ public static Connection connectToPrinter(DiscoveredPrinter printer, SGDHelperCa
|
90 | 91 | if (callback != null) {
|
91 | 92 | callback.onMessage("Connecting to bluetooth Printer: " + printer.address);
|
92 | 93 | }
|
93 |
| - mConnection = new BluetoothConnection(printer.address); |
| 94 | + mConnection = printer.getConnection(); |
94 | 95 | try {
|
95 | 96 | if (callback != null) {
|
96 | 97 | callback.onMessage("Opening connection.");
|
@@ -132,7 +133,7 @@ public static Connection connectToPrinter(DiscoveredPrinter printer, SGDHelperCa
|
132 | 133 | callback.onMessage("Connecting to Network Printer: " + printer.address);
|
133 | 134 | }
|
134 | 135 | try {
|
135 |
| - mConnection = new TcpConnection(printer.address, PrintWrapperHelpers.getNetworkPrinterPort(printer)); |
| 136 | + mConnection = printer.getConnection(); |
136 | 137 |
|
137 | 138 | if (callback != null) {
|
138 | 139 | callback.onMessage("Opening connection.");
|
@@ -166,9 +167,37 @@ public static Connection connectToPrinter(DiscoveredPrinter printer, SGDHelperCa
|
166 | 167 | mConnection = null;
|
167 | 168 | }
|
168 | 169 | }
|
| 170 | + |
169 | 171 | throw new PrinterWrapperException(new Exception("SGDHelper.connectToPrinter: Could not establish connection with Network Printer."));
|
170 | 172 | }
|
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.")); |
172 | 201 | }
|
173 | 202 |
|
174 | 203 | public static String GET(String propertyName, DiscoveredPrinter discoveredPrinter, SGDHelperCallback callback) throws PrinterWrapperException {
|
|
0 commit comments