printing - Android Epson Epos TM-T20ii print PDF -
how can connect via usb , print pdf file epson tm-t20ii on android? have tried this:
public class mainactivity extends activity implements view.onclicklistener, receivelistener { private context mcontext = null; private edittext medittarget = null; private spinner mspnseries = null; private spinner mspnlang = null; private printer mprinter = null; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); mcontext = this; int[] target = { r.id.btndiscovery, r.id.btnsamplereceipt, r.id.btnsamplecoupon }; (int = 0; < target.length; i++) { button button = (button)findviewbyid(target[i]); button.setonclicklistener(this); } mspnseries = (spinner)findviewbyid(r.id.spnmodel); arrayadapter<spnmodelsitem> seriesadapter = new arrayadapter<spnmodelsitem>(this, android.r.layout.simple_spinner_item); seriesadapter.setdropdownviewresource(android.r.layout.simple_spinner_dropdown_item); seriesadapter.add(new spnmodelsitem(getstring(r.string.printerseries_m10), printer.tm_m10)); seriesadapter.add(new spnmodelsitem(getstring(r.string.printerseries_m30), printer.tm_m30)); seriesadapter.add(new spnmodelsitem(getstring(r.string.printerseries_p20), printer.tm_p20)); seriesadapter.add(new spnmodelsitem(getstring(r.string.printerseries_p60), printer.tm_p60)); seriesadapter.add(new spnmodelsitem(getstring(r.string.printerseries_p60ii), printer.tm_p60ii)); seriesadapter.add(new spnmodelsitem(getstring(r.string.printerseries_p80), printer.tm_p80)); seriesadapter.add(new spnmodelsitem(getstring(r.string.printerseries_t20), printer.tm_t20)); seriesadapter.add(new spnmodelsitem(getstring(r.string.printerseries_t60), printer.tm_t60)); seriesadapter.add(new spnmodelsitem(getstring(r.string.printerseries_t70), printer.tm_t70)); seriesadapter.add(new spnmodelsitem(getstring(r.string.printerseries_t81), printer.tm_t81)); seriesadapter.add(new spnmodelsitem(getstring(r.string.printerseries_t82), printer.tm_t82)); seriesadapter.add(new spnmodelsitem(getstring(r.string.printerseries_t83), printer.tm_t83)); seriesadapter.add(new spnmodelsitem(getstring(r.string.printerseries_t88), printer.tm_t88)); seriesadapter.add(new spnmodelsitem(getstring(r.string.printerseries_t90), printer.tm_t90)); seriesadapter.add(new spnmodelsitem(getstring(r.string.printerseries_t90kp), printer.tm_t90kp)); seriesadapter.add(new spnmodelsitem(getstring(r.string.printerseries_u220), printer.tm_u220)); seriesadapter.add(new spnmodelsitem(getstring(r.string.printerseries_u330), printer.tm_u330)); seriesadapter.add(new spnmodelsitem(getstring(r.string.printerseries_l90), printer.tm_l90)); seriesadapter.add(new spnmodelsitem(getstring(r.string.printerseries_h6000), printer.tm_h6000)); mspnseries.setadapter(seriesadapter); mspnseries.setselection(0); mspnlang = (spinner)findviewbyid(r.id.spnlang); arrayadapter<spnmodelsitem> langadapter = new arrayadapter<spnmodelsitem>(this, android.r.layout.simple_spinner_item); langadapter.setdropdownviewresource(android.r.layout.simple_spinner_dropdown_item); langadapter.add(new spnmodelsitem(getstring(r.string.lang_ank), printer.model_ank)); langadapter.add(new spnmodelsitem(getstring(r.string.lang_japanese), printer.model_japanese)); langadapter.add(new spnmodelsitem(getstring(r.string.lang_chinese), printer.model_chinese)); langadapter.add(new spnmodelsitem(getstring(r.string.lang_taiwan), printer.model_taiwan)); langadapter.add(new spnmodelsitem(getstring(r.string.lang_korean), printer.model_korean)); langadapter.add(new spnmodelsitem(getstring(r.string.lang_thai), printer.model_thai)); langadapter.add(new spnmodelsitem(getstring(r.string.lang_southasia), printer.model_southasia)); mspnlang.setadapter(langadapter); mspnlang.setselection(0); try { log.setlogsettings(mcontext, log.period_temporary, log.output_storage, null, 0, 1, log.loglevel_low); } catch (exception e) { showmsg.showexception(e, "setlogsettings", mcontext); } medittarget = (edittext)findviewbyid(r.id.edttarget); } @override protected void onactivityresult(int requestcode, final int resultcode, final intent data) { if (data != null && resultcode == result_ok) { string target = data.getstringextra(getstring(r.string.title_target)); if (target != null) { edittext medttarget = (edittext)findviewbyid(r.id.edttarget); medttarget.settext(target); } } } @override public void onclick(view v) { intent intent = null; switch (v.getid()) { case r.id.btndiscovery: intent = new intent(this, discoveryactivity.class); startactivityforresult(intent, 0); break; case r.id.btnsamplereceipt: updatebuttonstate(false); if (!runprintreceiptsequence()) { updatebuttonstate(true); } break; case r.id.btnsamplecoupon: updatebuttonstate(false); if (!runprintcouponsequence()) { updatebuttonstate(true); } break; default: // nothing break; } } private boolean runprintreceiptsequence() { if (!initializeobject()) { return false; } if (!createreceiptdata()) { finalizeobject(); return false; } if (!printdata()) { finalizeobject(); return false; } return true; } private boolean createreceiptdata() { string method = ""; bitmap logodata = bitmapfactory.decoderesource(getresources(), r.drawable.store); stringbuilder textdata = new stringbuilder(); final int barcodewidth = 2; final int barcodeheight = 100; if (mprinter == null) { return false; } try { method = "addtextalign"; mprinter.addtextalign(printer.align_center); method = "addimage"; mprinter.addimage(logodata, 0, 0, logodata.getwidth(), logodata.getheight(), printer.color_1, printer.mode_mono, printer.halftone_dither, printer.param_default, printer.compress_auto); method = "addfeedline"; mprinter.addfeedline(1); textdata.append("the store 123 (555) 555 – 5555\n"); textdata.append("store director – john smith\n"); textdata.append("\n"); textdata.append("7/01/07 16:58 6153 05 0191 134\n"); textdata.append("st# 21 op# 001 te# 01 tr# 747\n"); textdata.append("------------------------------\n"); method = "addtext"; mprinter.addtext(textdata.tostring()); textdata.delete(0, textdata.length()); textdata.append("400 oheida 3pk springf 9.99 r\n"); textdata.append("410 3 cup blk teapot 9.99 r\n"); textdata.append("445 emeril griddle/pan 17.99 r\n"); textdata.append("438 candymaker assort 4.99 r\n"); textdata.append("474 tripod 8.99 r\n"); textdata.append("433 blk logo prnted zo 7.99 r\n"); textdata.append("458 aqua microterry sc 6.99 r\n"); textdata.append("493 30l blk ff dress 16.99 r\n"); textdata.append("407 levitating desktop 7.99 r\n"); textdata.append("441 **blue overprint p 2.99 r\n"); textdata.append("476 repose 4pcpm choc 5.49 r\n"); textdata.append("461 westgate black 25 59.99 r\n"); textdata.append("------------------------------\n"); method = "addtext"; mprinter.addtext(textdata.tostring()); textdata.delete(0, textdata.length()); textdata.append("subtotal 160.38\n"); textdata.append("tax 14.43\n"); method = "addtext"; mprinter.addtext(textdata.tostring()); textdata.delete(0, textdata.length()); method = "addtextsize"; mprinter.addtextsize(2, 2); method = "addtext"; mprinter.addtext("total 174.81\n"); method = "addtextsize"; mprinter.addtextsize(1, 1); method = "addfeedline"; mprinter.addfeedline(1); textdata.append("cash 200.00\n"); textdata.append("change 25.19\n"); textdata.append("------------------------------\n"); method = "addtext"; mprinter.addtext(textdata.tostring()); textdata.delete(0, textdata.length()); textdata.append("purchased item total number\n"); textdata.append("sign , save !\n"); textdata.append("with preferred saving card\n"); method = "addtext"; mprinter.addtext(textdata.tostring()); textdata.delete(0, textdata.length()); method = "addfeedline"; mprinter.addfeedline(2); method = "addbarcode"; mprinter.addbarcode("01209457", printer.barcode_code39, printer.hri_below, printer.font_a, barcodewidth, barcodeheight); method = "addcut"; mprinter.addcut(printer.cut_feed); } catch (exception e) { showmsg.showexception(e, method, mcontext); return false; } textdata = null; return true; } private boolean runprintcouponsequence() { if (!initializeobject()) { return false; } if (!createcoupondata()) { finalizeobject(); return false; } if (!printdata()) { finalizeobject(); return false; } return true; } private boolean createcoupondata() { string method = ""; bitmap coffeedata = bitmapfactory.decoderesource(getresources(), r.drawable.coffee); bitmap wmarkdata = bitmapfactory.decoderesource(getresources(), r.drawable.wmark); final int barcodewidth = 2; final int barcodeheight = 64; final int pageareaheight = 500; final int pageareawidth = 500; final int fontaheight = 24; final int fontawidth = 12; final int barcodewidthpos = 110; final int barcodeheightpos = 70; if (mprinter == null) { return false; } try { method = "addpagebegin"; mprinter.addpagebegin(); method = "addpagearea"; mprinter.addpagearea(0, 0, pageareawidth, pageareaheight); method = "addpagedirection"; mprinter.addpagedirection(printer.direction_top_to_bottom); method = "addpageposition"; mprinter.addpageposition(0, coffeedata.getheight()); method = "addimage"; mprinter.addimage(coffeedata, 0, 0, coffeedata.getwidth(), coffeedata.getheight(), printer.param_default, printer.param_default, printer.param_default, 3, printer.param_default); method = "addpageposition"; mprinter.addpageposition(0, wmarkdata.getheight()); method = "addimage"; mprinter.addimage(wmarkdata, 0, 0, wmarkdata.getwidth(), wmarkdata.getheight(), printer.param_default, printer.param_default, printer.param_default, printer.param_default, printer.param_default); method = "addpageposition"; mprinter.addpageposition(fontawidth * 4, (pageareaheight / 2) - (fontaheight * 2)); method = "addtextsize"; mprinter.addtextsize(3, 3); method = "addtextstyle"; mprinter.addtextstyle(printer.param_default, printer.param_default, printer.true, printer.param_default); method = "addtextsmooth"; mprinter.addtextsmooth(printer.true); method = "addtext"; mprinter.addtext("free coffee\n"); method = "addpageposition"; mprinter.addpageposition((pageareawidth / barcodewidth) - barcodewidthpos, coffeedata.getheight() + barcodeheightpos); method = "addbarcode"; mprinter.addbarcode("01234567890", printer.barcode_upc_a, printer.param_default, printer.param_default, barcodewidth, barcodeheight); method = "addpageend"; mprinter.addpageend(); method = "addcut"; mprinter.addcut(printer.cut_feed); } catch (exception e) { showmsg.showexception(e, method, mcontext); return false; } return true; } private boolean printdata() { if (mprinter == null) { return false; } if (!connectprinter()) { return false; } printerstatusinfo status = mprinter.getstatus(); dispprinterwarnings(status); if (!isprintable(status)) { showmsg.showmsg(makeerrormessage(status), mcontext); try { mprinter.disconnect(); } catch (exception ex) { // nothing } return false; } try { mprinter.senddata(printer.param_default); } catch (exception e) { showmsg.showexception(e, "senddata", mcontext); try { mprinter.disconnect(); } catch (exception ex) { // nothing } return false; } return true; } private boolean initializeobject() { try { mprinter = new printer(((spnmodelsitem) mspnseries.getselecteditem()).getmodelconstant(), ((spnmodelsitem) mspnlang.getselecteditem()).getmodelconstant(), mcontext); } catch (exception e) { showmsg.showexception(e, "printer", mcontext); return false; } mprinter.setreceiveeventlistener(this); return true; } private void finalizeobject() { if (mprinter == null) { return; } mprinter.clearcommandbuffer(); mprinter.setreceiveeventlistener(null); mprinter = null; } private boolean connectprinter() { boolean isbegintransaction = false; if (mprinter == null) { return false; } try { mprinter.connect(medittarget.gettext().tostring(), printer.param_default); } catch (exception e) { showmsg.showexception(e, "connect", mcontext); return false; } try { mprinter.begintransaction(); isbegintransaction = true; } catch (exception e) { showmsg.showexception(e, "begintransaction", mcontext); } if (isbegintransaction == false) { try { mprinter.disconnect(); } catch (epos2exception e) { // nothing return false; } } return true; } private void disconnectprinter() { if (mprinter == null) { return; } try { mprinter.endtransaction(); } catch (final exception e) { runonuithread(new runnable() { @override public synchronized void run() { showmsg.showexception(e, "endtransaction", mcontext); } }); } try { mprinter.disconnect(); } catch (final exception e) { runonuithread(new runnable() { @override public synchronized void run() { showmsg.showexception(e, "disconnect", mcontext); } }); } finalizeobject(); } private boolean isprintable(printerstatusinfo status) { if (status == null) { return false; } if (status.getconnection() == printer.false) { return false; } else if (status.getonline() == printer.false) { return false; } else { ;//print available } return true; } private string makeerrormessage(printerstatusinfo status) { string msg = ""; if (status.getonline() == printer.false) { msg += getstring(r.string.handlingmsg_err_offline); } if (status.getconnection() == printer.false) { msg += getstring(r.string.handlingmsg_err_no_response); } if (status.getcoveropen() == printer.true) { msg += getstring(r.string.handlingmsg_err_cover_open); } if (status.getpaper() == printer.paper_empty) { msg += getstring(r.string.handlingmsg_err_receipt_end); } if (status.getpaperfeed() == printer.true || status.getpanelswitch() == printer.switch_on) { msg += getstring(r.string.handlingmsg_err_paper_feed); } if (status.geterrorstatus() == printer.mechanical_err || status.geterrorstatus() == printer.autocutter_err) { msg += getstring(r.string.handlingmsg_err_autocutter); msg += getstring(r.string.handlingmsg_err_need_recover); } if (status.geterrorstatus() == printer.unrecover_err) { msg += getstring(r.string.handlingmsg_err_unrecover); } if (status.geterrorstatus() == printer.autorecover_err) { if (status.getautorecovererror() == printer.head_overheat) { msg += getstring(r.string.handlingmsg_err_overheat); msg += getstring(r.string.handlingmsg_err_head); } if (status.getautorecovererror() == printer.motor_overheat) { msg += getstring(r.string.handlingmsg_err_overheat); msg += getstring(r.string.handlingmsg_err_motor); } if (status.getautorecovererror() == printer.battery_overheat) { msg += getstring(r.string.handlingmsg_err_overheat); msg += getstring(r.string.handlingmsg_err_battery); } if (status.getautorecovererror() == printer.wrong_paper) { msg += getstring(r.string.handlingmsg_err_wrong_paper); } } if (status.getbatterylevel() == printer.battery_level_0) { msg += getstring(r.string.handlingmsg_err_battery_real_end); } return msg; } private void dispprinterwarnings(printerstatusinfo status) { edittext edtwarnings = (edittext)findviewbyid(r.id.edtwarnings); string warningsmsg = ""; if (status == null) { return; } if (status.getpaper() == printer.paper_near_end) { warningsmsg += getstring(r.string.handlingmsg_warn_receipt_near_end); } if (status.getbatterylevel() == printer.battery_level_1) { warningsmsg += getstring(r.string.handlingmsg_warn_battery_near_end); } edtwarnings.settext(warningsmsg); } private void updatebuttonstate(boolean state) { button btnreceipt = (button)findviewbyid(r.id.btnsamplereceipt); button btncoupon = (button)findviewbyid(r.id.btnsamplecoupon); btnreceipt.setenabled(state); btncoupon.setenabled(state); } @override public void onptrreceive(final printer printerobj, final int code, final printerstatusinfo status, final string printjobid) { runonuithread(new runnable() { @override public synchronized void run() { showmsg.showresult(code, makeerrormessage(status), mcontext); dispprinterwarnings(status); updatebuttonstate(true); new thread(new runnable() { @override public void run() { disconnectprinter(); } }).start(); } }); } }
do need add project? use eclipse? , how do it? added epos2.jar "add external jars".
Comments
Post a Comment