I followed some tutorial using the adam rocker source code for my NFCTest. I want to be able to read and write NFC tags and also launch an application.
First of all you have to get permission in AndroidManifest.xml file for NFC. The permissions are:
The Activity which will perform NFC Read/write operation, add this intent filter in that activity in AndroidManifest.xml file:
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 Full complete project is here in My github profile