首先需要在Manifest文件中添加权限<uses-permission android:name="android.permission.NFC" /><uses-permission android:name="android.permission.NFC_PREFERRED_PAYMENT_INFO" />

NFC根据不同标签类型分为NfcA(操作14443-3A)、NfcB(操作14443-3B)、NfcF(操作JIS 6319-4)、NfcV(操作15693)。

下面主要介绍NfcV 15693的读写方法。

在activity页面中添加

    NfcV nfcV;NfcVUtil util;@Overrideprotected void onNewIntent(Intent intent) {super.onNewIntent(intent);resolveIntent(intent);}private void resolveIntent(Intent intent) {Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);if (tag != null) {//获取卡id这里即uidbyte[] aa = tag.getId();String str = ByteArrayToHexString(aa);//String UID = flipHexStr(str);Toast.makeText(context,"RFID已经连接,可以开始读写 UID:" + ID,Toast.LENGTH_SHORT).show();try {nfcV = NfcV.get(tag);if(nfcV != null) {nfcV.connect();//建立连接util = new NfcVUtil(nfcV);//创建工具类,操作连接}} catch (Exception e) {e.printStackTrace();}}}/** 写入RFID卡数据,** @param v*/public void write(){if(!isEnable()){return;}try {String info = etIdinput.getText().toString();String writeStr = //为a-f的8长度的字符串,转换成byte数组长度为4。//第一个参数为块索引,第二个参数为4长度的byte数组。如数据不够,需要补位util.writeBlock(0,util.hexStringToBytes(writeStr));} catch (IOException e) {e.printStackTrace();}}public void read(){if(!isEnable()){return;}try {String readOneBlockStr = util.readOneBlock(i);//读取block在1位置的内容//("读取结果 : " + result ,ctx);} catch (IOException e) {e.printStackTrace();}}private boolean isEnable(){if(util == null){//请先连接RFID卡return false;}if(nfcV ==null){//请先连接RFID卡return false;}if(!nfcV.isConnected()){//接连中断,请重新连接return false;}return true;}

工具类NfcVUtil

import android.nfc.tech.NfcV;import java.io.IOException;
import java.nio.charset.StandardCharsets;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;/*** 初始化* @param mNfcV NfcV对象* @throws IOException*/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();//获取标签的信息}public String getUID() {return UID;}/*** 取得标签信息*/private byte[] getInfoRmation() throws IOException{byte[] cmd = new byte[10];cmd[0] = (byte) 0x22; // flagcmd[1] = (byte) 0x2B; // commandSystem.arraycopy(ID, 0, cmd, 2, ID.length); // UIDinfoRmation = mNfcV.transceive(cmd);blockNumber = infoRmation[12];//可使用的数据块数量,一般为27oneBlockSize = infoRmation[13];//每个块byte数组的长度,一般为4AFI = printHexString(new byte[]{infoRmation[11]});//AFI值在某些领域会需要使用DSFID = printHexString(new byte[]{infoRmation[10]});//DSFID值return infoRmation;}/*** 写入AFI数据* @param b* @return* @throws IOException*/public boolean setAFIState(byte b) throws IOException {byte[] cmd = new byte[11];cmd[0] = (byte) 0x22; // flagcmd[1] = (byte) 0x27; // commandSystem.arraycopy(ID, 0, cmd, 2, ID.length); // UIDcmd[10] = b;byte[] res = mNfcV.transceive(cmd);if(res[0] == 0x00){byte[] cmdNew = new byte[10];cmdNew[0] = (byte) 0x22; // flagcmdNew[1] = (byte) 0x2B; // commandSystem.arraycopy(ID, 0, cmdNew, 2, ID.length); // UIDAFI = printHexString(new byte[]{infoRmation[11]});DSFID = printHexString(new byte[]{infoRmation[10]});return true;//千万不要锁定,不可逆
//            byte[] cmd2 = new byte[10];
//            cmd2[0] = (byte) 0x22; // flag
//            cmd2[1] = (byte) 0x28; // command
//            System.arraycopy(ID, 0, cmd2, 2, ID.length); // UID
//            byte[] res2 = mNfcV.transceive(cmd2);
//            if(res2[0] == 0x00){
//               return true;
//            }}return false;}/*** 写入DSFID数据* @param b* @return* @throws IOException*/public boolean setDSFIDState(byte b) throws IOException {byte[] cmd = new byte[11];cmd[0] = (byte) 0x22; // flagcmd[1] = (byte) 0x29; // commandSystem.arraycopy(ID, 0, cmd, 2, ID.length); // UIDcmd[10] = b;byte[] res = mNfcV.transceive(cmd);if(res[0] == 0x00){byte[] cmdNew = new byte[10];cmdNew[0] = (byte) 0x22; // flagcmdNew[1] = (byte) 0x2B; // commandSystem.arraycopy(ID, 0, cmdNew, 2, ID.length); // UIDAFI = printHexString(new byte[]{infoRmation[11]});DSFID = printHexString(new byte[]{infoRmation[10]});return true;//千万不要锁定,不可逆
//            byte[] cmd2 = new byte[10];
//            cmd2[0] = (byte) 0x22; // flag
//            cmd2[1] = (byte) 0x2A; // command
//            System.arraycopy(ID, 0, cmd2, 2, ID.length); // UID
//            byte[] res2 = mNfcV.transceive(cmd2);
//            if(res2[0] == 0x00){
//                return true;
//            }}return false;}public String getDSFID() {return DSFID;}public String getAFI() {String temp = String.valueOf(mNfcV.getDsfId());
//        return AFI;return temp + AFI;}/*** 读取一个位置在position的block* @param position 要读取的block位置* @return 返回内容字符串* @throws IOException*/public String readOneBlock(int position) throws IOException{byte cmd[] = new byte[12];cmd[0] = (byte) 0x22;cmd[1] = (byte) 0x23;//0x20System.arraycopy(ID, 0, cmd, 2, ID.length); // UIDcmd[10] = (byte) position;cmd[11] = (byte) 0;byte res[] = mNfcV.transceive(cmd);if(res[0] == 0x00){byte block[] = new byte[res.length - 1];System.arraycopy(res, 1, block, 0, res.length - 1);return printHexString(block);}return null;}/*** String转Hex格式Byte[]* @param hexString* @return*/public byte[] hexStringToBytes(String hexString) {if (hexString == null || hexString.equals("")) {return null;}hexString = hexString.toUpperCase();int length = hexString.length() / 2;char[] hexChars = hexString.toCharArray();byte[] d = new byte[length];for (int i = 0; i < length; i++) {int pos = i * 2;d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));}return d;}private static byte charToByte(char c) {return (byte) "0123456789ABCDEF".indexOf(c);}/*** 将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();}//结果大写private String ByteArrayToHexString(byte[] inarray) {int i, j, in;String[] hex = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A","B", "C", "D", "E", "F" };String out = "";for (j = inarray.length - 1; j >= 0 ; j--) {in = (int) inarray[j] & 0xff;i = (in >> 4) & 0x0f;out += hex[i];i = in & 0x0f;out += hex[i];}return out;}/*** 将数据写入到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//blockcmd[10] = (byte) position;//valueSystem.arraycopy(data, 0, cmd, 11, data.length);byte[]rsp = mNfcV.transceive(cmd);if(rsp[0] == 0x00)return true;return false;}}

android 通过NFC读写15693格式的RFID标签相关推荐

  1. android中NFC读写功能的实现方法

    这篇文章主要为大家详细介绍了android中NFC读写功能的实现方法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下 本文实例为大家分享了android中NFC读写功能的 ...

  2. Android之NFC读写操作

    上次记录NFC知识时,还处在研究状态,现在项目的第一阶段开发已经完成.上篇Android之NFC开发,简单介绍了一些知识,也是对未知信息的研究,总要了解一点来龙去脉,省的心发慌.这篇文章总结自己的项目 ...

  3. Android开发NFC读写数据

    1.权限<uses-permission android:name="android.permission.NFC"/><uses-feature android ...

  4. Android实现NFC读写

    一.NFC是什么? 近距离无线通讯技术,这个技术由非接触式射频识别(RFID)演变而来,由飞利浦半导体(现恩智浦半导体公司).诺基亚和索尼共同研制开发,其基础是RFID及互连技术.近场通信(Near ...

  5. Android中NFC读写

    参考网址:https://blog.csdn.net/sky2016_w/article/details/79026596 https://www.jianshu.com/p/61f90708bb02 ...

  6. 安卓手机APP读写高频RFID标签(校园卡)NDEF格式数据设计

    ** 通过手机的NFC功能是否能够读取RFID标签 ** 可以读取部分标签 RFID标签有多种类型:依据频率的不同可分为低频(LF).高频(HF).超高频(UHF).微波(MW)电子标签. 1.高频卡 ...

  7. Android基于nfc的读写(一)

    这里写自定义目录标题 Android的NFC读写(一) NFC简介 代码解析 源码(Demo) Android的NFC读写(一) 近来,因需求需要开发nfc读写功能,网上查阅了许多资料,发现相关方面资 ...

  8. Android应用实例之----MifareUltralight格式的nfc标签读写

    随着支持nfc通信功能的智能手机更加普及,在移动支付及公交卡.诊疗卡读写等方面将会发挥更大的作用. 首先介绍Android NFC的工作流程: 步骤1:通过android.nfc.NfcAdapter ...

  9. Android开发——NFC标签读写

    Android开发----NFC标签读写 前言 最近因为项目需要,特意学习了NFC的Android开发.加上之前并没有系统地学习过Android开发知识,起手比较困难,搞了半天才算一知半解.怎么办呢? ...

  10. 安卓Android下如何开发USB NFC读写器APP

    荣士第二代USB免驱动IC读写器IC-02支持Windows.安卓(Android).Linux系统,为方便IC-02读写器能快速的接入安卓(Android)系统,我们提供了各种安卓版本的So库及示例 ...

最新文章

  1. Kafka(1)-概述
  2. akka es/cqrs_在Akka中实现主从/网格计算模式
  3. 正则表达式小括号的多义性
  4. 成长的道路上,我很幸运
  5. 为什么我在实时编码时失败了?
  6. YIi 数据操作备注
  7. 不同编码页引用同一个css文件
  8. element的form表单中如何一行显示多el-form-item标签
  9. 日月神话_功能色彩神话
  10. 100m网速测试软件,网速测试哦(100兆宽带wifi最佳设置)
  11. windows11账户登录不上去怎么办?
  12. 使用eNSP配置防火墙USG6000v双机热备(VGMP+HRP+OSPF+NAT)
  13. nginx服务器404错误页面设置完整版
  14. StudyJams-第01课_初识Android的View(TextView、ImageView、Button)
  15. BEA-141281
  16. 看过《非你莫属》那期,因为刘俐俐,说说陈鸥
  17. matlab实验八,matlab实验八
  18. 8-设计模式之行为型模式二(状态模式、观察者模式、中介者模式)
  19. asp.net webs制作(包括连接数据库)
  20. 化工厂人员定位解决方案,智能化工厂定位系统无所不在-新导智能

热门文章

  1. RecyclerView 数据刷新的几种方式 局部刷新 notify MD
  2. CCF推荐国际学术会议和期刊目录2019年
  3. 23种设计模式之模板方法模式
  4. ES6面试题(参考文档)
  5. Eclipse启动时 弹出subversive connector discovery
  6. 文本在线查重系统的设计与实现(毕业设计)
  7. 菜鸟教程python100例-菜鸟教程python
  8. VC++ MsChart控件怎么用?
  9. Java基础编程练习题
  10. gif一键抠图 在线_给视频抠图?这个在线免费神器超厉害