2012-08-22 10 views
11

pracuję na Android (v2.3) przez kilka tygodni, a ja natknęliśmy się na pewne problemy z Unimag kart Swiper od IDTECH.IDTECH Unimag kart Swiper na Androida

Urządzenie ma ograniczoną dokumentację, a aplikacja demo SDK implementuje aktualizację oprogramowania i kilka klas dla dialogów oraz takie, które naprawdę zaciemniają sposoby uzyskania podstawowej funkcjonalności (dodane do niewielu i niezbyt dobrych komentarzy w kod).

Zaimplementowałem interfejs w podstawowej czynności i próbowałem wykryć, kiedy urządzenie jest podłączone lub odłączone, ale wydaje się, że słuchacz łapie oba zdarzenia (połączenie/rozłączenie) jako "rozłączenie", nie mówiąc już o próbie odczytu karty.

Czy ktoś pracował z tym urządzeniem na Androidzie i ma kilka wyraźnych przykładów?

Nawiasem mówiąc, tu jest moja klasa:

package com.card.swipe; 

import java.io.File; 
import java.io.FileOutputStream; 
import java.io.InputStream; 
import IDTech.MSR.uniMag.uniMagReader; 
import IDTech.MSR.uniMag.uniMagReaderMsg; 
import android.app.Activity; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.widget.TextView; 
import android.widget.Toast; 

public class CardSwipeTestActivity extends Activity implements uniMagReaderMsg { 

