Android实现读取NFC卡卡号示例
发布时间 - 2026-01-10 22:18:53 点击率:次Android实现读取NFC卡卡号示例,具体如下:

1.权限
<uses-permission android:name="android.permission.NFC" />
<uses-feature
android:name="android.hardware.nfc"
android:required="true" />
2.注册(静态)
<intent-filter>
<action android:name="android.nfc.action.TAG_DISCOVERED" />
<data android:mimeType="text/plain" />
</intent-filter>
3.Activity
初始化
//初始化NfcAdapter
mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
// 初始化PendingIntent,当有NFC设备连接上的时候,就交给当前Activity处理
pi = PendingIntent.getActivity(this, 0, new Intent(this, getClass())
.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
启动
@Override
protected void onResume() {
super.onResume();
mNfcAdapter.enableForegroundDispatch(this, pi, null, null); //启动
}
获取数据
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
// 当前app正在前端界面运行,这个时候有intent发送过来,那么系统就会调用onNewIntent回调方法,将intent传送过来
// 我们只需要在这里检验这个intent是否是NFC相关的intent,如果是,就调用处理方法
if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())) {
processIntent(intent);
}
}
解析
/**
* Parses the NDEF Message from the intent and prints to the TextView
*/
private void processIntent(Intent intent) {
//取出封装在intent中的TAG
Tag tagFromIntent = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
String CardId =ByteArrayToHexString(tagFromIntent.getId());
}
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;
}
4.完整参考
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cn.com.jslh.zjcdprogrect">
<uses-permission android:name="android.permission.NFC" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-feature
android:name="android.hardware.nfc"
android:required="true" />
<application
android:name=".common.MyApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".LoginActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".saoka.WorkActivity">
<intent-filter>
<action android:name="android.nfc.action.TAG_DISCOVERED" />
<data android:mimeType="text/plain" />
</intent-filter>
<!--<meta-data android:name="android.nfc.action.TECH_DISCOVERED" android:resource="@xml/nfc_tech_filter" />-->
</activity>
</application>
</manifest>
package cn.com.jslh.zjcdprogrect.saoka;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.nfc.NfcAdapter;
import android.nfc.Tag;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import cn.com.jslh.zjcdprogrect.R;
public class WorkActivity extends AppCompatActivity {
private NfcAdapter mNfcAdapter;
private PendingIntent pi;
private IntentFilter tagDetected;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_work);
//初始化NfcAdapter
mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
//初始化PendingIntent
// 初始化PendingIntent,当有NFC设备连接上的时候,就交给当前Activity处理
pi = PendingIntent.getActivity(this, 0, new Intent(this, getClass())
.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
// 新建IntentFilter,使用的是第二种的过滤机制
// tagDetected = new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED);
// tagDetected.addCategory(Intent.CATEGORY_DEFAULT);
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
// 当前app正在前端界面运行,这个时候有intent发送过来,那么系统就会调用onNewIntent回调方法,将intent传送过来
// 我们只需要在这里检验这个intent是否是NFC相关的intent,如果是,就调用处理方法
if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())) {
processIntent(intent);
}
}
@Override
protected void onResume() {
super.onResume();
mNfcAdapter.enableForegroundDispatch(this, pi, null, null);
}
/**
* Parses the NDEF Message from the intent and prints to the TextView
*/
private void processIntent(Intent intent) {
//取出封装在intent中的TAG
Tag tagFromIntent = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
String CardId =ByteArrayToHexString(tagFromIntent.getId());
}
public static void startActivity(Context context){
Intent intent = new Intent();
intent.setClass(context,WorkActivity.class);
context.startActivity(intent);
}
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;
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
# android
# nfc读卡号
# nfc
# 读取
# 读取卡号
# Android开发实现NFC刷卡读取的两种方式
# Android实现读取NFC卡的编号
# Android实现NFC读取校园卡
# Android 使用手机NFC的读取NFC标签数据的方法
# android nfc常用标签读取总结
# android实现通过NFC读取卡号
# 就会
# 在这里
# 这个时候
# 只需要
# 回调
# 装在
# 的是
# 卡号
# 第二种
# 大家多多
# ACTION_TAG_DISCOVERED
# app
# equals
# onNewIntent
# Override
# protected
# addFlags
# FLAG_ACTIVITY_SINGLE_TOP
# void
# enableForegroundDispatch
相关栏目:
【
网站优化151355 】
【
网络推广146373 】
【
网络技术251813 】
【
AI营销90571 】
相关推荐:
打开php文件提示内存不足_怎么调整php内存限制【解决方案】
bing浏览器学术搜索入口_bing学术文献检索地址
Laravel如何使用Collections进行数据处理?(实用方法示例)
Laravel如何实现事件和监听器?(Event & Listener实战)
韩国代理服务器如何选?解析IP设置技巧与跨境访问优化指南
谷歌Google入口永久地址_Google搜索引擎官网首页永久入口
Python文件异常处理策略_健壮性说明【指导】
利用python获取某年中每个月的第一天和最后一天
微信h5制作网站有哪些,免费微信H5页面制作工具?
如何为不同团队 ID 动态生成多个非值班状态按钮
作用域操作符会触发自动加载吗_php类自动加载机制与::调用【教程】
Laravel如何处理JSON字段_Eloquent原生JSON字段类型操作教程
七夕网站制作视频,七夕大促活动怎么报名?
javascript中数组(Array)对象和字符串(String)对象的常用方法总结
Android 常见的图片加载框架详细介绍
🚀拖拽式CMS建站能否实现高效与个性化并存?
html5audio标签播放结束怎么触发事件_onended回调方法【教程】
Linux虚拟化技术教程_KVMQEMU虚拟机安装与调优
Linux系统命令中tree命令详解
百度输入法ai面板怎么关 百度输入法ai面板隐藏技巧
如何用美橙互联一键搭建多站合一网站?
如何在建站主机中优化服务器配置?
Laravel怎么实现支付功能_Laravel集成支付宝微信支付
小视频制作网站有哪些,有什么看国内小视频的网站,求推荐?
laravel怎么实现图片的压缩和裁剪_laravel图片压缩与裁剪方法
Laravel怎么导出Excel文件_Laravel Excel插件使用教程
想要更高端的建设网站,这些原则一定要坚持!
网页设计与网站制作内容,怎样注册网站?
阿里云高弹*务器配置方案|支持分布式架构与多节点部署
Laravel如何生成URL和重定向?(路由助手函数)
如何在 Pandas 中基于一列条件计算另一列的分组均值
重庆市网站制作公司,重庆招聘网站哪个好?
googleplay官方入口在哪里_Google Play官方商店快速入口指南
Laravel如何升级到最新版本?(升级指南和步骤)
高防服务器租用指南:配置选择与快速部署攻略
Laravel如何设置定时任务(Cron Job)_Laravel调度器与任务计划配置
如何用5美元大硬盘VPS安全高效搭建个人网站?
如何在宝塔面板创建新站点?
Laravel Vite是做什么的_Laravel前端资源打包工具Vite配置与使用
微信小程序 wx.uploadFile无法上传解决办法
Laravel怎么实现一对多关联查询_Laravel Eloquent模型关系定义与预加载【实战】
Laravel如何使用Contracts(契约)进行编程_Laravel契约接口与依赖反转
韩国网站服务器搭建指南:VPS选购、域名解析与DNS配置推荐
EditPlus中的正则表达式 实战(4)
佛山企业网站制作公司有哪些,沟通100网上服务官网?
详解Nginx + Tomcat 反向代理 负载均衡 集群 部署指南
Laravel如何配置任务调度?(Cron Job示例)
如何在沈阳梯子盘古建站优化SEO排名与功能模块?
胶州企业网站制作公司,青岛石头网络科技有限公司怎么样?
Laravel如何实现API版本控制_Laravel版本化API设计方案

