Chesneycar
Chesneycar

Reputation: 563

My android application prints only 1 ticket every 5 seconds

Firstly, thank you for your time reading my question. I'm still a noob when it comes to programming in Java and I am grateful for any advice or assistance.

The App

I wrote a small android application that prints out jobcard tickets via a bluetooth printer.

The Problem

My application can only print one ticket every 5 seconds, meaning if a second jobcard is captured within 5 seconds the application does not print a ticket.

What I want

I really want the application to consecutively print out a ticket for every jobcard captured.

I think I got lost in the details with AsyncTasks,Threads (Bluetooth comms) etc. and when to use what class.

Any guidelines on best practices how to use these classes are more than welcome. I have pasted some of the source code below which I suspect might be the root of the problem.

Equipment :

Zebra RW420 Printer (Zebra SDK)

What the log says :

03-06 20:17:04.328: W/BluetoothAdapter(25552): getBluetoothService() called with no BluetoothManagerCallback 03-06 20:17:04.328: W/BluetoothAdapter(25552): getBluetoothService() called with no BluetoothManagerCallback 03-06 20:17:04.386: D/dalvikvm(25552): GC_CONCURRENT freed 133K, 9% free 4079K/4468K, paused 17ms+23ms, total

Source Code :

MainScreen.java

public class MainScreen extends Activity {

submitBtn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub                                              
               //Do printing
                    String tpl = "Hello World!";
                    new PrintTask().execute(tpl);                    
            }
        });       
   }

//Async Printing Task
private class PrintTask extends AsyncTask<String, Integer, String> {
            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                uiHelper.showLoadingDialog("Printing, please wait...");                                 
            }

            @Override
            protected String doInBackground(String... params) {
                String msg = params[0];                                 
                print(msg);                                                                 
                publishProgress(1);                 
                return "All Done!";
            }

            @Override
            protected void onProgressUpdate(Integer... values) {
                super.onProgressUpdate(values);
                uiHelper.updateLoadingDialog("Printing jobcard " + values[0]);              
            }

            @Override
            protected void onPostExecute(String result) {
                super.onPostExecute(result);
                uiHelper.dismissLoadingDialog();
                finish(); //close the window
            }
        }

    public void print(String tpl) {
        final String msg = tpl;     
        //runOnUiThread(new Runnable()
        new Thread(new Runnable() {
            public void run() {                                                             
                Looper.prepare();
                printer_ob.printMessage(msg);               
                Looper.loop();
                Looper.myLooper().quit();                
            }
        }).start();                     
    }

}

The ZebraPrinter helper class I use :

public class ZebraPrinterHelper {
    Context context;
    private ZebraPrinterConnection zebraPrinterConnection;
    private RadioButton btRadioButton;
    private ZebraPrinter printer;
    private TextView statusField;
    private EditText macAddress, ipDNSAddress, portNumber;
    private Button testButton;  

    public ZebraPrinterHelper(Context c) {
        context = c;
    }

    public ZebraPrinter connect() {

        zebraPrinterConnection = null;      
        zebraPrinterConnection = new BluetoothPrinterConnection(SettingsHelper.getBluetoothAddress(context));               

        try {
            zebraPrinterConnection.open();          
        } catch (ZebraPrinterConnectionException e) {       
            Toast.makeText(context, "Comm Error! Disconnecting", 500).show();
            DemoSleeper.sleep(1000);
            disconnect();
        }
        ZebraPrinter printer = null;

        if (zebraPrinterConnection.isConnected()) {
            try {
                printer = ZebraPrinterFactory.getInstance(zebraPrinterConnection);
                //setStatus("Determining Printer Language", Color.YELLOW);
                PrinterLanguage pl = printer.getPrinterControlLanguage();
                //    setStatus("Printer Language " + pl, Color.BLUE);
            } catch (ZebraPrinterConnectionException e) {
                //"Unknown Printer Language");
                Toast.makeText(context, "Error, Unknown printer language", 500).show();
                DemoSleeper.sleep(1000);
                printer = null;
                DemoSleeper.sleep(1000);
                disconnect();
            } catch (ZebraPrinterLanguageUnknownException e) {
                //setStatus("Unknown Printer Language", Color.RED);
                Toast.makeText(context, "Error, Unknown printer language", 500).show();             
                printer = null;
                DemoSleeper.sleep(1000);
                disconnect();
            }
        }

        return printer;         
    }

    private void writeMessage(byte[] message) {
        //message in bytes      
        try {           
            zebraPrinterConnection.write(message);              
            DemoSleeper.sleep(1500);
            if (zebraPrinterConnection instanceof BluetoothPrinterConnection) {
                String friendlyName = ((BluetoothPrinterConnection) zebraPrinterConnection).getFriendlyName();                  
                DemoSleeper.sleep(500);
            }
        } catch (ZebraPrinterConnectionException e) {
            //helper.showErrorDialogOnGuiThread("Error:" + e.getMessage());
            Log.d("Error",e.getMessage());
        } finally {
            disconnect();
        }
    }



        public void printMessage(String message) {
            byte[] msg = null;          
            msg = message.getBytes();

            //check connections

            printer = connect();
            if (printer != null) {
                writeMessage(msg);
            } else {
                disconnect();
            }

        }

        public void disconnect() {
            try {           
                if (zebraPrinterConnection != null) {
                    zebraPrinterConnection.close();
                }               
            } catch (ZebraPrinterConnectionException e) {
                //setStatus("COMM Error! Disconnected", Color.RED);
            } finally {

            }
        }
    }

Upvotes: 3

Views: 1818

Answers (2)

Ovi Tisler
Ovi Tisler

Reputation: 6473

You have a bunch of DemoSleeper.sleep() calls all throughout your code which sounds like it's going to block execution for some period of time. It sleeps for 1 second after open() and then a couple seconds after write(), and the communication to the printer surely takes some time.

Is this what's causing the slow printing? Can you try removing those? You won't need them for printing

Upvotes: 0

the-ginger-geek
the-ginger-geek

Reputation: 7081

I think you will have to use a IntentService rather than a AsyncTask. IntentServices handle asynchronous requests and also has built in features for queuing. Have a look at this link for more info on IntentServices.

Upvotes: 1

Related Questions