    private uniMagReader myUniMagReader = null; 
    private TextView etCardData; 
    private String _strMSRData = null; 
    private byte[]_MSRData = null; 
    private String _strStatus = null; 
    private int _nGetChallengeResult = 0; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
//  InitializeUI(); 
     InitializeReader(); 
     String strManufacture = myUniMagReader.getInfoManufacture(); 
     String strModel = myUniMagReader.getInfoModel(); 
     String strSDKVerInfo = myUniMagReader.getSDKVersionInfo(); 
     String strOSVerInfo = android.os.Build.VERSION.RELEASE; 
     etCardData = (TextView)findViewById(R.id.text_view); 
     etCardData.setText("Phone: "+strManufacture+"\n"+"Model: "+strModel+"\n"+"SDK Ver: "+strSDKVerInfo+"\nOS Version: "+strOSVerInfo);  
    } 

    @Override 
    protected void onPause() { 
     // TODO Auto-generated method stub 
     if(myUniMagReader!=null) 
     { 
      //you should stop swipe card and unregister when the application go to background 
      myUniMagReader.stopSwipeCard();   
//   myUniMagReader.unregisterListen(); 
//   myUniMagReader.release(); 
     } 
     super.onPause(); 
    } 
    @Override 
    protected void onResume() { 
     // TODO Auto-generated method stub 
     // you should register to listen the headset event when the application resumed. 
//  if(myUniMagReader!=null) 
//  { 
////   myUniMagReader.registerListen(); 
//   if(_bCheckedSaveLogItem==true) 
//    myUniMagReader.setSaveLogEnable(true); 
//   else 
//    myUniMagReader.setSaveLogEnable(false); 
//  } 
//  if(itemStartSC!=null) 
//   itemStartSC.setEnabled(true); 
//  waitingCommandResult=false; 
     super.onResume(); 
    } 
    @Override 
    protected void onDestroy() { 
     myUniMagReader.release(); 
     super.onDestroy();  
     android.os.Process.killProcess(android.os.Process.myPid()); 
    }  

    //********************************************************************************//  

    @Override 
    public boolean getUserGrant(int arg0, String arg1) { 
     // TODO Auto-generated method stub 
     return false; 
    } 

    @Override 
    public void onReceiveMsgAutoConfigProgress(int arg0) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void onReceiveMsgCardData(byte arg0, byte[] arg1) { 
     // TODO Auto-generated method stub 
     Log.d("SWIPE", "Card swiped!"); 
     Toast.makeText(getApplicationContext(), "Card swiped!", Toast.LENGTH_SHORT).show(); 
    } 

    @Override 
    public void onReceiveMsgCommandResult(int arg0, byte[] arg1) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void onReceiveMsgConnected() { 
     Log.d("CONNECTION","Swiper Connected"); 
     Toast.makeText(getApplicationContext(), "Swiper Connected!", Toast.LENGTH_SHORT).show(); 
    } 

    @Override 
    public void onReceiveMsgDisconnected() { 
     Log.d("CONNECTION","Swiper Disconnected"); 
     Toast.makeText(getApplicationContext(), "Swiper Disconnected!", Toast.LENGTH_SHORT).show(); 
    } 

    @Override 
    public void onReceiveMsgFailureInfo(int arg0, String arg1) { 
     // TODO Auto-generated method stub 
     Log.d("CONNECTION","Swiper Failure"); 
    } 

    @Override 
    public void onReceiveMsgSDCardDFailed(String arg0) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void onReceiveMsgTimeout(String arg0) { 
     Log.d("TIMEOUT","Timed out!"); 
     Toast.makeText(getApplicationContext(), "Timed out!", Toast.LENGTH_SHORT).show(); 
    } 

    @Override 
    public void onReceiveMsgToConnect() { 
     Log.d("CONNECTION","Swiper Powered Up"); 
     Toast.makeText(getApplicationContext(), "Swiper Powered Up", Toast.LENGTH_SHORT).show(); 
    } 

    @Override 
    public void onReceiveMsgToSwipeCard() { 
     Log.d("SWIPE","Ready to swipe!"); 
     Toast.makeText(getApplicationContext(), "Ready to swipe!", Toast.LENGTH_SHORT).show();  
    } 

    //********************************************************************************// 

    private void InitializeReader() 
    { 
     if(myUniMagReader==null) 
      myUniMagReader = new uniMagReader(this,this); 

     myUniMagReader.setVerboseLoggingEnable(true); 
     myUniMagReader.registerListen(); 
     //load the XML configuratin file 
     String fileNameWithPath = getXMLFileFromRaw(); 
     if(!isFileExist(fileNameWithPath)) { fileNameWithPath = null; }   
     myUniMagReader.setXMLFileNameWithPath(fileNameWithPath); 
     myUniMagReader.loadingConfigurationXMLFile(true); 
     myUniMagReader.setTimeoutOfSwipeCard(5); 
    } 

    private boolean isFileExist(String path) { 
     if(path==null) 
      return false; 
     File file = new File(path); 
     if (!file.exists()) { 
      return false ; 
     } 
     return true; 
    } 

    private String getXMLFileFromRaw(){ 
     //the target filename in the application path 
     String fileNameWithPath = null; 
     fileNameWithPath = "idt_unimagcfg_default.xml"; 

     try{ 
      InputStream in = getResources().openRawResource(R.raw.idt_unimagcfg_default); 
      int length = in.available(); 
      byte [] buffer = new byte[length]; 
      in.read(buffer);   
      in.close(); 

      deleteFile(fileNameWithPath); 

      FileOutputStream fout = openFileOutput(fileNameWithPath, MODE_PRIVATE); 
      fout.write(buffer); 
      fout.close(); 

      // to refer to the application path 
      File fileDir = this.getFilesDir(); 
      fileNameWithPath = fileDir.getParent() + java.io.File.separator + fileDir.getName(); 
      fileNameWithPath = fileNameWithPath+java.io.File.separator+"idt_unimagcfg_default.xml"; 

      } 
      catch(Exception e){ 
      e.printStackTrace(); 
      fileNameWithPath = null; 
      } 

     return fileNameWithPath; 

     } 

    public void swipe(View v) 
    { 
     if(myUniMagReader!=null) 
     { 
      myUniMagReader.startSwipeCard(); 
     } 
     if(myUniMagReader.isSwipeCardRunning()==true) 
     { 
      Log.d("SWIPE","Swipe Card Running!"); 
     }  
    } 

    private String getHexStringFromBytes(byte []data) 
    { 
     if(data.length<=0) return null; 
     StringBuffer hexString = new StringBuffer(); 
     String fix = null; 
     for (int i = 0; i < data.length; i++) { 
      fix = Integer.toHexString(0xFF & data[i]); 
      if(fix.length()==1) 
       fix = "0"+fix; 
      hexString.append(fix); 
     } 
     fix = null; 
     fix = hexString.toString(); 
     return fix; 
    } 

    public byte[] getBytesFromHexString(String strHexData) 
    { 
     if (1==strHexData.length()%2) { 
      return null; 
     } 
     byte[] bytes = new byte[strHexData.length()/2]; 
     for (int i=0;i<strHexData.length()/2;i++) { 
      bytes[i] = (byte) Integer.parseInt(strHexData.substring(i*2, (i+1)*2) , 16); 
     } 
     return bytes; 
    } 
} 

