Android NFC读取数据

本文介绍了如何在Android平台上实现NFC(近场通信)数据读取。首先,在res/xml中创建nfc_tech_filter.xml文件来定义过滤器,接着配置使用NFC的Activity,详细讲解了NFCActivity的代码实现,并提供了NfcUtils工具类的使用方法。如需获取完整资源,可访问提供的CSDN下载链接。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、添加相应的权限和gradle引用
<uses-feature
    android:name="android.hardware.nfc"
    android:required="true"/>
<uses-permission android:name="android.permission.NFC"/>
api 'com.google.guava:guava:25.1-android'

2、在res下创建xml文件夹,然后新建xml文件nfc_tech_filter

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
    <tech-list>
        <resources >
            <tech-list>
                <!--<tech>android.nfc.tech.IsoDep</tech>-->
                <tech>android.nfc.tech.NfcA</tech>
                <!--<tech>android.nfc.tech.NfcB</tech>-->
                <!--<tech>android.nfc.tech.NfcF</tech>-->
                <!--<tech>android.nfc.tech.NfcV</tech>-->
                <tech>android.nfc.tech.Ndef</tech>
                <!--<tech>android.nfc.tech.NdefFormatable</tech>-->
                <!--<tech>android.nfc.tech.MifareClassic</tech>-->
                <!--<tech>android.nfc.tech.MifareUltralight</tech>-->
            </tech-list>
        </resources>
    </tech-list>
</resources>

3、使用NFC的activity添加设置

<activity android:name=".activitys.rfid_nfc.NFCActivity"
                  android:launchMode="singleTop">
            <intent-filter>
                <action android:name="android.nfc.action.NDEF_DISCOVERED" />
            </intent-filter>

            <intent-filter>
                <action android:name="android.nfc.action.TECH_DISCOVERED" />
            </intent-filter>

            <intent-filter>
                <action android:name="android.nfc.action.TAG_DISCOVERED" />
            </intent-filter>

            <meta-data
                android:name="android.nfc.action.TECH_DISCOVERED"
                android:resource="@xml/nfc_tech_filter" />

        </activity>

4、NFCActivity代码

package com.sam.more.activitys.rfid_nfc;

import android.annotation.SuppressLint;
import android.app.PendingIntent;
import android.content.Intent;
import android.nfc.NdefMessage;
import android.nfc.NfcAdapter;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.sam.more.R;

import java.util.Calendar;
import java.util.List;

import butterknife.BindView;
import butterknife.Bu
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值