


下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
【移动应用开发技术】AndroidNFC开发中MifareTag读写的示例分析
本篇文章为大家展示了AndroidNFC开发中MifareTag读写的示例分析,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。针对常用的MifareTag具体说明。MifareTag可以有1K,2K,4K,其内存分区大同小异,下图给出了1K字节容量的Tag的内存分布:数据分为16个区(Sector),每个区有4个块(Block),每个块可以存放16字节的数据,其大小为16X4X16=1024bytes。每个区***一个块称为Trailer,主要用来存放读写该区Block数据的Key,可以有A,B两个Key,每个Key长度为6个字节,缺省的Key值一般为全FF或是0.由MifareClassic.KEY_DEFAULT定义。因此读写MifareTag首先需要有正确的Key值(起到保护的作用),如果鉴权成功:auth
=
mfc.authenticateSectorWithKeyA(j,
MifareClassic.KEY_DEFAULT);然后才可以读写该区数据。本例定义几个Mifare相关的类MifareClassCard,MifareSector,MifareBlock和MifareKey以方便读写MifareTag.Android系统来检测到NFCTag,将其封装成Tag类,存放到Intent的NfcAdapter.EXTRA_TAGExtra数据包中,可以使用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);下面为读取Mifarecard的主要代码://
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<i>
=
mifareBlock;
}
mifareClassCard.setSector(mifareSector.sectorIndex,
mifareSector);
}
else
{
//
Authentication
failed
-
Handle
it
}
}
ArrayList<String>
blockData=new
ArrayList<String>();
int
blockIndex=0;
for(int
i=0;i<secCount;i++){
MifareSector
mifareSector=mifareClassCard.getSector(i);
for(int
j=0;j<MifareSector.BLOCKCOUNT;j++){
MifareBlock
mifareBlock=mifareSector.blocks[j];
byte
[]data=mifareBlock.getData();
blockData.add("Block
"+
blockIndex++
+"
:
"+
Converter.getHexString(data,
data.length));
}
}
String
[]contents=new
String[blockData.size()];
blockData.toArray(contents);
setListAdapter(new
ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,
contents));
getListView().setTextFilterEnabled(true)
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 实时预测算法-洞察及研究
- 小学宪法知识竞赛题库(含参考答案)
- 重庆外国语学校2026届高三(上)开学考试政治试题及答案
- 2025年版能源服务合同范本
- 出国培训安全注意事项
- 2025贷款中介服务合同范本
- 2025调整抚养权合同
- 出入仓安全培训课件
- 大一上政治期末考试卷及答案
- triz技术矛盾解决原理
- 人教版五年级数学上册教学计划(及进度表)
- 委托第三方代付款协议书
- 2025年合伙项目新增合伙人协议书
- 部编版小学道德与法治二年级上册同步表格式教案(全册)
- 养殖厂生物安全管理制度
- 上海市2024-2025学年八年级上册期末模语文卷(2)原卷版
- 交互式游戏设计趋势-深度研究
- 2025年中国海洋功能性食品行业全景评估及投资规划建议报告
- 2025-2030年中国铷行业市场规模分析及投资前景研究报告
- 《中国英语教育史》教学大纲
- 临床医学《门静脉高压症》教学课件
评论
0/150
提交评论