【移动应用开发技术】Android NFC开发中Mifare Tag读写的示例分析_第1页
【移动应用开发技术】Android NFC开发中Mifare Tag读写的示例分析_第2页
【移动应用开发技术】Android NFC开发中Mifare Tag读写的示例分析_第3页
免费预览已结束,剩余1页可下载查看

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

【移动应用开发技术】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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论