You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求助:在Android Studio中用Java实现读取MIFARE标签UID的方案

求助:在Android Studio中用Java实现读取MIFARE标签UID的方案

别着急,我来一步步教你怎么在Android里用Java读取MIFARE标签的UID,这个完全可以用Android自带的NFC API搞定,不用额外依赖第三方库,上手很简单。

第一步:配置AndroidManifest.xml

首先得给你的App开NFC相关的权限,还要声明App支持NFC功能,不然系统不让你访问NFC硬件。把下面的代码加到你的AndroidManifest.xml里:

<!-- 声明NFC权限 -->
<uses-permission android:name="android.permission.NFC" />

<!-- 声明App需要NFC硬件支持,如果你的App必须依赖NFC才能运行,就把required设为true;如果是可选,就设为false -->
<uses-feature
    android:name="android.hardware.nfc"
    android:required="true" />

<!-- 在你要处理NFC的Activity里添加Intent Filter,让系统知道这个Activity能处理NFC标签事件 -->
<activity android:name=".MainActivity">
    <intent-filter>
        <action android:name="android.nfc.action.TECH_DISCOVERED" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>

    <!-- 配置TECH_DISCOVERED的meta-data,指定我们要处理的NFC技术类型,这里包含MIFARE常用的类型 -->
    <meta-data
        android:name="android.nfc.action.TECH_DISCOVERED"
        android:resource="@xml/nfc_tech_filter" />
</activity>

然后你需要在res/xml目录下新建一个nfc_tech_filter.xml文件(如果没有xml目录就手动新建一个),内容如下,用来指定我们支持的NFC标签技术:

<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
    <tech-list>
        <tech>android.nfc.tech.MifareClassic</tech>
    </tech-list>
    <tech-list>
        <tech>android.nfc.tech.MifareUltralight</tech>
    </tech-list>
</resources>

第二步:编写Activity的核心Java代码

接下来就是在你的Activity里实现NFC监听和UID读取的逻辑了,我给你写了完整的示例代码,每一行都加了注释,你直接参考着改就行:

import android.nfc.NfcAdapter;
import android.nfc.Tag;
import android.nfc.tech.MifareClassic;
import android.nfc.tech.MifareUltralight;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    private NfcAdapter nfcAdapter;
    private TextView uidTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        uidTextView = findViewById(R.id.uid_text_view);

        // 初始化NFC适配器
        nfcAdapter = NfcAdapter.getDefaultAdapter(this);

        // 检查设备是否支持NFC
        if (nfcAdapter == null) {
            Toast.makeText(this, "当前设备不支持NFC功能", Toast.LENGTH_LONG).show();
            finish();
            return;
        }

        // 检查NFC是否开启
        if (!nfcAdapter.isEnabled()) {
            Toast.makeText(this, "请先开启设备的NFC功能", Toast.LENGTH_LONG).show();
            finish();
            return;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        // 启动前台调度,确保App在前台时优先捕获NFC事件,不会被系统其他App抢走
        if (nfcAdapter != null) {
            nfcAdapter.enableForegroundDispatch(
                    this,
                    NfcAdapter.createPendingIntent(this, getIntent(), 0, 0),
                    null,
                    null
            );
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        // 停止前台调度,节省系统资源
        if (nfcAdapter != null) {
            nfcAdapter.disableForegroundDispatch(this);
        }
    }

    @Override
    protected void onNewIntent(android.content.Intent intent) {
        super.onNewIntent(intent);
        // 当检测到NFC标签靠近时,系统会自动调用这个方法
        String action = intent.getAction();
        if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(action)) {
            // 获取标签的核心对象
            Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
            if (tag != null) {
                // 从Tag对象中提取UID(默认是字节数组格式)
                byte[] uidBytes = tag.getId();
                // 把字节数组转成十六进制字符串,方便阅读和展示
                String uidHex = bytesToHex(uidBytes);
                // 在界面上显示读取到的UID
                uidTextView.setText("标签UID:" + uidHex);
                Toast.makeText(this, "成功读取标签UID", Toast.LENGTH_SHORT).show();

                // 额外:如果需要判断标签类型,可以加这段逻辑
                if (MifareClassic.get(tag) != null) {
                    Toast.makeText(this, "这是MIFARE Classic标签", Toast.LENGTH_SHORT).show();
                } else if (MifareUltralight.get(tag) != null) {
                    Toast.makeText(this, "这是MIFARE Ultralight标签", Toast.LENGTH_SHORT).show();
                }
            }
        }
    }

    // 工具方法:将字节数组转换为十六进制字符串
    private String bytesToHex(byte[] bytes) {
        StringBuilder sb = new StringBuilder();
        for (byte b : bytes) {
            sb.append(String.format("%02X ", b));
        }
        // 去掉最后一个多余的空格
        return sb.length() > 0 ? sb.substring(0, sb.length() - 1) : "";
    }
}

一些重要的测试提醒

  • 一定要用真实的Android设备测试,Android模拟器是不支持NFC功能的
  • 测试前记得打开设备的NFC开关,不然根本读取不到标签
  • 这个方法对MIFARE Classic、MIFARE Ultralight等绝大多数MIFARE标签都有效,因为它们的UID信息都统一存在Tag对象里
  • 如果你只是读取UID,完全不需要额外的位置权限(Android 10+的特殊场景除外,但读取UID的场景下完全用不到)

内容来源于stack exchange

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.08 10:24:37