(Istnieje kilka metod niezaimplementowanej tam również)

+0

Naprawiłeś to przy okazji? – dariopy

+0

Tak, zrobiłem, sprawdź mój komentarz poniżej na temat rozwiązania. Rozwiązałem go dawno temu, ale zapomniałem wrócić, rozwiązanie drowe'a jest i tak na miejscu. – CodingDuckling

+0

Skąd pobierasz pakiet SDK? Próbowałem go znaleźć bezskutecznie. – Ares

Odpowiedz

9

Oto, co kiedyś z UniMag. Za pomocą programu obsługi wyprowadzić przeskanowane dane i przycisk "Przesuń", aby rozpocząć przesuwanie. Aby to poprawić, należy poczekać, aż czytnik kart zgłasza, że ​​jest podłączony/gotowy do przesuwania, a następnie włączyć przycisk, wyłączyć podczas oczekiwania na przesunięcie i ponownie włączyć po otrzymaniu danych.

import java.util.regex.Matcher; 
import java.util.regex.Pattern; 

import IDTech.MSR.XMLManager.StructConfigParameters; 
import IDTech.MSR.uniMag.uniMagReader; 
import IDTech.MSR.uniMag.uniMagReaderMsg; 
import android.app.Activity; 
import android.os.Bundle; 
import android.os.Handler; 
import android.os.Message; 
import android.util.Log; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.TextView; 

public class MainActivity extends Activity implements uniMagReaderMsg { 

    private uniMagReader myUniMagReader = null; 
    private Button btnSwipe; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     if(myUniMagReader == null) { 
      myUniMagReader = new uniMagReader(this,this); 
      myUniMagReader.setSaveLogEnable(false); 
      myUniMagReader.setXMLFileNameWithPath(null); 
      myUniMagReader.loadingConfigurationXMLFile(true); 

      //myUniMagReader.setVerboseLoggingEnable(true); 
      myUniMagReader.registerListen(); 
     } 

