简体   繁体   中英

Can not use the methods to connect printer Zebra MZ 220

I have the following code to print in my zebra mz 220 using my android 2.3.7:

public class Printing extends Activity {
ProgressDialog dialog;
ZebraPrinterConnection zebraPrinterConnection;
ZebraPrinter zebra;
String mac;

Handler handlerWorking = new Handler(){
    @Override
    public void handleMessage(Message msg){
        String returnedValue = (String)msg.obj;
        //textV.setText("Returned by thread Circle:" + returnedValue);
    }
};

Handler handlerDone = new Handler(){
    @Override
    public void handleMessage(Message msg){
        String returnedValue = (String)msg.obj;
        //textV.setText(returnedValue);
    }
};

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Bundle bundle = this.getIntent().getExtras();
    String mac = bundle.getString("param1");
    this.mac = mac;

    System.out.println(mac);
    showDialog(0);
    //finish();
}


@Override 
public Dialog onCreateDialog(int id){
    final Runnable threadMethod = new Runnable() {
        public void run(){
            try{
                connect();
            }catch(Throwable e){

            }
            Message msg = handlerDone.obtainMessage(1, "DONE!!!");
            handlerDone.sendMessage(msg);
            dialog.dismiss();
        }
    };

    dialog = new ProgressDialog(this);
    dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
    dialog.setMessage("Loading...");
    Thread thread = new Thread(null, threadMethod, "Thread1");
    thread.start();
    return dialog;
}

public void connect() {
    Looper.prepare();
    zebraPrinterConnection = null;
    zebraPrinterConnection = new BluetoothPrinterConnection(mac);

    try {
        zebraPrinterConnection.open();
    } catch (ZebraPrinterConnectionException e) {
        disconnect();
    }

    if (zebraPrinterConnection.isConnected()) {
        try {
            zebra = ZebraPrinterFactory.getInstance(zebraPrinterConnection);
            sendLabel();
        } catch (ZebraPrinterConnectionException e) {
            zebra = null;
            disconnect();
        } catch (ZebraPrinterLanguageUnknownException e) {
            zebra = null;
            disconnect();
        }
    }
    Looper.loop();
    Looper.myLooper().quit();
}

public void disconnect() {
    try {
        if (zebraPrinterConnection != null) {
            zebraPrinterConnection.close();
        }
    } catch (ZebraPrinterConnectionException e) {

    } 
}

private byte[] getLabel() {
    PrinterLanguage printerLanguage = zebra.getPrinterControlLanguage();
    byte[] configLabel = null;
    if (printerLanguage == PrinterLanguage.ZPL) {
        configLabel = "^XA^FO17,16^GB379,371,8^FS^FT65,255^A0N,135,134^FDTEST^FS^XZ".getBytes();
    } else if (printerLanguage == PrinterLanguage.CPCL) {
        String cpclConfigLabel = "! 0 200 200 50 1\r\n" + "ON-FEED IGNORE\r\n" + "T 5 0 0 0 Hola\r\n" + "PRINT\r\n";
        configLabel = cpclConfigLabel.getBytes();
    }
    return configLabel;
}

private void sendLabel() {
    try {
        byte[] configLabel = getLabel();
        zebraPrinterConnection.write(configLabel);
    } catch (ZebraPrinterConnectionException e) {

    } finally {
        disconnect();
    }
}

The problem is that i get the next errors on my logCat:

06-12 11:08:03.327: E/AndroidRuntime(14323): FATAL EXCEPTION: main
06-12 11:08:03.327: E/AndroidRuntime(14323): java.lang.VerifyError: com.api.printer.zebra.Printing
06-12 11:08:03.327: E/AndroidRuntime(14323):    at java.lang.Class.newInstanceImpl(Native Method)
06-12 11:08:03.327: E/AndroidRuntime(14323):    at java.lang.Class.newInstance(Class.java:1409)
06-12 11:08:03.327: E/AndroidRuntime(14323):    at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
06-12 11:08:03.327: E/AndroidRuntime(14323):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1672)
06-12 11:08:03.327: E/AndroidRuntime(14323):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1784)
06-12 11:08:03.327: E/AndroidRuntime(14323):    at android.app.ActivityThread.access$1500(ActivityThread.java:123)
06-12 11:08:03.327: E/AndroidRuntime(14323):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:939)
06-12 11:08:03.327: E/AndroidRuntime(14323):    at android.os.Handler.dispatchMessage(Handler.java:99)
06-12 11:08:03.327: E/AndroidRuntime(14323):    at android.os.Looper.loop(Looper.java:130)
06-12 11:08:03.327: E/AndroidRuntime(14323):    at android.app.ActivityThread.main(ActivityThread.java:3835)
06-12 11:08:03.327: E/AndroidRuntime(14323):    at java.lang.reflect.Method.invokeNative(Native Method)
06-12 11:08:03.327: E/AndroidRuntime(14323):    at java.lang.reflect.Method.invoke(Method.java:507)
06-12 11:08:03.327: E/AndroidRuntime(14323):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847)
06-12 11:08:03.327: E/AndroidRuntime(14323):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
06-12 11:08:03.327: E/AndroidRuntime(14323):    at dalvik.system.NativeStart.main(Native Method)

If i comment the disconnect method, the sendLabel method and the getLabel method the application does not crash but the next error line appears:

06-12 11:13:35.207: E/dalvikvm(15869): Could not find class 'com.zebra.android.comm.BluetoothPrinterConnection', referenced from method com.api.printer.zebra.Printing.connect

I have the library of zebra(ZSDK_API.jar) in my referenced libraries, what is happenning? Thanks for your time.

Depending on the version of ADT you are using (if you are indeed using Eclipse), if you are using the latest version it should be sufficient to put the ZSDK_API.jar in your libs/ directory. This should get picked up by the Android build process. The jar should then show up in the Android Dependencies library in your Android project automatically.

Check this URL for more info: http://android.foxykeep.com/dev/how-to-fix-the-classdefnotfounderror-with-adt-17

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM