2017-02-26 49 views
6

Mam drukarkę Epson i użyłem kodu epos2_printer (przykładowy projekt) podanego w pakiecie SDK do integracji z moją aplikacją. Skopiowałem ten sam kod, ale nigdy nie działa!Połączenie drukarki Epson kończy się niepowodzeniem ERR_CONN

Jednak to samo działa po podłączeniu przykładowego projektu do mojej drukarki.

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"); 
     //textData.append("STORE DIRECTOR – John Smith\n"); 
     textData.append("\n"); 
     textData.append((new SimpleDateFormat("dd/MM/yy HH:mm:ss")).format(currentDate).toString() + "\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()); 
     if(alFinalOrder != null) 
     { 
      for(int i=0; i < alFinalOrder.size(); i++) 
      { 
       currItem = alFinalOrder.get(i); 
       textData.append(currItem.getName()+" "+currItem.getQty()+" "+currItem.getPrice()+"\n"); 

       //calculate total quantity 
       totalQty = totalQty + currItem.getQty(); 

       //calculate price 
       double dPrice = currItem.getQty()*Double.parseDouble(currItem.getPrice().substring(1)); 
       total = total + dPrice; 
       total = Math.round(total*100.0)/100.0; 

      } 
     } 
     textData.append("------------------------------\n"); 
     method = "addText"; 
     mPrinter.addText(textData.toString()); 
     textData.delete(0, textData.length()); 

     textData.append("TOTAL     "+"\n"); 
     textData.append("TAX      "+"\n"); 
     method = "addText"; 
     mPrinter.addText(textData.toString()); 
     textData.delete(0, textData.length()); 

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; 
} 

Zawsze daje mi wyjątek ERR_CONNECT na printer.connect wewnątrz funkcji connectprinter.

Co robię źle?

Ten kod działa poprawnie w przypadku przykładowej aplikacji. P.S: Próbowałem podłączyć tę aplikację przed podłączeniem przykładowej aplikacji, aby sprawdzić, czy przykładowa aplikacja utrzymuje połączenie i nie pozwala innym aplikacjom na połączenie, ale tak nie jest. Pomoc firmy Epson nie jest w stanie zapewnić dalszej pomocy.

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> 

+0

jaki tryb używasz usb otg itp. – Killer

+0

Jeśli otrzymujesz błąd połączenia upewnij się, że używasz właściwej nazwy modelu i innych stałych. Jeśli korzystasz z micro USB, upewnij się, że dostałeś ścieżkę do tcp, taką jak tcp: //../../usb1. Ta informacja może zostać uzyskana przez discoverabilty zgodnie z definicją w dokumencie SDK doc i przykładową aplikacją – Killer

Odpowiedz

1

Wygląda na to, że nie ma odpowiednich uprawnień w swoim manifeście. Spróbuj umieścić je w swoim projekcie pod manifest tagu:

<uses-permission android:name="android.permission.BLUETOOTH"/>

<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>

<uses-permission android:name="android.permission.INTERNET"/>

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

Mają też

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>

pozwolenie, ale nie jestem pewien, czy tego potrzebujesz. Myślę, że najważniejsze są uprawnienia Bluetooth i Internet.

+0

ok !! spróbuję go na miejscu jutro .. ale ciekawy ... czy potrzebuję uprawnień, nawet gdy moja drukarka jest podłączona do sieci? – Samra

+0

Wierzę, że potrzebowałbyś pozwolenia internetowego na to. Nie 100%, ale dajcie mu szansę, jeśli to nie zadziała, spróbujemy czegoś innego. –

1

Status ERR_CONN to zasadniczo pokazuje, czy urządzenie nie może być osiągnięte lub connection failing z urządzeniem. Może to być USB, LAN/NETWORK, Bluetooth Stan awarii połączenia.

Jeśli próbujesz podłączyć drukarkę Bluetooth to musisz napisać poniżej uprawnień:

<uses-permission android:name="android.permission.BLUETOOTH"/> 
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/> 

Jeśli używasz sieci LAN dla drukarek sieciowych następnie

<uses-permission android:name="android.permission.INTERNET"/> 

Dla USB Printer Connection użytkowania:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> 

Podczas korzystania z Przykładowy projekt EPOS2 ct, pamiętaj, aby zaimportować plik Jar "EPOS2.jar". Click here to download the file.

2

Podejrzewam, że prawdopodobnie wystąpił błąd w metodzie podłączenia drukarki.Użyłem tego SDK raz, więc lepiej postępuj zgodnie z dokumentacją prawidłowo (czyli nie pomiń kroków). Upewnij się, że masz zdefiniowane uprawnienia wymienione w SDK.

<uses-permission android:name="android.permission.BLUETOOTH"/> 
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/> 
<uses-permission android:name="android.permission.INTERNET"/> 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> 


<activity android:name=".MainActivity" android:label="@string/app_title" android:screenOrientation="portrait"> 
<intent-filter> 
<action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"/> 
<action android:name="android.intent.action.MAIN"/> 
<category android:name="android.intent.category.LAUNCHER"/> 
</intent-filter> 
<meta-data android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" android:resource="@xml/device_filter"/> 

Jak widzę nie wspomnieli, jaki typ połączenia robicie więc, w tym przypadku, nie musi być problemem przy wyborze TARGET

Sprawdź funkcję z SDK:

Publiczne połączenie void (cel ciągu, int timeout) generuje wyjątek Epos2Exception;

enter image description here

może być używany niewłaściwy parametr docelowy.

Uwaga: jeśli korzystasz z USB lub w jakimkolwiek innym trybie, upewnij się, że uruchomiono usługę wykrywania widoczności zgodnie z dokumentami.

Discovery.start(this, mFilterOption, mDiscoveryListener); 

Zwróci ci żądaną nazwę celu. I jestem pewien, że nie dojdzie do awarii połączenia. Szczęście \ o

+1

Dzięki, stary! Kiedy odkryłem, udało mi się łatwo połączyć i drukować. :-) – Thahaseen