android nfc中MifareClassic格式的读写

借鉴文章:https://blog.csdn.net/coslay/article/details/25075595

最近项目中涉及到了nfc,也是找了很多资料借鉴以后最终研究出来

我们的nfc一共有16个扇区,每个扇区又有4个块,而每个块由16个字节数组组成,第一个扇区记录的是厂家的信息,所以第一个扇区不能被写入,而其余每个扇区中有4个块,每个扇区的最后一个块用来存放密码或控制位,其余为数据块,一个块占用16个字节,keyA占用6字节,控制位占用4字节,keyB占用6字节

 

说下nfc重要的几点

1.activity的启动模式要为singtask或者singtop

  <activity android:name=".MainActivity"
            android:launchMode="singleTask">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

2.只要刷nfc就会触发onNewIntent()方法,nfc的读写都是在此方法里执行的

3.所持有的nfc类型要与AndroidManifest.xml中指向的资源文件匹配

4.开启nfc权限

 

 

1.AndroidManifest.xml文件 ,

数据格式的Intent filter

AndroidManifest.xml文件中,要像向下列示例那样,在<activity>元素内的<meta-data>元素中指定你创建的资源文件:

 <intent-filter>
                <action android:name="android.nfc.action.NDEF_DISCOVERED" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.nfc.action.TAG_DISCOVERED" >
                </action>
            </intent-filter>
            <intent-filter>
                <action android:name="android.nfc.action.TECH_DISCOVERED" />
            </intent-filter>
            <meta-data
                android:name="android.nfc.action.TECH_DISCOVERED"
                android:resource="@xml/nfc_tech_filter" />

nfc_tech_filter.xml文件(一个Tag标签只有全部匹配tech-list元素中的tech元素指定的nfc芯片时才认为被匹配):

<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
	<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>

 查看标签支持数据格式的方法:

通过Tag.getTechlist()方法,获得标签所支持的数据格式

 

2.MifareClassic类的常用方法

get():根据Tag对象来获得MifareClassic对象;

Connect():允许对MifareClassic标签进行IO操作;

getType():获得MifareClassic标签的具体类型:TYPE_CLASSIC,TYPE_PLUA,TYPE_PRO,TYPE_UNKNOWN;

getSectorCount():获得标签总共有的扇区数量;

getBlockCount(ÿ

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值