ZD420t issue with printing over android API

Tags: 

Hello All,
I have Zebra ZD420t and I've developed simple app for printing. I've developed below code using Android API and samples from Zebra repo:

public class ZebraPrinterHandler extends Handler {
private Connection printerConnection;

public ZebraPrinterHandler(String macAddress){
printerConnection = new BluetoothConnectionInsecure(macAddress);
}

private void tryToPrintLabel(Message msg) {
try {
String zplData = message.getZplData();

printerConnection.open();
//below code same label twice on purpose- it is not a mistake ;)
printerConnection.write(zplData.getBytes());
SystemClock.sleep(2000);
printerConnection.write(zplData.getBytes());
SystemClock.sleep(2000);

//perform important stuff only if label is printed correctly

} catch (ConnectionException e) {
//do some stuff
}
//do other stuff
}
}
}

In general it works perfectly. Labels are printed as desired and sometimes, when there are connection issues, exception is thrown and I can perform appropriate code to handle it.

If no exception is thrown, I assume label was printed correctly and line "//perform important stuff only if label is printed correctly" is executed.
Unfortunately, in very rare cases (once every few hundreds labels) situation happens, that no label is printed and no exception is thrown. This is serious issue for me because code in line "//perform important stuff ..." shouldn't be executed in case no label was printed- this create system inconsistency.
I was trying to find a way in API, how to receive confirmation that label was printed successfully (other than lack of exception) but I don't see any option for that.
Can you please advice on programmatic way to confirm the label was printed other than observing that no exception was thrown- this doesn't work correctly.

Regards
Andrzej

Steven Si
Reformatted your code snippet

Reformatted your code snippet as below for better readability.

public class ZebraPrinterHandler extends Handler {
    private Connection printerConnection;
	
	public ZebraPrinterHandler(String macAddress){
        printerConnection = new BluetoothConnectionInsecure(macAddress);
    }

	private void tryToPrintLabel(Message msg) {
			try {
				String zplData = message.getZplData();

				printerConnection.open();
				//below code same label twice on purpose- it is not a mistake ;)
				printerConnection.write(zplData.getBytes());
				SystemClock.sleep(2000);
				printerConnection.write(zplData.getBytes());
				SystemClock.sleep(2000);
				
				//perform important stuff only if label is printed correctly
				
			} catch (ConnectionException e) {
				//do some stuff
			}
				//do other stuff
	}
}

There are a few things missing in your code snippets. Before sending each print job to the printer, we should check the connection status and the printer status (printerConnection.isConnected(), getCurrentStatus()) to ensure that the printer is ready before we send anything to the printer. After the printing is done, the connection should be closed (printerConnection.close()) to ensure we return the resource so we can have a clean start next time. Adding 2,000 ms delay is not ideal. I'd suggest to check the printer status (getCurrentStatu()). Only when the printer is in ready state, we send the next print job.

Vote: 
Vote up!
Vote down!

Points: 0

You voted ‘up’


Log in to post comments