1

Is there any working sample on how to create sample app listening for credit card presence?

I tried this piece of code from Android samples but nothing happens when I scan the card (NFC is working I hear the beep sound)

 public class MainActivity extends Activity {

private NfcAdapter mAdapter;
private PendingIntent mPendingIntent;
private IntentFilter[] mFilters;
private String[][] mTechLists;
private TextView mText;
private int mCount = 0;

@Override
public void onCreate(Bundle savedState) {
    super.onCreate(savedState);

    setContentView(R.layout.activity_main);

    mAdapter = NfcAdapter.getDefaultAdapter(this);

    mPendingIntent = PendingIntent.getActivity(this, 0,
            new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);

    IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED);
    try {
        ndef.addDataType("*/*");
    } catch (MalformedMimeTypeException e) {
        throw new RuntimeException("fail", e);
    }
    mFilters = new IntentFilter[] {
            ndef,
    };

    mTechLists = new String[][] { new String[] { NfcF.class.getName() } };
}

@Override
public void onResume() {
    super.onResume();
    mAdapter.enableForegroundDispatch(this, mPendingIntent, mFilters, mTechLists);

    String action = getIntent().getAction(); //allways MAIN, even after scanning a card
    Parcelable[] msg1 = getIntent().getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES); //allways null
    Parcelable[] msg2 = getIntent().getParcelableArrayExtra(NfcAdapter.EXTRA_TAG); //allways null
}

@Override
public void onNewIntent(Intent intent) {
    Log.i("Foreground dispatch", "Discovered tag with intent: " + intent);
    mText.setText("Discovered tag " + ++mCount + " with intent: " + intent);

}
@Override
public void onPause() {
    super.onPause();
    mAdapter.disableForegroundDispatch(this);
}

}

Maybe there is something wrong with my Manifest file:

<uses-sdk
    android:minSdkVersion="16"
    android:targetSdkVersion="17" />

<uses-permission android:name="android.permission.NFC" />

<uses-feature
    android:name="android.hardware.nfc"
    android:required="true" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="pl.aprilapps.nfcreader.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.nfc.action.TAG_DISCOVERED" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.nfc.action.NDEF_DISCOVERED" />

            <category android:name="android.intent.category.DEFAULT" />

            <data android:mimeType="text/plain" />
        </intent-filter>
    </activity>
</application>

Neha Shukla
  • 3,572
  • 5
  • 38
  • 69
Jacek Kwiecień
  • 12,397
  • 20
  • 85
  • 157
  • Here a nice answer: http://stackoverflow.com/questions/9648224/reading-visa-paywave-credit-card-details-via-nfc-on-android – artemiygrn Oct 30 '13 at 13:39
  • 1
    Not really, all answers link to sdks, and I want to create my own – Jacek Kwiecień Oct 30 '13 at 13:49
  • If you are refering to EMV-compliant credit cards, you might want to make your app sensitive to IsoDep tech instead of NfcF (which is certainly not used by EMV credit cards). Also your credit card will typically not contain an NDEF message, so skip those intent filters. – Michael Roland Oct 30 '13 at 15:23
  • still I think the problem is that the action is allways MAIN... – Jacek Kwiecień Oct 30 '13 at 15:32
  • Of course it is MAIN... in `onResume()`. Your intent filters in the manifest are not really set up to start your application upon detection of a credit card (both the NDEF_DISCOVERED intent and the **fallback** TAG_DISCOVERED intent will most likely not trigger. If you want to receive the foreground dispatch intent you have to stick to `onNewIntent()`. Anyways, you want to be sensitive (regardless of manifest or foreground dispatch) to TECH_DISCOVERED with tech `IsoDep`. – Michael Roland Oct 31 '13 at 17:20
  • Hi @Xylian have u found any solution for this. Actually I am also facing the same issue.. if u hv don ethis can u plz help me – Neha Shukla Nov 10 '14 at 07:28

1 Answers1

1

Take a look at this blog post. I outline exactly the type of NFC tags you need to listen to, to react to presence of a contactless EMV card. The code uses Triangle.io's API to extract the card information once the card is detected, but you can fairly easily change that part as you see fit.

Ameen
  • 2,576
  • 1
  • 14
  • 17