实例介绍
【实例简介】
【实例截图】
【实例截图】
【核心代码】
package com.example.mynfcdemon;
import java.io.IOException;
import android.nfc.tech.NfcV;
public class NfcVUtil {
private NfcV mNfcV;
//UID数组行式
private byte[] ID;
private String UID;
private String DSFID;
private String AFI;
//block的个数
private int blockNumber;
//一个block长度
private int oneBlockSize;
//信息
private byte[] infoRmation;
/*@function :初始化
@param :mNfcV NfcV对象
@return :返回内容byte[]
@author: :Demon 503536038@qq.com
*/
public NfcVUtil(NfcV mNfcV) throws IOException{
this.mNfcV = mNfcV;
ID = this.mNfcV.getTag().getId();
byte[] uid = new byte[ID.length];
int j = 0;
for(int i = ID.length - 1; i>=0; i-- ){
uid[j] = ID[i];
j ;
}
this.UID = printHexString(uid);
getInfoRmation();
System.out.println("UID:" getUID()
"AFI:" getAFI()
"DSFID:" getDSFID()
"BlockNumber:" getBlockNumber()
"BlockSize:" getOneBlockSize());
}
public String getUID() {
return UID;
}
/*@function :取得标签信息
@return :返回内容byte[]
@author: :Demon 503536038@qq.com
*/
private byte[] getInfoRmation() throws IOException{
byte[] cmd = new byte[10];
cmd[0] = (byte) 0x22; //flag
cmd[1] = (byte) 0x2B; //command
System.arraycopy(ID, 0, cmd, 2, ID.length); // UID
infoRmation = mNfcV.transceive(cmd);
blockNumber = infoRmation[12];
oneBlockSize = infoRmation[13];
AFI = printHexString(new byte[]{infoRmation[11]});
DSFID = printHexString(new byte[]{infoRmation[10]});
return infoRmation;
}
public String getDSFID() {
return DSFID;
}
public String getAFI() {
return AFI;
}
public int getBlockNumber(){
return blockNumber 1;
}
public int getOneBlockSize() {
return oneBlockSize 1;
}
/*@function :读取一个位置在position的block
@param :position 要读取的block位置
@return :返回内容字符串
@author: :Demon 503536038@qq.com
*/
public String readOneBlock(int position) throws IOException{
byte cmd[] = new byte[11];
cmd[0] = (byte) 0x22;
cmd[1] = (byte) 0x20;
System.arraycopy(ID, 0, cmd, 2, ID.length); //UID
cmd[10] = (byte) position;
byte res[] = mNfcV.transceive(cmd);
for(int i=0; i < res.length; i ){
System.out.println("/" res[i]);
}
String r = new String(res);
System.out.println("/" r);
if(res[0] == 0x00){
byte block[] = new byte[res.length - 1];
System.arraycopy(res, 1, block, 0, res.length - 1);
//return printHexString(block);
String blockstr = new String(block);
return blockstr;
}
return null;
}
/*@function :读取从begin开始end个block
@instructions :begin count 不能超过blockNumber
@param :begin block开始位置
@param :count 读取block数量
@return :返回内容字符串
@author: :Demon 503536038@qq.com
*/
public String readBlocks(int begin, int count) throws IOException{
if((begin count)>blockNumber){
count = blockNumber - begin;
}
StringBuffer data = new StringBuffer();
for(int i = begin; i<=count begin; i ){
data.append(readOneBlock(i));
}
return data.toString();
}
/* 将byte[]转换成16进制字符串
@param data 要转换成字符串的字节数组
@return 16进制字符串
*/
private String printHexString(byte[] data) {
StringBuffer s = new StringBuffer();;
for (int i = 0; i < data.length; i ) {
String hex = Integer.toHexString(data[i] & 0xFF);
if (hex.length() == 1) {
hex = '0' hex;
}
s.append(hex);
}
return s.toString();
}
/* 将数据写入到block,
@param position 要写内容的block位置
@param data 要写的内容,必须长度为blockOneSize
@return false为写入失败,true为写入成功
@throws IOException */
public boolean writeBlock(int position, byte[] data) throws IOException{
byte cmd[] = new byte[15];
cmd[0] = (byte) 0x22;
cmd[1] = (byte) 0x21;
System.arraycopy(ID, 0, cmd, 2, ID.length);// UID
//block
cmd[10] = (byte) position;
//value
System.arraycopy(data, 0, cmd, 11, data.length);
byte[]rsp = mNfcV.transceive(cmd);
if(rsp[0] == 0x00){
return true;
}
return false;
}
public boolean writeStrToTag(String str){
return false;
}
}
好例子网口号:伸出你的我的手 — 分享!
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明


网友评论
我要评论