Skip to content

Commit

Permalink
Added USB Printer Handling
Browse files Browse the repository at this point in the history
  • Loading branch information
ltrudu committed Dec 20, 2024
1 parent eedcba5 commit 0ee3cb3
Show file tree
Hide file tree
Showing 11 changed files with 64 additions and 69 deletions.
10 changes: 10 additions & 0 deletions .idea/deploymentTargetSelector.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions PrintWrapper/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ plugins {
ext {
PUBLISH_GROUP_ID = 'com.zebra.printwrapper'
PUBLISH_ARTIFACT_ID = 'printwrapper'
PUBLISH_VERSION = '1.17'
PUBLISH_VERSION = '1.19'
}

android {
Expand All @@ -20,8 +20,8 @@ android {
defaultConfig {
minSdkVersion 24
targetSdkVersion 34
versionCode 17
versionName "1.17"
versionCode 19
versionName "1.19"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles "consumer-rules.pro"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.zebra.sdk.printer.discovery.DiscoveredPrinter;
import com.zebra.sdk.printer.discovery.DiscoveredPrinterBluetooth;
import com.zebra.sdk.printer.discovery.DiscoveredPrinterNetwork;
import com.zebra.sdk.printer.discovery.DiscoveredPrinterUsb;

import java.util.Map;

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

/**
* Verify if printer is a usb printer
* @param printer
* @return true if printer is a network printer
*/
public static boolean isUSBPrinter(DiscoveredPrinter printer)
{
return printer instanceof DiscoveredPrinterUsb;
}

/**
* Get network printer port
* @param printer
Expand Down
35 changes: 32 additions & 3 deletions PrintWrapper/src/main/java/com/zebra/printwrapper/SGDHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import com.zebra.sdk.printer.discovery.DiscoveredPrinter;
import com.zebra.sdk.printer.discovery.DiscoveredPrinterBluetooth;
import com.zebra.sdk.printer.discovery.DiscoveredPrinterNetwork;
import com.zebra.sdk.printer.discovery.DiscoveredPrinterUsb;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
Expand Down Expand Up @@ -90,7 +91,7 @@ public static Connection connectToPrinter(DiscoveredPrinter printer, SGDHelperCa
if (callback != null) {
callback.onMessage("Connecting to bluetooth Printer: " + printer.address);
}
mConnection = new BluetoothConnection(printer.address);
mConnection = printer.getConnection();
try {
if (callback != null) {
callback.onMessage("Opening connection.");
Expand Down Expand Up @@ -132,7 +133,7 @@ public static Connection connectToPrinter(DiscoveredPrinter printer, SGDHelperCa
callback.onMessage("Connecting to Network Printer: " + printer.address);
}
try {
mConnection = new TcpConnection(printer.address, PrintWrapperHelpers.getNetworkPrinterPort(printer));
mConnection = printer.getConnection();

if (callback != null) {
callback.onMessage("Opening connection.");
Expand Down Expand Up @@ -166,9 +167,37 @@ public static Connection connectToPrinter(DiscoveredPrinter printer, SGDHelperCa
mConnection = null;
}
}

throw new PrinterWrapperException(new Exception("SGDHelper.connectToPrinter: Could not establish connection with Network Printer."));
}
throw new PrinterWrapperException(new Exception("SGDHelper.connectToPrinter: Printer connexion type not supported. USB ?"));
else if(printer instanceof DiscoveredPrinterUsb) {
try {
mConnection = printer.getConnection();
mConnection.open();
if (mConnection.isConnected() == true) {
if (callback != null) {
callback.onMessage("Connection opened with success.");
}
return mConnection;
} else {
if (callback != null) {
callback.onMessage("Error:Could not connect to printer.");
}

}
} catch (Exception e) {
e.printStackTrace();
try {
mConnection.close();
mConnection = null;
} catch (ConnectionException ex) {
ex.printStackTrace();
mConnection = null;
}
}
throw new PrinterWrapperException(new Exception("SGDHelper.connectToPrinter: Could not establish connection with USB Printer."));
}
throw new PrinterWrapperException(new Exception("SGDHelper.connectToPrinter: Printer connexion type not supported."));
}

public static String GET(String propertyName, DiscoveredPrinter discoveredPrinter, SGDHelperCallback callback) throws PrinterWrapperException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,7 @@ private void sendPrint() {
return;
}

Connection connection = null;
if(PrintWrapperHelpers.isBluetoothPrinter(printer))
{
connection = new BluetoothConnection(printer.address);
}
else
{
connection = new TcpConnection(printer.address, PrintWrapperHelpers.getNetworkPrinterPort(printer));
}
Connection connection = printer.getConnection();

try {
connection.open();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,7 @@ private void sendPrint() {
return;
}

Connection connection = null;
if(PrintWrapperHelpers.isBluetoothPrinter(printer))
{
connection = new BluetoothConnection(printer.address);
}
else
{
connection = new TcpConnection(printer.address, PrintWrapperHelpers.getNetworkPrinterPort(printer));
}
Connection connection = printer.getConnection();

try {
connection.open();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,7 @@ private void sendPrint() {
return;
}

Connection connection = null;
if(PrintWrapperHelpers.isBluetoothPrinter(printer))
{
connection = new BluetoothConnection(printer.address);
}
else
{
connection = new TcpConnection(printer.address, PrintWrapperHelpers.getNetworkPrinterPort(printer));
}
Connection connection = printer.getConnection();

try {
connection.open();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,7 @@ private void sendPrint() {
return;
}

Connection connection = null;
if(PrintWrapperHelpers.isBluetoothPrinter(printer))
{
connection = new BluetoothConnection(printer.address);
}
else
{
connection = new TcpConnection(printer.address, PrintWrapperHelpers.getNetworkPrinterPort(printer));
}
Connection connection = printer.getConnection();

try {
connection.open();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,7 @@ private void sendPrint() {
return;
}

Connection connection = null;
if(PrintWrapperHelpers.isBluetoothPrinter(printer))
{
connection = new BluetoothConnection(printer.address);
}
else
{
connection = new TcpConnection(printer.address, PrintWrapperHelpers.getNetworkPrinterPort(printer));
}
Connection connection = printer.getConnection();

try {
connection.open();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,15 +120,7 @@ private void sendPrint() {
return;
}

Connection connection = null;
if(PrintWrapperHelpers.isBluetoothPrinter(printer))
{
connection = new BluetoothConnection(printer.address);
}
else
{
connection = new TcpConnection(printer.address, PrintWrapperHelpers.getNetworkPrinterPort(printer));
}
Connection connection = printer.getConnection();

try {
connection.open();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,8 @@ private void sendPrint() {
return;
}

Connection connection = null;
if(PrintWrapperHelpers.isBluetoothPrinter(printer))
{
connection = new BluetoothConnection(printer.address);
}
else
{
connection = new TcpConnection(printer.address, PrintWrapperHelpers.getNetworkPrinterPort(printer));
}
Connection connection = printer.getConnection();


try {
connection.open();
Expand Down

0 comments on commit 0ee3cb3

Please sign in to comment.