11

I've been working on Android (v2.3) for a couple of weeks now, and I've stumbled upon some problems with the Unimag Card Swiper from IDTECH.

The unit comes with a scarce documentation and the demo app from the SDK implements the firmware update and a few classes for dialogs and such which really offuscate how to achieve basic functionality (added to the few and not so good comments in the code).

I have implemented the interface in a basic activity and tried to detect when the unit is connected or disconnected but it seems the listener catches both events (connection/disconnection) as "disconnect", let alone trying to read a card.

Has anyone worked with this unit on Android and has some clear examples?

By the way, here is my class:

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

(There are some unimplemented methods there as well)

CodingDuckling
  • 595
  • 9
  • 20
  • Yes, I did, check my comment below on the solution. I solved it long ago but forgot to come back, drowe's solution is spot on anyway. – CodingDuckling Jan 20 '14 at 13:44
  • Where did you download the SDK? I've been trying to find it with no avail. – Ares Oct 07 '14 at 16:34
  • Hey Ares, I got it directly from IDTech back then when my client bought the readers. However, I found the SDK on my email, I uploaded it with release notes and manual here: http://tempsend.com/EA3D8889C9 - mind you it is an old version (v2.8) from 2012. Hope it helps! – CodingDuckling Oct 08 '14 at 08:37
  • Hey guys, I know this is an old post but I'm starting to work with Unimag now and to be honest I'm completely lost on how to start. I've received the SDK and a manual but it doensn't seem to be a good way to start, I mean, their demo code has more than 4k code lines and I'm trying to start from the basic. Where did you find information about the Unimag development? Is there anything else besides the sample code? Anyone found other example codes?? Thanks a lot!! – Blanco Sep 15 '16 at 23:52
  • Hey, it's been so long since I worked on this that I barely remember but if I recall correctly, documentation was very scarce. I could not find anything online and all documentation had to be directly requested to them, but it was nothing more than the manual and the SDK. There are a few other Unimag questions here in SO, you might be able to find some more stuff. – CodingDuckling Sep 16 '16 at 12:23

2 Answers2

9

Here's what I used with the UniMag. Using a handler to output the swiped data, and a 'Swipe' button to start the swiping. To improve, one should wait until the card reader reports it is connected / ready to swipe, then enable the button, disable while waiting for swipe, and re-enable after receiving data.

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");
    }
}
drowe
  • 2,312
  • 18
  • 14
  • Hi @drowe. I have got an UniMag reader, but I do not have the latest SDK... Would you know if I run the project without the lines : myUniMagReader.setXMLFileNameWithPath(null); and myUniMagReader.loadingConfigurationXMLFile(true); will become impossible to make it works? I tried it and despite of myUniMagReader.getIsToSwipeCard() be "true", the handler does not receive any data when I swipe the card... The reader is working perfectly with the UniMagIIDemo app. – Devester Jan 31 '13 at 16:46
  • Does the above code work? Actually I am working on a project which uses Unimag 2 as card swiper. I have its SDK and demo but as you know the demo app is very complex. And the main thing is the client has not sent me the unimag swiper yet. He is asking to make a demo without swiper. That's why I need a simple working example of Unimag 2 reader. Any help? – Khawar Raza Jun 26 '13 at 10:53
  • the example is working and what about the pin code and the encryption and decryption things? Do you know how to implement these things...??? – Khawar Raza Jun 27 '13 at 15:55
  • I don't know that the PIN is accessible - it might be? I didn't work to get that information, I simply wanted the name attached to the card. – drowe Jul 02 '13 at 21:30
  • Wow, sorry for the late, late reply! I fixed this long ago and forgot to come back to post the answer, thought nobody were using Unimag. I marked drowe's solution as correct as it is pretty spot on and by reading it it is almost the same I did back then. @KhawarRaza what is your issue with encryption/decryption? I also had to "emulate" the device's encryption/decryption process by hand, for when the user introduced his credit card info manually. I can look that code up and share it. – CodingDuckling Jan 20 '14 at 13:41
  • CodingDuckling, do you or @drowe have a copy of the finished project that can be looked at? Trying to work something out and I'm new to Android dev. – M Charles Feb 26 '14 at 17:33
  • HI , i am getting following Error `W/UMSDK: SDK: reader attached, but no config loaded` – Dhiru Oct 11 '17 at 07:31
  • I tried in 2022, but I get this: UMSDK: SDK: Task not started: Reader not attached, in Android 12 – lacas May 14 '22 at 12:10
1

I found that the 3.8 demo app they ship with the SDK has a bug where it will not connect to an Android gt-p3113 tablet unless you go into settings on the app on the phone and turn off 'Command To Connect'.

Daniel L.
  • 5,060
  • 10
  • 36
  • 59
Fred Covely
  • 71
  • 1
  • 5