Credit Card NFC Reader
This library helps developer to read data from credit card: card number, expired date, card type.Works only with cards that has paypass or paywave technology!
Screenshots
Usage
Add the dependencies to your gradle file:
dependencies {
compile 'com.github.pro100svitlo:creditCardNfcReader:1.0.3'
}
Inside your Manifest:
<uses-permission android:name="android.permission.NFC" />
<application
...
>
<activity
...
//activity that will start when nfc scan new tag
>
<intent-filter>
<action android:name="android.nfc.action.TECH_DISCOVERED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<meta-data
android:name="android.nfc.action.TECH_DISCOVERED"
android:resource="@xml/nfc_tech_filter" />
</activity>
Add the xml file 'nfc_tech_filter':
<resources >
<tech-list>
<tech>android.nfc.tech.IsoDep</tech>
</tech-list>
</resources>
Inside your activity or fragment:
@Override
protected void onCreate(Bundle savedInstanceState) {
...
mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
if (mNfcAdapter == null){
//do something if there are no nfc module on device
} else {
//do something if there are nfc module on device
mCardNfcUtils = new CardNfcUtils(this);
//next few lines here needed in case you will scan credit card when app is closed
mIntentFromCreate = true;
onNewIntent(getIntent());
}
}
...
@Override
protected void onResume() {
super.onResume();
mIntentFromCreate = false;
if (mNfcAdapter != null && !mNfcAdapter.isEnabled()){
//show some turn on nfc dialog here. take a look in the samle ;-)
} else if (mNfcAdapter != null){
mCardNfcUtils.enableDispatch();
}
}
...
@Override
public void onPause() {
super.onPause();
if (mNfcAdapter != null) {
mCardNfcUtils.disableDispatch();
}
}
...
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
if (mNfcAdapter != null && mNfcAdapter.isEnabled()) {
//this - interface for callbacks
//intent = intent :)
//mIntentFromCreate - boolean flag, for understanding if onNewIntent() was called from onCreate or not
mCardNfcAsyncTask = new CardNfcAsyncTask.Builder(this, intent, mIntentFromCreate)
.build();
}
}
That's pretty much all what you need to start the work!