     btnSwipe = (Button) findViewById(R.id.button1); 
     btnSwipe.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       myUniMagReader.startSwipeCard(); 
      } 
     }); 
    } 

    @Override 
    public void onDestroy() { 
    myUniMagReader.stopSwipeCard(); 
     myUniMagReader.unregisterListen(); 
     myUniMagReader.release(); 
     super.onDestroy(); 
    } 

    @Override 
    public boolean getUserGrant(int arg0, String arg1) { 
     Log.d("UniMag", "getUserGrant -- " + arg1); 
     return true; 
    } 

    @Override 
    public void onReceiveMsgAutoConfigProgress(int arg0) { 
     // TODO Auto-generated method stub 
     Log.d("UniMag", "onReceiveMsgAutoConfigProgress"); 
    } 

    @Override 
    public void onReceiveMsgCardData(byte arg0, byte[] arg1) { 
     Log.d("UniMag", "onReceiveMsgCardData"); 
     Log.d("UniMag", "Successful swipe!"); 

     String strData = new String(arg1); 
     Log.d("UniMag", "SWIPE - " + strData); 
     if(myUniMagReader.isSwipeCardRunning()) { 
      myUniMagReader.stopSwipeCard(); 
     } 

     // Match the data we want. 
     String pattern = "%B(\\d+)\\^([^\\^]+)\\^(\\d{4})"; 
     Log.d("UniMag", pattern); 
     Pattern r = Pattern.compile(pattern); 
     Matcher m = r.matcher(strData); 
     String card = ""; 
     String name = ""; 
     String exp = ""; 
     String data = ""; 
     if(m.find()) { 
      for(int a = 0; a < m.groupCount(); ++a) { 
       Log.d("UniMag", a + " - "+m.group(a)); 
      } 
      card = m.group(1); 
      name = m.group(2); 
      exp = m.group(3); 
      data = "Data: " + name + " -- " + card + " -- " + exp; 
      Log.d("UniMag", data); 

      Message msg = new Message(); 
      msg.obj = data; 
      swipeHandler.sendMessage(msg); 
     } 

    } 

    final Handler swipeHandler = new Handler() { 
     @Override 
     public void handleMessage(Message msg) { 
      String text = (String)msg.obj; 
      TextView dataView = (TextView) findViewById(R.id.text_view); 
      dataView.setText(text); 
     } 
    }; 

    @Override 
    public void onReceiveMsgCommandResult(int arg0, byte[] arg1) { 
     Log.d("UniMag", "onReceiveMsgCommandResult"); 
    } 

    @Override 
    public void onReceiveMsgConnected() { 
     Log.d("UniMag", "onReceiveMsgConnected"); 
     Log.d("UniMag", "Card reader is connected."); 
    } 

    @Override 
    public void onReceiveMsgDisconnected() { 
     Log.d("UniMag", "onReceiveMsgDisconnected"); 
     if(myUniMagReader.isSwipeCardRunning()) { 
      myUniMagReader.stopSwipeCard(); 
     } 
     myUniMagReader.release(); 

    } 

    @Override 
    public void onReceiveMsgFailureInfo(int arg0, String arg1) { 
     Log.d("UniMag","onReceiveMsgFailureInfo -- " + arg1); 
    } 

    @Override 
    public void onReceiveMsgSDCardDFailed(String arg0) { 
     Log.d("UniMag", "onReceiveMsgSDCardDFailed -- " + arg0); 
    } 

    @Override 
    public void onReceiveMsgTimeout(String arg0) { 
     Log.d("UniMag", "onReceiveMsgTimeout -- " + arg0); 
     Log.d("UniMag","Timed out!"); 
    } 

    @Override 
    public void onReceiveMsgToConnect() { 
     Log.d("UniMag","Swiper Powered Up"); 
    } 

    @Override 
    public void onReceiveMsgToSwipeCard() { 
     Log.d("UniMag","onReceiveMsgToSwipeCard");  
    } 

    @Override 
    public void onReceiveMsgAutoConfigCompleted(StructConfigParameters arg0) { 
     Log.d("UniMag", "onReceiveMsgAutoConfigCompleted"); 
    } 
} 
+0

Witam @drowe. Mam czytnik UniMag, ale nie mam najnowszego SDK ... Czy wiesz, że uruchomię projekt bez linii: myUniMagReader.setXMLFileNameWithPath (null); i myUniMagReader.loadingConfigurationXMLFile (true); stanie się niemożliwe, aby to zadziałało? Wypróbowałem to i pomimo mojegoUniMagReader.getIsToSwipeCard() "true", handler nie otrzymuje żadnych danych po przesunięciu karty ... Czytnik działa doskonale z aplikacją UniMagIIDemo. – Devester

+0

Czy powyższy kod działa? Aktualnie pracuję nad projektem, który wykorzystuje Unimag 2 jako swiper karty.Mam jego SDK i demo, ale jak wiesz, aplikacja demo jest bardzo skomplikowana. A najważniejsze jest to, że klient nie wysłał mi jeszcze suwaka unimag. On prosi o wykonanie demo bez suwaka. Dlatego potrzebuję prostego roboczego przykładu czytnika Unimag 2. Jakaś pomoc? –

+0

powyższy kod działał jak czar ... –

1

okazało się, że aplikacja 3,8 demo one dostarczane z SDK ma błąd, gdzie nie będzie łączyć się z Android tablet gt-p3113 chyba że idziesz do ustawienia w aplikacji na telefonie i wyłącz „Command Połączyć'.

+0

Tak, SDK od Unimag ma kilka błędów tu i tam, trzeba być ostrożnym i uważać. – CodingDuckling