First of all You have to get permission in AndroidMenifest.xml File for nfc. The permissions are:
<uses-permission android:name="android.permission.NFC" />
<uses-feature android:name="android.hardware.nfc" />
The Activity which will perform Nfc Read/write operation , add this intent filter in that activity in menifest.xml file:
<intent-filter>
<action android:name="android.nfc.action.TAG_DISCOVERED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
In your activity onCreate() method you have to initialize the NFC adapter and define Pending Intent :
NfcAdapter mAdapter;
PendingIntent mPendingIntent;
mAdapter = NfcAdapter.getDefaultAdapter(this);
if (mAdapter == null) {
//nfc not support your device.
return;
}
mPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this,
getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
In onResume() Call back enable the Foreground Dispatch to detect NFC intent.
mAdapter.enableForegroundDispatch(this, mPendingIntent, null, null);
In onPause() callback you must have to disable the forground dispatch:
if (mAdapter != null) {
mAdapter.disableForegroundDispatch(this);
}
In onNewIntent() call back method you will get the new Nfc Intent. After getting The Intent , you have to parse the intent to detect the card:
@Override
protected void onNewIntent(Intent intent){
getTagInfo(intent)
}
private void getTagInfo(Intent intent) {
Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
}
Now You have the Tag. Then you can check the Tag Tech list to detect that Tag. The tag detection technique is here in [My Another Answer][1]
Full Project is in [My GitHub Repo][2]
[1]: https://stackoverflow.com/a/28565383/3073945
[2]: https://github.com/mesuk08/NFC-UTILS
First of all You have to get the permission in AndroidMenifest.xml File. The permissions are:
<uses-permission android:name="android.permission.NFC" />
<uses-feature android:name="android.hardware.nfc" />
The Activity which will perform Nfc Read/write operation , add this intent filter in that activity in menifest.xml file:
<intent-filter>
<action android:name="android.nfc.action.TAG_DISCOVERED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
In your activity onCreate() method you have to initialize the NFC adapter and define Pending Intent :
NfcAdapter mAdapter;
PendingIntent mPendingIntent;
mAdapter = NfcAdapter.getDefaultAdapter(this);
if (mAdapter == null) {
//nfc not support your device.
return;
}
mPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this,
getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
In onResume() Call back enable the Foreground Dispatch to detect NFC intent.
mAdapter.enableForegroundDispatch(this, mPendingIntent, null, null);
In onPause() callback you must have to disable the forground dispatch:
if (mAdapter != null) {
mAdapter.disableForegroundDispatch(this);
}
In onNewIntent() call back method you will get the new Nfc Intent. After getting The Intent , you have to parse the intent to detect the card:
@Override
protected void onNewIntent(Intent intent){
getTagInfo(intent)
}
private void getTagInfo(Intent intent) {
Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
}
Now You have the Tag. Then you can check the Tag Tech list to detect that Tag.
The tag detection technique is here [in My Another Answer][1]
[1]: https://stackoverflow.com/a/28565383/3073945