Skip to content

Instantly share code, notes, and snippets.

@luixal
Created June 12, 2013 20:39
Show Gist options
  • Star 49 You must be signed in to star a gist
  • Fork 13 You must be signed in to fork a gist
  • Save luixal/5768921 to your computer and use it in GitHub Desktop.
Save luixal/5768921 to your computer and use it in GitHub Desktop.
Read NFC Tag UID
// Needed in AndroidManifest:
<!-- Permission for using NFC hardware -->
<uses-permission android:name="android.permission.NFC"/>
<!-- Forcing device to have NFC hardware -->
<uses-feature android:name="android.hardware.nfc" android:required="true"/>
<!-- Registering app for receiving NFC's TAG_DISCOVERED intent -->
<intent-filter>
<action android:name="android.nfc.action.TAG_DISCOVERED"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
// handling ACTION_TAG_DISCOVERED action from intent:
@Override
protected void onResume() {
super.onResume();
if (getIntent().getAction().equals(NfcAdapter.ACTION_TAG_DISCOVERED)) {
((TextView)findViewById(R.id.text)).setText(
"NFC Tag\n" +
this.ByteArrayToHexString(getIntent().getByteArrayExtra(NfcAdapter.EXTRA_ID)));
}
}
// Converting byte[] to hex string:
private String ByteArrayToHexString(byte [] inarray) {
int i, j, in;
String [] hex = {"0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"};
String out= "";
for(j = 0 ; j < inarray.length ; ++j)
{
in = (int) inarray[j] & 0xff;
i = (in >> 4) & 0x0f;
out += hex[i];
i = in & 0x0f;
out += hex[i];
}
return out;
}
package es.luixal.ragbag;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.nfc.NfcAdapter;
import android.nfc.tech.IsoDep;
import android.nfc.tech.MifareClassic;
import android.nfc.tech.MifareUltralight;
import android.nfc.tech.Ndef;
import android.nfc.tech.NfcA;
import android.nfc.tech.NfcB;
import android.nfc.tech.NfcF;
import android.nfc.tech.NfcV;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.widget.TextView;
public class MainActivity extends Activity {
// list of NFC technologies detected:
private final String[][] techList = new String[][] {
new String[] {
NfcA.class.getName(),
NfcB.class.getName(),
NfcF.class.getName(),
NfcV.class.getName(),
IsoDep.class.getName(),
MifareClassic.class.getName(),
MifareUltralight.class.getName(), Ndef.class.getName()
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main_activity, menu);
return true;
}
@Override
protected void onResume() {
super.onResume();
// creating pending intent:
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
// creating intent receiver for NFC events:
IntentFilter filter = new IntentFilter();
filter.addAction(NfcAdapter.ACTION_TAG_DISCOVERED);
filter.addAction(NfcAdapter.ACTION_NDEF_DISCOVERED);
filter.addAction(NfcAdapter.ACTION_TECH_DISCOVERED);
// enabling foreground dispatch for getting intent from NFC event:
NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
nfcAdapter.enableForegroundDispatch(this, pendingIntent, new IntentFilter[]{filter}, this.techList);
}
@Override
protected void onPause() {
super.onPause();
// disabling foreground dispatch:
NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
nfcAdapter.disableForegroundDispatch(this);
}
@Override
protected void onNewIntent(Intent intent) {
if (intent.getAction().equals(NfcAdapter.ACTION_TAG_DISCOVERED)) {
((TextView)findViewById(R.id.text)).setText(
"NFC Tag\n" +
ByteArrayToHexString(intent.getByteArrayExtra(NfcAdapter.EXTRA_ID)));
}
}
private String ByteArrayToHexString(byte [] inarray) {
int i, j, in;
String [] hex = {"0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"};
String out= "";
for(j = 0 ; j < inarray.length ; ++j)
{
in = (int) inarray[j] & 0xff;
i = (in >> 4) & 0x0f;
out += hex[i];
i = in & 0x0f;
out += hex[i];
}
return out;
}
}
@anajetli
Copy link

anajetli commented Dec 5, 2014

I am new and coding simple app in android studio only for reading the TAG ID. I just want to read ID only and not data, Please, please guide. I am searching and studing but my code reads ID of blank cards and nothing on written cards.

@Elliott-Green
Copy link

This code helped, thanks!

@WatherMG
Copy link

WatherMG commented May 28, 2016

Thank you, this code help me!

@pc133
Copy link

pc133 commented Oct 15, 2016

Thank you for code !

@lahari67
Copy link

lahari67 commented Mar 1, 2017

the code shows a fatal error.what have to do

@monis0395
Copy link

Thanks a lot!

@IsaacGutierrez
Copy link

Thanks, this is very helpful.

@luixal
Copy link
Author

luixal commented May 16, 2017

You're welcome!

It's great to see that my snippet is useful for more devs :)

@ruan65
Copy link

ruan65 commented Jun 8, 2017

Yes, it is!

@appy15
Copy link

appy15 commented Feb 5, 2018

Thank you very much. Save my day...

@ArturStarz
Copy link

ArturStarz commented Jul 12, 2018

Hi.. Can you help me and make a * .aix component from these files (for app inventor).. ?

@ikhdarisnan
Copy link

Thankyou for the code!, Its very helpful for me.

@NgomGit
Copy link

NgomGit commented Apr 9, 2019

Thank's you for the code!. It's help me a lot

@MojtabaShams
Copy link

how to read all sector data?

@luixal
Copy link
Author

luixal commented Oct 16, 2019

@MojtabaShams not sure, this snippet dates from 2013/14, It's been ages since I coded something for Android. Sorry not being able to help, but I'm sure there are better libs to do this nowadays :)

In fact, it's surprising this is still used in 2019! :D

@dalafiarisamuel
Copy link

Thanks for this snippet 👍🏽

@GFromm91
Copy link

GFromm91 commented Jan 5, 2020

Thanks for this code. No fuzz, working like a charm.

@nikkon404
Copy link

Thanks a lot worked for me too!

@TimmyRedoc
Copy link

Thank you, this saved me a lot of time.

@indarsyah
Copy link

Thanks a lot worked for me too!

@hao759
Copy link

hao759 commented Dec 16, 2022

Thanks a lot worked for me too!

@morihofi
Copy link

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment