Android NFC开发中Mifare Tag读写的示例分析
发表于:2025-11-14 作者:千家信息网编辑
千家信息网最后更新 2025年11月14日,本篇文章为大家展示了Android NFC开发中Mifare Tag读写的示例分析,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。针对常用的Mifare Tag
千家信息网最后更新 2025年11月14日Android NFC开发中Mifare Tag读写的示例分析
本篇文章为大家展示了Android NFC开发中Mifare Tag读写的示例分析,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。
针对常用的Mifare Tag具体说明。
Mifare Tag 可以有1K ,2K, 4K,其内存分区大同小异,下图给出了1K字节容量的Tag的内存分布:
数据分为16个区(Sector) ,每个区有4个块(Block) ,每个块可以存放16字节的数据,其大小为16 X 4 X 16 =1024 bytes。
每个区***一个块称为Trailer ,主要用来存放读写该区Block数据的Key ,可以有A,B两个Key,每个Key 长度为6个字节,缺省的Key值一般为全FF或是0. 由 MifareClassic.KEY_DEFAULT 定义。
因此读写Mifare Tag 首先需要有正确的Key值(起到保护的作用),如果鉴权成功:
auth = mfc.authenticateSectorWithKeyA(j, MifareClassic.KEY_DEFAULT);
然后才可以读写该区数据。
本例定义几个Mifare相关的类 MifareClassCard ,MifareSector, MifareBlock 和MifareKey 以方便读写Mifare Tag.
Android 系统来检测到NFC Tag, 将其封装成Tag类,存放到Intent的NfcAdapter.EXTRA_TAG Extra 数据包中,可以使用MifareClassic.get(Tag) 获取对象的 MifareClassic类。
Tag tagFromIntent = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG); // 4) Get an instance of the Mifare classic card from this TAG // intent MifareClassic mfc = MifareClassic.get(tagFromIntent);
下面为读取Mifare card 的主要代码:
// 1) Parse the intent and get the action that triggered this intent String action = intent.getAction(); // 2) Check if it was triggered by a tag discovered interruption. if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(action)) { // 3) Get an instance of the TAG from the NfcAdapter Tag tagFromIntent = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG); // 4) Get an instance of the Mifare classic card from this TAG // intent MifareClassic mfc = MifareClassic.get(tagFromIntent); MifareClassCard mifareClassCard=null; try { // 5.1) Connect to card mfc.connect(); boolean auth = false; // 5.2) and get the number of sectors this card has..and loop // thru these sectors int secCount = mfc.getSectorCount(); mifareClassCard= new MifareClassCard(secCount); int bCount = 0; int bIndex = 0; for (int j = 0; j < secCount; j++) { MifareSector mifareSector = new MifareSector(); mifareSector.sectorIndex = j; // 6.1) authenticate the sector auth = mfc.authenticateSectorWithKeyA(j, MifareClassic.KEY_DEFAULT); mifareSector.authorized = auth; if (auth) { // 6.2) In each sector - get the block count bCount = mfc.getBlockCountInSector(j); bCount =Math.min(bCount, MifareSector.BLOCKCOUNT); bIndex = mfc.sectorToBlock(j); for (int i = 0; i < bCount; i++) { // 6.3) Read the block byte []data = mfc.readBlock(bIndex); MifareBlock mifareBlock = new MifareBlock(data); mifareBlock.blockIndex = bIndex; // 7) Convert the data into a string from Hex // format. bIndex++; mifareSector.blocks = mifareBlock; } mifareClassCard.setSector(mifareSector.sectorIndex, mifareSector); } else { // Authentication failed - Handle it } } ArrayList blockData=new ArrayList(); int blockIndex=0; for(int i=0;i(this, android.R.layout.simple_list_item_1, contents)); getListView().setTextFilterEnabled(true); } catch (IOException e) { Log.e(TAG, e.getLocalizedMessage()); showAlert(3); }finally{ if(mifareClassCard!=null){ mifareClassCard.debugPrint(); } } } 运行结果:
上述内容就是Android NFC开发中Mifare Tag读写的示例分析,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注行业资讯频道。
数据
字节
示例
分析
开发
内存
内容
技能
知识
简明
成功
大同小异
简明扼要
下图
两个
代码
作用
大小
容量
对象
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
数据库查询是否党员
计算机网络技术用华硕灵耀14
网络技术跟应用技术哪个好
玉林市网络安全宣传周
银行网络安全现状自查报告
怎么把服务器做代理
数据库图片下载
软件开发的工资那么高为什么
网络安全与网络道德简报
优润软件开发有限公司
支付的软件开发费怎么入账
天津网络时间同步服务器云空间
fuchsia+软件开发
数据库技术与信息管理
1.9.0 isp数据库
云开发数据库资源并发限制5个
我的世界pc有什么服务器
医保 数据库连接失败
吃鸡手游哪个服务器最好
淘宝上的服务器在哪
手把手网络安全培训
我的世界服务器吃什么性能
初中男生学计算机网络技术
美讯在线网络技术有
触摸屏一体机设置asp服务器
标准42u服务器机柜一天用电量
迅邦第一次使用数据库
山西餐饮软件开发高性价比的选择
石家庄源代码网络技术公司
五大连池软件开发设计在线咨询