Android NFC读取数据

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

摘要生成于 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
以下是一个基本的Android NFC读取数据的代码示例: ``` private NfcAdapter nfcAdapter; private PendingIntent pendingIntent; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); nfcAdapter = NfcAdapter.getDefaultAdapter(this); pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()) .addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0); } @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); } } @Override protected void onNewIntent(Intent intent) { super.onNewIntent(intent); if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())) { Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG); if (tag != null) { readFromTag(tag); } } } private void readFromTag(Tag tag) { Ndef ndef = Ndef.get(tag); if (ndef != null) { try { ndef.connect(); NdefMessage message = ndef.getNdefMessage(); if (message != null) { NdefRecord[] records = message.getRecords(); if (records != null && records.length > 0) { String payload = new String(records[0].getPayload(), Charset.forName("UTF-8")); // Do something with the payload } } } catch (IOException | FormatException e) { e.printStackTrace(); } finally { try { ndef.close(); } catch (IOException e) { e.printStackTrace(); } } } } ``` 这段代码首先获取默认的NFC适配器和一个用于处理NFC意图的PendingIntent,然后在 onResume() 方法中启用前台调度以确保应用程序在前台时可以接收NFC意图。在 onPause() 方法中,我们禁用前台调度以节省电池寿命。在 onNewIntent() 方法中,我们检查接收到的意图是否是一个标签发现意图,如果是,则读取标签并将其传递给 readFromTag() 方法。在 readFromTag() 方法中,我们首先获取该标签是否支持NDEF格式,并连接到该标签。如果连接成功,我们尝试从标签中读取NDEF消息,并从第一个记录中获取有效载荷。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值