I have an Epson printer and I used the epos2_printer code (sample project) given by the SDK to integrate with my application. I copied the same code, but it never works!
However, the same thing works when I connect a sample project to my printer.
private boolean runPrintReceiptSequence() { if (!initializeObject()) { return false; } if (!createReceiptData()) { finalizeObject(); return false; } if (!printData()) { finalizeObject(); return false; } return true; } private boolean initializeObject() { try { final SpnModelsItem spnModel = new SpnModelsItem("TM-T82 Series", Printer.TM_T82); final SpnModelsItem spnLang = new SpnModelsItem("ANK", Printer.MODEL_ANK); mPrinter = new Printer(spnModel.getModelConstant(), spnLang.getModelConstant(), this); } catch (Exception e) { Log.e("Printer", e.toString()); return false; } mPrinter.setReceiveEventListener(this); return true; } private boolean createReceiptData() { String method = ""; Bitmap logoData = BitmapFactory.decodeResource(getResources(), R.drawable.logo_saltnpepper); StringBuilder textData = new StringBuilder(); final int barcodeWidth = 2; final int barcodeHeight = 100; Date currentDate = new Date(); info.saltnpepper.ordersmart2.MenuItem currItem = null; double price = 0; double total = 0; int totalQty =0; 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("SALT-N-PEPPER\n");
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 printData() { if (mPrinter == null) { return false; } if (!connectPrinter()) { return false; } PrinterStatusInfo status = mPrinter.getStatus(); dispPrinterWarnings(status); if (!isPrintable(status)) { Log.e("Printer", "Is not printable"); try { mPrinter.disconnect(); } catch (Exception ex) { // Do nothing } return false; } try { mPrinter.sendData(Printer.PARAM_DEFAULT); } catch (Exception e) { Log.e("Printer", e.getMessage()); try { mPrinter.disconnect(); } catch (Exception ex) { // Do nothing } return false; } return true; } private boolean connectPrinter() { boolean isBeginTransaction = false; if (mPrinter == null) { return false; } try { mPrinter.connect("TCP:"+mIP, Printer.PARAM_DEFAULT); } catch (Epos2Exception e) { //ShowMsg.showException(e, "connect", this); if(e.getErrorStatus() == Epos2Exception.ERR_CONNECT) { Log.e("testing", "error connect"); } if(e.getErrorStatus() == Epos2Exception.ERR_ALREADY_OPENED) { Log.e("testing", "already open"); } if(e.getErrorStatus() == Epos2Exception.ERR_ALREADY_USED) { Log.e("testing", "already used"); } if(e.getErrorStatus() == Epos2Exception.ERR_BOX_CLIENT_OVER) { Log.e("testing", "box client over"); } if(e.getErrorStatus() == Epos2Exception.ERR_BOX_COUNT_OVER) { Log.e("testing", "count over"); } if(e.getErrorStatus() == Epos2Exception.ERR_DISCONNECT) { Log.e("testing", "disconnect"); } if(e.getErrorStatus() == Epos2Exception.ERR_FAILURE) { Log.e("testing", "failure"); } if(e.getErrorStatus() == Epos2Exception.ERR_ILLEGAL) { Log.e("testing", "illegal"); } if(e.getErrorStatus() == Epos2Exception.ERR_IN_USE) { Log.e("testing", "in use"); } if(e.getErrorStatus() == Epos2Exception.ERR_MEMORY) { Log.e("testing", "memory"); } return false; } try { mPrinter.beginTransaction(); isBeginTransaction = true; } catch (Exception e) { Log.e("Printer", e.toString ()); } if (isBeginTransaction == false) { try { mPrinter.disconnect(); } catch (Epos2Exception e) { // Do nothing return false; } } return true; }
It always gives me an ERR_CONNECT exception on printer.connect inside the connectprinter function.
What am I doing wrong?
This code works great with a sample application. PS: I tried connecting this application before connecting the sample application to check if the sample application supports the connection and does not allow other applications to connect, but this is not the case. Epson Help cannot provide further assistance.
My AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="xyz" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="21" android:targetSdkVersion="21" /> <application android:allowBackup="true" android:icon="@drawable/icon" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".MenuActivity" > </activity> <activity android:name=".SaltnPepperActivity" android:label="@string/title_activity_saltn_pepper" > </activity> <activity android:name=".FinalOrder" ></activity> <activity android:name=".ZinVietActivity" > </activity> <activity android:name="com.epson.epos2_printer.DiscoverActivity" ></activity> </application>