发卡器介绍:https://item.taobao.com/item.htm?spm=a1z10.5-c.w4002-17663462238.11.14c4789euYabVr&id=615391857885

import com.sun.jna.Library ;
import com.sun.jna.Native;
import java.io.IOException;interface CLibrary extends Library {//DLL绝对路径的地址获取,注意要去空格,不要使用中文目录//不同版本的读写器,接口DLL文件名称、函数名称是一样的,但内核代码不一样,请选用与读写器、操作系统一致的OUR_MIFARE.dllString filePath = CLibrary.class.getResource("").getPath().replaceFirst("/","").replaceAll("%20"," ")+"OUR_MIFARE";CLibrary sdtapi = (CLibrary) Native.loadLibrary(filePath, CLibrary.class);//动态链接库中的方法byte pcdbeep(int xms);                         //让设备发出声音byte pcdgetdevicenumber(byte[] devicenumber);  //读取设备编号byte piccrequest_ul(byte[] mypiccserial);      //读卡的序列号byte piccread_ul(byte ReadPageBegin,byte[] mypiccdata);//读卡内4个页数据byte piccwrite_ul(byte WritePage,byte[] mypiccdata);   //写数据到指定页byte piccauthkey_ntag(byte[] mypicckey,byte[] mypiccntagpack);  //校验卡片密码byte piccreadex_ntag(byte myctrlword,byte[] mypiccserial,byte[] mypicckey,byte myblockaddr,byte myblocksize,byte[] mypiccdata);    //轻松读卡,一次最多可读12页共48个字节的数据byte piccwriteex_ntag(byte myctrlword,byte[] mypiccserial,byte[] mypicckey,byte myblockaddr,byte myblocksize,byte[] mypiccdata);   //轻松写卡,一次最多可读11页共44个字节的数据byte piccinit_ntag(byte myctrlword,byte[] mypiccserial,byte[] mypicckey,byte[] mypiccdata); //设置卡密码及保护机制
}public class RWNtag21x {public static void main(String[] args) throws Exception {System.setProperty("jna.encoding", "GBK");String filePath = CLibrary.class.getResource("").getPath().replaceFirst("/", "").replaceAll("%20", " ") + "OUR_MIFARE.DLL";System.out.println("本示例引用的DLL文件:" + filePath);if (args.length == 0) {System.out.println("\n请先输入运行参数!");System.out.println("\n参数 0:驱动读卡器嘀一声");System.out.println("\n参数 1:读取设备编号");System.out.println("\n参数 2:读卡的序列号");System.out.println("\n参数 3:读卡内4个连续页的数据");System.out.println("\n参数 4:写数据到指定页");System.out.println("\n参数 5:校验卡片密码");System.out.println("\n参数 6:轻松读卡,一次最多可读12页共48个字节的数据");System.out.println("\n参数 7:轻松写卡,一次最多可读11页共44个字节的数据");System.out.println("\n参数 8:设置卡密码及保护机制");return;}//Java中只能使用string1.equals(string2)的方式来比较字符串if (args[0].equals("0")) {             //驱动读卡器发嘀一声System.out.print("\n0-驱动读卡器嘀一声\n");CLibrary.sdtapi.pcdbeep(50);System.out.print("结果:如果能听到读卡器嘀一声表示成功,否则请检查读卡器是否已连上线!\n");} else if (args[0].equals("1")){         //读取设备编号,可做为软件加密狗用,也可以根据此编号在公司网站上查询保修期限int status;                          //存放返回值byte[] devicenumber = new byte[4];   //4字节设备编号status = (int) (CLibrary.sdtapi.pcdgetdevicenumber(devicenumber) & 0xff);//& 0xff用于转为无符号行数据System.out.print("\n1-读取设备编号\n");System.out.print("结果:");if (status == 0) {CLibrary.sdtapi.pcdbeep(38);System.out.print("读取成功!设备编号为:" + (devicenumber[0] & 0xff) + "-" + (devicenumber[1] & 0xff) + "-" + (devicenumber[2] & 0xff) + "-" + (devicenumber[3] & 0xff) + "\n");} else {PrintErrInf(status);   //返回代码提示}}else if (args[0].equals("2")){          //读卡的序列号int status;                          //存放返回值byte[] mypiccserial = new byte[7];   //7字节卡号status = (int) (CLibrary.sdtapi.piccrequest_ul(mypiccserial) & 0xff);//& 0xff用于转为无符号行数据System.out.print("\n2-读卡的序列号\n");if (status == 0) {CLibrary.sdtapi.pcdbeep(38);String uidstr = "";for (int i = 0; i < 7; i++) {String bytestr = "00" + Integer.toHexString(mypiccserial[i] & 0xff);uidstr = uidstr + bytestr.substring(bytestr.length() - 2, bytestr.length()) + " ";}System.out.print("7字节卡序号:" + uidstr+"\n");} else {PrintErrInf(status);   //返回代码提示}}else if (args[0].equals("3")){          //读卡内4个页的数据int status;                          //存放返回值byte[] mypiccserial = new byte[7];   //7字节卡号byte[] mypiccdata=new byte[16];      //16个字节读卡的数据缓冲byte ReadPageBegin=4;                //读起始页地址status = (int) (CLibrary.sdtapi.piccrequest_ul(mypiccserial) & 0xff);//& 0xff用于转为无符号行数据if (status == 0) {String uidstr = "";for (int i = 0; i < 7; i++) {String bytestr = "00" + Integer.toHexString(mypiccserial[i] & 0xff);uidstr = uidstr + bytestr.substring(bytestr.length() - 2, bytestr.length()) + " ";}System.out.print("\n3-读卡内4个页的数据");System.out.print("\n卡序号:" + uidstr);status = (int) (CLibrary.sdtapi.piccread_ul(ReadPageBegin,mypiccdata) & 0xff);//& 0xff用于转为无符号行数据if (status == 0) {CLibrary.sdtapi.pcdbeep(38);String databuf = "";for (int i = 0; i < mypiccdata.length ; i++) {String bytestr = "00" + Integer.toHexString(mypiccdata[i] & 0xff);databuf = databuf + bytestr.substring(bytestr.length() - 2, bytestr.length()) + " ";}System.out.print("\n页内数据:" + databuf + "\n");}else {PrintErrInf(status);   //返回代码提示}} else {PrintErrInf(status);   //返回代码提示}}else if (args[0].equals("4")){          //写数据到指定页int status;                          //存放返回值byte[] mypiccserial = new byte[7];   //7字节卡号byte[] mypiccdata=new byte[4];       //4个字节的数据缓冲byte WritePage=4;                    //页地址status = (int) (CLibrary.sdtapi.piccrequest_ul(mypiccserial) & 0xff);//& 0xff用于转为无符号行数据if (status == 0) {String uidstr = "";for (int i = 0; i < 7; i++) {String bytestr = "00" + Integer.toHexString(mypiccserial[i] & 0xff);uidstr = uidstr + bytestr.substring(bytestr.length() - 2, bytestr.length()) + " ";}System.out.print("\n4-写数据到指定页");System.out.print("\n卡序号:" + uidstr+"\n");mypiccdata[0]=0;mypiccdata[1]=1;mypiccdata[2]=2;mypiccdata[3]=3;status = (int) (CLibrary.sdtapi.piccwrite_ul(WritePage,mypiccdata) & 0xff);//& 0xff用于转为无符号行数据if (status == 0) {CLibrary.sdtapi.pcdbeep(38);System.out.print("第"+Integer.toString(WritePage)+"页数据改写成功!\n");}else {PrintErrInf(status);   //返回代码提示}} else {PrintErrInf(status);   //返回代码提示}}else if (args[0].equals("5")){          //校验卡片密码int status;                          //存放返回值byte[] mypiccserial = new byte[7];   //7字节卡号byte[] mypicckey=new byte[4];        //4个字节的卡密码byte[] mypiccntagpack=new byte[2];   //返回的确认码status = (int) (CLibrary.sdtapi.piccrequest_ul(mypiccserial) & 0xff);//& 0xff用于转为无符号行数据if (status == 0) {String uidstr = "";for (int i = 0; i < 7; i++) {String bytestr = "00" + Integer.toHexString(mypiccserial[i] & 0xff);uidstr = uidstr + bytestr.substring(bytestr.length() - 2, bytestr.length()) + " ";}System.out.print("\n5-校验卡片密码");System.out.print("\n卡序号:" + uidstr+"\n");status = (int) (CLibrary.sdtapi.piccauthkey_ntag(mypicckey,mypiccntagpack) & 0xff);//& 0xff用于转为无符号行数据if (status == 0) {CLibrary.sdtapi.pcdbeep(38);System.out.print("卡密码认证成功!确认码为:"+Integer.toHexString(mypiccntagpack[0])+" "+Integer.toHexString(mypiccntagpack[1]));}else {PrintErrInf(status);   //返回代码提示}} else {PrintErrInf(status);   //返回代码提示}}else if (args[0].equals("6")){          //校轻松读卡,一次最多可读12页共48个字节的数据int status;                          //存放返回值byte[] mypiccserial = new byte[7];   //7字节卡号byte[] mypicckey=new byte[4];        //4个字节的卡密码byte[] mypiccdata=new byte[48];      //48个字节读卡的数据缓冲byte myblockaddr=4;                  //读起始页地址byte myblocksize=12;                 //读卡页数byte myctrlword=0;                   //取值0不认证密码,16要认证卡密码if(myctrlword==16) {   //如果要认证卡密码,设置认证密码mypicckey[0]=(byte) 0x12;mypicckey[1]=(byte) 0x34;mypicckey[2]=(byte) 0x56;mypicckey[3]=(byte) 0x78;}System.out.print("\n6-轻松读卡");status = (int) (CLibrary.sdtapi.piccreadex_ntag(myctrlword,mypiccserial,mypicckey,myblockaddr,myblocksize,mypiccdata) & 0xff);//& 0xff用于转为无符号行数据if (status == 0) {CLibrary.sdtapi.pcdbeep(38);String uidstr = "";for (int i = 0; i < 7; i++) {String bytestr = "00" + Integer.toHexString(mypiccserial[i] & 0xff);uidstr = uidstr + bytestr.substring(bytestr.length() - 2, bytestr.length()) + " ";}System.out.print("\n卡序号:" + uidstr);String databuf="";for (int i = 0; i < myblocksize*4; i++) {String bytestr = "00" + Integer.toHexString(mypiccdata[i] & 0xff);databuf = databuf + bytestr.substring(bytestr.length() - 2, bytestr.length()) + " ";}System.out.print("\n卡内数据:" + databuf+"\n");} else {PrintErrInf(status);   //返回代码提示}}else if (args[0].equals("7")) {         //轻松写卡,一次最多可读11页共44个字节的数据int status;                          //存放返回值byte[] mypiccserial = new byte[7];   //7字节卡号byte[] mypicckey = new byte[4];      //4个字节的卡密码byte[] mypiccdata = new byte[44];    //44个字节写卡缓冲byte myblockaddr = 4;                //写起始页地址byte myblocksize = 11;               //写卡页数byte myctrlword = 0;                 //取值0不认证密码,16要认证卡密码if (myctrlword == 16) {   //如果要认证卡密码,设置认证密码mypicckey[0] = (byte) 0x12;mypicckey[1] = (byte) 0x34;mypicckey[2] = (byte) 0x56;mypicckey[3] = (byte) 0x78;}//写中文或字母数字等字符信息,将要写入的字符转ASCII码写入String WriteStr = "伟大的中华人民共和国万岁!1949-10-01                                "; //将要写入的文字生成字节数组byte[] WriteBuf = WriteStr.getBytes("gb2312");for (int i = 0; i < myblocksize*4; i++) {  //指定写卡数据,写卡页数*4个字节mypiccdata[i] = WriteBuf[i];}System.out.print("\n7-轻松写卡");status = (int) (CLibrary.sdtapi.piccwriteex_ntag(myctrlword, mypiccserial, mypicckey, myblockaddr, myblocksize, mypiccdata) & 0xff);//& 0xff用于转为无符号行数据if (status == 0) {CLibrary.sdtapi.pcdbeep(38);String uidstr = "";for (int i = 0; i < 7; i++) {String bytestr = "00" + Integer.toHexString(mypiccserial[i] & 0xff);uidstr = uidstr + bytestr.substring(bytestr.length() - 2, bytestr.length()) + " ";}System.out.print("\n卡序号:" + uidstr + ",写卡成功!\n");} else {PrintErrInf(status);   //返回代码提示}}else if (args[0].equals("8")) {         //设置卡密码及保护机制int status;                          //存放返回值byte[] mypicckey = new byte[4];      //4个字节的卡旧密码byte[] newPickey = new byte[44];     //4个字节的新密码byte[] mypiccserial = new byte[7];   //7字节卡号byte EnOrDisKey = 1;                 //0开启密码保护功能,1取消密码保护功能byte ReadNeedKey = 0;                //取值0读卡不需要认证密码,非0读卡也要认证密码byte myctrlword = 16;                //取值0不需要认证密码,16要认证卡密码  对卡操作byte KeyErrNum=0;                    //对卡操作允许密码错误次数(0为不限次)byte[] mypiccdata=new byte[16];      //本次对卡的配置信息if (myctrlword == 16) {   //如果要认证卡密码,设置认证密码mypicckey[0] = (byte) 0x12;mypicckey[1] = (byte) 0x34;mypicckey[2] = (byte) 0x56;mypicckey[3] = (byte) 0x78;}if(EnOrDisKey==0){      //卡片开启密码保护功能,开启后要记住卡密码,否则卡报废!!!myctrlword=(byte)(myctrlword+4);mypiccdata[3]=20;               //配置:密码保护起始页,要根据不同类型的卡来设置myctrlword=(byte)(myctrlword+1);mypiccdata[4]=(byte)(KeyErrNum % 8); //配置:最大卡密码认证错误次数if(ReadNeedKey>0){                   //配置:读卡时需要认证卡密码mypiccdata[4]=(byte)(mypiccdata[4]+(byte)(0x80));}myctrlword=(byte)(myctrlword+2);   //配置:启用计数器newPickey[0]=(byte) 0x12;newPickey[1]=(byte) 0x34;newPickey[2]=(byte) 0x56;newPickey[3]=(byte) 0x78;  //配置:4个字节的新密码mypiccdata[12]=(byte)(0x16);mypiccdata[13]=(byte)(0x16);}else{   //卡片取消密码保护功能mypiccdata[3]=(byte)(0xff);myctrlword=(byte)(myctrlword+1);myctrlword=(byte)(myctrlword+2);}System.out.print("\n8-设置卡密码及保护机制");status = (int) (CLibrary.sdtapi.piccinit_ntag(myctrlword,mypiccserial,mypicckey,mypiccdata) & 0xff);//& 0xff用于转为无符号行数据if (status == 0) {CLibrary.sdtapi.pcdbeep(38);String uidstr = "";for (int i = 0; i < 7; i++) {String bytestr = "00" + Integer.toHexString(mypiccserial[i] & 0xff);uidstr = uidstr + bytestr.substring(bytestr.length() - 2, bytestr.length()) + " ";}System.out.print("\n卡序号:" + uidstr + ",设置卡密码及保护机制成功!\n");} else {PrintErrInf(status);   //返回代码提示}}}//----------------------------------------------------------------------------------返回代码提示static void PrintErrInf(int errcode) {switch(errcode){case 1:System.out.print("错误代码:1,0~2块都没读出来,可能刷卡太块。但卡序列号已被读出来!");break;case 2:System.out.print("错误代码:2,第0块已被读出,但1~2块读取失败。卡序列号已被读出来!");break;case 3:System.out.print("错误代码:3,第0、1块已被读出,但2块读取失败。卡序列号已被读出来!");break;case 8:System.out.print("错误代码:8,未寻到卡,请重新拿开卡后再放到感应区!");break;case 9:System.out.print("错误代码:9,有多张卡在感应区,寻卡过程中防冲突失败,读序列吗错误!");break;case 10:System.out.print("错误代码:10,该卡可能已被休眠,无法选中卡片!");break;case 11:System.out.print("错误代码:11,密码装载失败!");break;case 12:System.out.print("错误代码:12,卡片密码认证失败!");break;case 13:System.out.print("错误代码:13,读本块失败,原因是刷卡太快或本块所对应的区还没通过密码认证!");break;case 14:System.out.print("错误代码:14,写本块失败,原因是刷卡太快或本块所对应的区还没通过密码认证!");break;case 21:System.out.print("错误代码:21,没有动态库!");break;case 22:System.out.print("错误代码:22,动态库或驱动程序异常!");break;case 23:System.out.print("错误代码:23,驱动程序错误或尚未安装!");break;case 24:System.out.print("错误代码:24,操作超时,一般是动态库没有反映!");break;case 25:System.out.print("错误代码:25,发送字数不够!");break;case 26:System.out.print("错误代码:26,发送的CRC错!");break;case 27:System.out.print("错误代码:27,接收的字数不够!");break;case 28:System.out.print("错误代码:28,接收的CRC错!");break;default:System.out.print("未知错误,错误代码:"+Integer.toString(errcode));break;}}
}

Java读写NFC标签Ntag2x芯片源码相关推荐

  1. C#读写NFC系列Ntag2x芯片源码

    本说明使用设备的淘宝链接:https://item.taobao.com/item.htm?spm=a1z10.5-c.w4002-17663462238.11.1386789eDRQjgF& ...

  2. VFP读写NFC标签Ntag卡

    本示例使用的读写器:Android Linux RFID读写器NFC发卡器WEB可编程NDEF文本/智能海报/-淘宝网 (taobao.com) NTAG21x系列是恩智浦半导体(NXP Semico ...

  3. 西门子PLC源码图纸S7-224XP-226 ARM芯片代码ST芯片源码

    西门子PLC源码图纸S7-224XP-226 ARM芯片代码ST芯片源码 ID:879638621827400东莞现货

  4. Android NFC 标签读写Demo与历史漏洞概述

    文章目录 前言 NFC基础 1.1 RFID区别 1.2 工作模式 1.3 日常应用 NFC标签 2.1 标签应用 2.2 应用实践 2.3 标签预览 2.4 前台调度 NFC开发 3.1 NDEF数 ...

  5. c语言读写nfc,Android NFC M1卡读写芯片卡读写(CPU卡读写)(RFID读写)

    版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/sgn5200/article/deta ...

  6. android nfc标签类型,Android NFC标签 开发深度解析 触碰的艺术

    原标题:Android NFC标签 开发深度解析 触碰的艺术 本文来自于CSDN博客,作者:郭朝,已获授权,版权归原作者所有,未经作者同意,请勿转载. 欢迎同有博客好文章的作者加微信(ID:tm_fo ...

  7. NFC标签IC - NTA5332

    什么是NFC? NFC: Near Field Communication NFC或近场通信是一种协议,当两个设备紧挨着对方时,可以帮助它们进行无线通信--例如,智能手机或智能手表可以用于支付或登机牌 ...

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

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

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

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

  10. ACR122u读写NTAG标签NFC标签

    ACR122u读写器是一种广泛应用的NFC读写器,NTAG213/215/216是常见的电子标签,他有7字节UID号,有数据存储区,支持密码验证,以及返回确认密码,可以应用在防伪,身份验证等 下边是支 ...

最新文章

  1. 【翻译】SQL Server索引进阶:第七级,过滤的索引
  2. 使用docker安装部署Spark集群来训练CNN(含Python实例)
  3. Angularjs 开始之Hello world
  4. RIP、 OSPF、 EIGRP的区别
  5. java 反射类成员_java 反射(二)类成员
  6. SpringCloud发现服务代码(EurekaClient,DiscoveryClient)
  7. 自学了三个月的软件测试,从小白到自动化测试工程师,我是如何从零拿到大厂offer的
  8. 首届Ceph亚太峰会来了!内有粉丝福利
  9. 大学生计算机考试PPT制作,计算机等级考试制作PPT表格
  10. visual studio 2019/2022 安装时卡住,一直正在提取文件时的亲测有效的解决方案
  11. 数据结构习题——第一章 绪论
  12. win10 net framework 3.5提示错误代码0x800f081f
  13. Win10、Win7系统,电脑蓝屏,什么原因怎么解决?一篇文章彻底解决!
  14. 金典《歌手》2019即将首播 创作季上演神仙打架
  15. 硬编码和软编码的区别
  16. 【Turtle圣诞系列】今年的圣诞树都来了圣诞还会远吗?(内含多份源码)
  17. 清洁机器人--屏幕显示之OLED屏和LCD屏的区别
  18. 计算机电源输出电压 电流,电脑USB接口的输出电压以及电流你知道吗?
  19. Linux DSA Net Switch驱动开发
  20. 电容笔和触控笔有什么区别?ipad手写电容笔推荐品牌

热门文章

  1. Spring boot设置文件上传大小限制
  2. Spring Quartz配置
  3. 临床基因组分析相关数据库汇总
  4. 西门子1500和300哪个贵_西门子PLC S7-300和1500之间的通信资源管理之比较?
  5. SAI 串行音频接口学习
  6. 《模拟电子技术》–童诗白
  7. java爬虫基础知识,Java网络爬虫基础知识
  8. 软件测试全套教程,软件测试自学线路图
  9. 关于锐捷校园网断网的解决办法
  10. springboot毕设项目作业查重系统i667s(java+VUE+Mybatis+Maven+Mysql)