Monday 13 April 2015

IDTech Unimag Card Swiper on Android Demo

We will be Getting Name,Card number and expiry date from the card using IDTech Unimag Card Swiper.Here we will be initializing the card swiper inside oncreate, So when activity starts card reader is ready to accept the card.Attached your IDTech card reader to device and run the below code.Swipe the card when activity is visible.

Download the IDT_UniMagSDKAndroid_v4.4.jar from below link and place in your libs folder :

http://spsrprofessionals.com/ClientSite/readers/DMPointOfSale/dMPointOfSale/libs/


1. CardReaderActivity.java

public class CardReaderActivity extends Activity implements uniMagReaderMsg {

private uniMagReader myUniMagReader = null;
private TextView tvCardData;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
// initialize class object
if (myUniMagReader == null)
myUniMagReader = new uniMagReader(this, this);
// initilize view id.
initView();
// init asyntask to read card value.
// new InitCardReader().execute();
new Thread() {
public void run() {
myUniMagReader.setSaveLogEnable(false);
myUniMagReader.setXMLFileNameWithPath(null);
myUniMagReader.loadingConfigurationXMLFile(true);
// myUniMagReader.setVerboseLoggingEnable(true);
myUniMagReader.registerListen();
myUniMagReader.startSwipeCard();
}
}.start();
}

/**
* This method getting all widget id from xml and initialize view by theirT
* ids.
*
* @return void
* **/

private void initView() {
tvCardData = (TextView) findViewById(R.id.tv_swip);
//btnSwipe = (Button) findViewById(R.id.btn_swipe);
//myUniMagReader.startSwipeCard();
}

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

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

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

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

String strData = new String(arg1);
Log.d("asd", "SWIPE - " + strData);
if (myUniMagReader.isSwipeCardRunning()) {
myUniMagReader.stopSwipeCard();
}
// Match the data we want.
String pattern = "%B(\\d+)\\^([^\\^]+)\\^(\\d{4})";
Log.d("asd", 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("asd", a + " - " + m.group(a));
}
card = m.group(1);
name = m.group(2);
exp = m.group(3);
data = "Name:" + name + "\nCard:" + card + "\nExpire:" + exp;
Log.d("asd", data);
tvCardData.setText("Card Data: \n" + data);
displayMsgAndReset(null);
} else {
displayMsgAndReset(null);
}

}

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

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

@Override
public void onReceiveMsgDisconnected() {
Log.d("asd", "onReceiveMsgDisconnected");
if (myUniMagReader.isSwipeCardRunning()) {
myUniMagReader.stopSwipeCard();
}
//myUniMagReader.release();
displayMsgAndReset("Device disconnected...");

}

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

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

@Override
public void onReceiveMsgTimeout(String arg0) {

displayMsgAndReset(null);
}

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

@Override
public void onReceiveMsgToSwipeCard() {
Log.d("asd", "onReceiveMsgToSwipeCard");
// displayAlert("Swip card");

}

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

@Override
public void onReceiveMsgAutoConfigProgress(int arg0, double arg1,
String arg2) {
// TODO Auto-generated method stub

}

@Override
public void onReceiveMsgProcessingCardData() {
// TODO Auto-generated method stub

}

@Override
public void onReceiveMsgToCalibrateReader() {
// TODO Auto-generated method stub

}

/**
* This method will show alert
* ***/
private void displayMsgAndReset(String msg) {
myUniMagReader.startSwipeCard();
if(msg!=null)
Toast.makeText(this, msg, Toast.LENGTH_LONG).show();

}

}

2. activity_splash.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    android:gravity="center"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/tv_swip"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:text="@string/app_name"
        android:textColor="@color/black"
        android:textSize="20sp"
        android:textStyle="bold" />

</LinearLayout>

3. manifest.xml permission

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

No comments:

Post a Comment