How to run ZPL on thread? - printimage is non blocking, but seems to die if I call Thread.sleep or any other printer status c...

// Expert user has replied.
G Greg Mort 2 years 11 months ago
3 2 0

If I do this
 
Nothing happens
new Thread(new Runnable() {    public void run() {   Connection connection;   String hardcodedmac = "ac3fa446767d";   connection = new BluetoothConnection(hardcodedmac);     try {    connection.open();   ZebraPrinter printer = ZebraPrinterFactory.getInstance(connection);   printer.printImage(new ZebraImageAndroid(sigImage), 0, 0,sigImage.getWidth()/4, sigImage.getHeight()/4, true);     } catch (ConnectionException e) {   e.printStackTrace();   } catch (ZebraPrinterLanguageUnknownException e) {   e.printStackTrace();   } finally {    try {   connection.close();   } catch (ConnectionException e) {   e.printStackTrace();   }    }   }    }).run(); 
 
If I add a thread.sleep, or PrinterStatus printerStatus = printer.getCurrentStatus();
it stops printting.... if I make a counter that counts to 100000 with no sleep, then it prints...
 
What is going on? Why is this happening?

Please register or login to post a reply

2 Replies

G Greg Mort

THis is the only way I seem to be able to get it to print, setting a timer for 10seconds to make sure the image fully printed...

void ConnectAndPrint(final MainActivity ma, final Bitmap sigImage,final Bitmap canImage)
{
   new Thread(new Runnable() {
   public void run() {
   boolean bPrint = true;
   int counter = 0;
   int maxtime = 10;
   int step = 0;

   while (bPrint) {
  step++;
  System.out.println("step:" + step);
   switch (step) {
   case 1:
   new Thread(new Runnable() {
   public void run() {
   try {
   printer.printImage(new ZebraImageAndroid(sigImage), 0, 0, sigImage.getWidth() / 4, sigImage.getHeight() / 4, false);
  } catch (ConnectionException e) {
  e.printStackTrace();
  }
  }
  }).run();
   break;
   case 2:

  System.out.println("counter:" + counter);
   //waiting 10 seconds   try {
  Thread.sleep(1000);
  } catch (InterruptedException e) {
  e.printStackTrace();
  }
  counter++;
   if (counter > maxtime) {
  counter = 0;
  System.out.println("counterMAX" + counter);
   // step++;   } else   step--; //reset it back to us   break;

   case 3:
   new Thread(new Runnable() {
   public void run() {
  sendTestLabelWithManyJobs(connection, ma);

  }
  }).run();
   break;
   case 4:

  System.out.println("counter:" + counter);
   //waiting 10 seconds   try {
  Thread.sleep(1000);
  } catch (InterruptedException e) {
  e.printStackTrace();
  }
  counter++;
   if (counter > maxtime) {
  counter = 0;
  System.out.println("counterMAX" + counter);
   // step++;   } else   step--; //reset it back to us   break;
   case 5:
  bPrint = false;
   break;

  }
  }
  }
  }).run();

M Manuel Caicedo-Rivera

Hi Greg,

Please, could you help us to understand your use case, could you explain us why you need to use threads for connection.

Logic sequence expected for each thread:
try{
connection.open();
ZebraPrinter printer = ZebraPrinterFactory.getInstance(connection);
PrinterStatus printerStatus = zebraPrinter.getCurrentStatus();

if (printerStatus.isReadyToPrint)
{
  printer.printImage(new ZebraImageAndroid(sigImage), 0, 0,sigImage.getWidth()/4, sigImage.getHeight()/4, true); 
}else if (printerStatus.isHeadOPen){
//message
}
}
} catch (ConnectionException e) { 
  e.printStackTrace();
} catch (ZebraPrinterLanguageUnknownException e) { 
  e.printStackTrace();
} finally { 
try { 
connection.close();
  } catch (ConnectionException e) { 
  e.printStackTrace();
}
I would recommend to review the Zebra's Best practices document below

MC

CONTACT
Can’t find what you’re looking for?