Read NFC Tag ID on Android to Use as Unique User Identifier
The article explains how to read an NFC card’s UID on Android, use it as a unique user identifier for login verification, and walks through required permissions, setting the activity launch mode to singleTop, designing the UI layout, handling NFC intents, converting the UID to a hex string, and debugging the implementation.
This guide shows how to read the UID of an NFC card in an Android app and use it as a unique identifier for user login.
1. Add NFC permissions
<!-- NFC permission -->
<uses-permission android:name="android.permission.NFC" />
<!-- Require NFC hardware -->
<uses-feature android:name="android.hardware.nfc" android:required="true" />2. Configure the activity launch mode
Set the main activity to singleTop so that a new intent is delivered to onNewIntent() instead of creating a new instance.
<activity android:name=".MainActivity" android:launchMode="singleTop">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>3. Design the layout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#151414">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_centerInParent="true">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="读取到的卡UID: "
android:textColor="#fff" />
<TextView
android:id="@+id/tv_uid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#fff" />
</LinearLayout>
</RelativeLayout>4. Implement NFC handling in MainActivity
In onCreate() the app obtains an NfcAdapter, checks whether NFC is supported and enabled, creates a PendingIntent, and calls onNewIntent() because the launch mode is singleTop.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvUid = findViewById(R.id.tv_uid);
nfcAdapter = NfcAdapter.getDefaultAdapter(this);
pendingIntent = PendingIntent.getActivity(this, 0,
new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
if (nfcAdapter == null) {
Toast.makeText(this, "设备不支持NFC", Toast.LENGTH_LONG).show();
return;
}
if (!nfcAdapter.isEnabled()) {
Toast.makeText(this, "请在系统设置中先启用NFC功能", Toast.LENGTH_LONG).show();
return;
}
onNewIntent(getIntent());
}The overridden onNewIntent() forwards the received Intent to resolveIntent(), which extracts the Tag object.
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
resolveIntent(intent);
}
void resolveIntent(Intent intent) {
Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
if (tag != null) {
processTag(intent);
}
} processTag()obtains the UID byte array, converts it to a hexadecimal string, and displays it.
public void processTag(Intent intent) {
Tag tagFromIntent = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
byte[] uidBytes = tagFromIntent.getId();
String uidHex = ByteArrayToHexString(uidBytes);
tvUid.setText(uidHex);
}
private String ByteArrayToHexString(byte[] inarray) {
String[] hex = {"0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"};
StringBuilder out = new StringBuilder();
for (byte b : inarray) {
int i = (b & 0xFF) >> 4;
out.append(hex[i]);
i = b & 0x0F;
out.append(hex[i]);
}
return out.toString();
}5. Manage foreground dispatch
Enable dispatch in onResume() and disable it in onPause() so the activity receives NFC intents while it is visible.
@Override
protected void onResume() {
super.onResume();
if (nfcAdapter != null) {
nfcAdapter.enableForegroundDispatch(this, pendingIntent, null, null);
}
}
@Override
protected void onPause() {
super.onPause();
if (nfcAdapter != null) {
nfcAdapter.disableForegroundDispatch(this);
}
}6. Run and test
Build and run the app, enable NFC on the device, and bring an NFC card close to the phone. The UID appears in the TextView. Debugging can be done by setting breakpoints in the methods above.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
The Dominant Programmer
Resources and tutorials for programmers' advanced learning journey. Advanced tracks in Java, Python, and C#. Blog: https://blog.csdn.net/badao_liumang_qizhi
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
