Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read and write Android NFC tags?

Tags:

android

nfc

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.

like image 905
ogfasasi Avatar asked Apr 05 '11 03:04

ogfasasi


People also ask

How read and write data from NFC tag in Android?

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.

How do you write on a NFC tag?

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!


2 Answers

The NDEF Tools for Android utility project helps doing the following

  1. Detect, then
  2. Read or write, or
  3. Beam (push) NFC content

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.

like image 79
ThomasRS Avatar answered Oct 12 '22 00:10

ThomasRS


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

like image 23
Md. Sajedul Karim Avatar answered Oct 12 '22 02:10

Md. Sajedul Karim