How to send parameter to this java class? -
i new on java, , trying implement print test using zebra sdk. zebra sdk (at end of class there "getconfiglabel"), prints constant "test", want send variable insted of constant. this, understand have send parameter class, not clear me how done. thanks.
here class:
/*********************************************** * confidential , proprietary * * source code , other information contained herein confidential , exclusive property of * zih corp. , subject terms , conditions in end user license agreement. * source code, , other information contained herein, shall not copied, reproduced, published, * displayed or distributed, in whole or in part, in medium, means, purpose except * expressly permitted under such license agreement. * * copyright zih corp. 2012 * * rights reserved ***********************************************/ package com.zebra.android.devdemo.connectivity; import android.app.activity; import android.graphics.color; import android.os.bundle; import android.os.looper; import android.view.view; import android.view.view.onclicklistener; import android.widget.button; import android.widget.edittext; import android.widget.radiobutton; import android.widget.radiogroup; import android.widget.radiogroup.oncheckedchangelistener; import android.widget.textview; import com.zebra.android.devdemo.r; import com.zebra.sdk.comm.bluetoothconnection; import com.zebra.sdk.comm.connection; import com.zebra.sdk.comm.connectionexception; import com.zebra.sdk.comm.tcpconnection; import com.zebra.sdk.printer.printerlanguage; import com.zebra.sdk.printer.zebraprinter; import com.zebra.sdk.printer.zebraprinterfactory; import com.zebra.sdk.printer.zebraprinterlanguageunknownexception; public class connectivitydemo extends activity { private connection printerconnection; private radiobutton btradiobutton; private zebraprinter printer; private textview statusfield; private edittext macaddress, ipdnsaddress, portnumber, vtexttoprint; private button testbutton; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.connection_screen_with_status); ipdnsaddress = (edittext) this.findviewbyid(r.id.ipaddressinput); ipdnsaddress.settext(settingshelper.getip(this)); vtexttoprint = (edittext) this.findviewbyid(r.id.texttoprint); portnumber = (edittext) this.findviewbyid(r.id.portinput); portnumber.settext(settingshelper.getport(this)); macaddress = (edittext) this.findviewbyid(r.id.macinput); macaddress.settext(settingshelper.getbluetoothaddress(this)); statusfield = (textview) this.findviewbyid(r.id.statustext); btradiobutton = (radiobutton) this.findviewbyid(r.id.bluetoothradio); testbutton = (button) this.findviewbyid(r.id.testbutton); testbutton.setonclicklistener(new onclicklistener() { public void onclick(view v) { new thread(new runnable() { public void run() { enabletestbutton(false); looper.prepare(); doconnectiontest(); looper.loop(); looper.mylooper().quit(); } }).start(); } }); radiogroup radiogroup = (radiogroup) this.findviewbyid(r.id.radiogroup); radiogroup.setoncheckedchangelistener(new oncheckedchangelistener() { public void oncheckedchanged(radiogroup group, int checkedid) { if (checkedid == r.id.bluetoothradio) { toggleeditfield(macaddress, true); toggleeditfield(portnumber, false); toggleeditfield(ipdnsaddress, false); } else { toggleeditfield(portnumber, true); toggleeditfield(ipdnsaddress, true); toggleeditfield(macaddress, false); } } }); } private void toggleeditfield(edittext edittext, boolean set) { /* * note: disabled edittext fields may still focus other means, , allow text input. * see http://code.google.com/p/android/issues/detail?id=2771 */ edittext.setenabled(set); edittext.setfocusable(set); edittext.setfocusableintouchmode(set); } @override protected void onstop() { super.onstop(); if (printerconnection != null && printerconnection.isconnected()) { disconnect(); } } private void enabletestbutton(final boolean enabled) { runonuithread(new runnable() { public void run() { testbutton.setenabled(enabled); } }); } private boolean isbluetoothselected() { return btradiobutton.ischecked(); } public zebraprinter connect() { setstatus("connecting...", color.yellow); printerconnection = null; if (isbluetoothselected()) { printerconnection = new bluetoothconnection(getmacaddressfieldtext()); settingshelper.savebluetoothaddress(this, getmacaddressfieldtext()); } else { try { int port = integer.parseint(gettcpportnumber()); printerconnection = new tcpconnection(gettcpaddress(), port); settingshelper.saveip(this, gettcpaddress()); settingshelper.saveport(this, gettcpportnumber()); } catch (numberformatexception e) { setstatus("port number invalid", color.red); return null; } } try { printerconnection.open(); setstatus("connected", color.green); } catch (connectionexception e) { setstatus("comm error! disconnecting", color.red); demosleeper.sleep(1000); disconnect(); } zebraprinter printer = null; if (printerconnection.isconnected()) { try { printer = zebraprinterfactory.getinstance(printerconnection); setstatus("determining printer language", color.yellow); printerlanguage pl = printer.getprintercontrollanguage(); setstatus("printer language " + pl, color.blue); } catch (connectionexception e) { setstatus("unknown printer language", color.red); printer = null; demosleeper.sleep(1000); disconnect(); } catch (zebraprinterlanguageunknownexception e) { setstatus("unknown printer language", color.red); printer = null; demosleeper.sleep(1000); disconnect(); } } return printer; } public void disconnect() { try { setstatus("disconnecting", color.red); if (printerconnection != null) { printerconnection.close(); } setstatus("not connected", color.red); } catch (connectionexception e) { setstatus("comm error! disconnected", color.red); } { enabletestbutton(true); } } private void setstatus(final string statusmessage, final int color) { runonuithread(new runnable() { public void run() { statusfield.setbackgroundcolor(color); statusfield.settext(statusmessage); } }); demosleeper.sleep(200); } private string getmacaddressfieldtext() { return macaddress.gettext().tostring(); } private string gettcpaddress() { return ipdnsaddress.gettext().tostring(); } private string gettcpportnumber() { return portnumber.gettext().tostring(); } private void doconnectiontest() { printer = connect(); if (printer != null) { sendtestlabel(); } else { disconnect(); } } private void sendtestlabel() { try { byte[] configlabel = getconfiglabel(); printerconnection.write(configlabel); setstatus("sending data", color.blue); demosleeper.sleep(500); if (printerconnection instanceof bluetoothconnection) { string friendlyname = ((bluetoothconnection) printerconnection).getfriendlyname(); setstatus(friendlyname, color.magenta); demosleeper.sleep(500); } } catch (connectionexception e) { setstatus(e.getmessage(), color.red); } { disconnect(); } } /* * returns command test label depending on printer control language * test label box word "test" inside of * * _________________________ * | | * | | * | test | * | | * | | * |_______________________| * * */ private byte[] getconfiglabel() { printerlanguage printerlanguage = printer.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 406 1\r\n" + "on-feed ignore\r\n" + "box 20 20 380 380 8\r\n" + "t 0 6 137 177 test\r\n" + "print\r\n"; configlabel = cpclconfiglabel.getbytes(); } return configlabel; } }
private byte[] getconfiglabel(string parameter) { ..... string cpclconfiglabel = "! 0 200 200 406 1\r\n" + "on-feed ignore\r\n" + "box 20 20 380 380 8\r\n" + "t 0 6 137 177 " + parameter + "r\n" + "print\r\n"; ....... }
this how take in argument method.
when calling method getconfiglabel, make sure pass in parameter string, if not causes error.
byte[] configlabel = getconfiglabel("test");
Comments
Post a Comment