8:41 AM
0




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

0 comments:

Post a Comment