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.
To read the NFC tag, the app needs to register for handling ACTION_NDEF_DISCOVERED intent. Registering this intent will let your app handle any NFC tag that is tapped to the Android device. If you wish to handle only those tags that belong to your application then you can add a filter.
To simply write to one blank NFC tag, make sure all of these options are unchecked. Finally, select “Write” at the bottom of the screen and tap the back of your phone to a blank NFC tag as if you were going to read it. This writes the URL to the tag. Congratulations, you've just encoded your first NFC tag!
The NDEF Tools for Android utility project helps doing the following
The project also includes data bindings for all standardized NDEF record types, which really simplifies things compared to working with the (byte-array-based) NDEF classes included in the Android SDK.
Also see the NFC Eclipse plugin for a graphical NDEF editor - comes with an utility app which reads and writes to tags and beams, also has NFC reader integration.
By the way, you are looking for the Android Application Record for launching the app. The launching 'feature' does not require any real implementation; it is built into Android >= 4.0, so putting that record on a tag is enough.
First of all you have to get permission in AndroidManifest.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 AndroidManifest.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 Full complete project is here in My github profile
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With