当前位置:   article > 正文

微信小程序读取NFC-MifareClassic1K卡-M1卡详解_微信小程序支持的nfc卡片有哪些

微信小程序支持的nfc卡片有哪些

最近好多朋友私信我想问小程序M1卡的读写问题,现在我给大家伙详细的剖析一下流程。

首先了解一下M1卡的基础知识

M1卡的内存图谱

我们把M1的内存区比作一个硬盘,它的16个扇区就相当于把硬盘分区分了A、B、C...等16个磁盘,每个磁盘上又创了四个文件(块0,块1,块2,块3),每个块都是用16个字节的16进制存放数据

块3:存放了当前扇区的密钥A(占用0-5字节)密钥B(占用10-15字节)、和各个块的访问控制权限(占用6-9字节)

块0~块2:存放用户自定义的数据

认证:

认证是M1进行读取和写入之前必须处理的操作,如果你要操作的块读写只需要密钥A的认证,那就只要做一次密钥A的认证就可以进行读写了,这完全取决于上面说的访问控制权限,关于访问控制本文就不细讲了,想了解的可以去百度学习一下。

指令:

小程序实现M1的认证读取:

//请阅读如下代码注释

  1. let adapter;//定义一个NFC适配器
  2. let mifare;
  3. adapter = wx.getNFCAdapter();
  4. adapter.startDiscovery({
  5. success:function(){
  6. console.log('开启NFC适配器成功。');
  7. },
  8. fail:function(res){
  9. console.log('开启NFC适配器失败!');
  10. if(res.errCode==13000){
  11. //当前设备不支持NFC
  12. }else if(res.errCode==13001){
  13. //系统NFC开关未打开
  14. }else{
  15. //未知错误
  16. }
  17. }
  18. });
  19. //注册发现芯片后处理事件
  20. adapter.onDiscovered(discoverHandlerAuthAndReadM1);
  21. //认证并读取M1卡指定分区处理事件
  22. var discoverHandlerAuthAndReadM1=function(res){
  23. if(res.techs.indexOf('MIFARE Classic') != -1){
  24. GetTagMemery_14443A_MifareClassic(M1Pwd1,M1Pwd2,M1ReadNum);
  25. }else{
  26. //芯片协议不匹配
  27. }
  28. }
  29. //读取14443A MifareClassic内存数据
  30. function GetTagMemery_14443A_MifareClassic(pwd1,pwd2,index){
  31. //读取扇区...
  32. mifare = adapter.getMifareClassic();
  33. mifare.connect({
  34. success: function (ress) {
  35. console.log('MifareClassic已连接',ress);
  36. //认证
  37. var cmd = new Int8Array(12);
  38. //认证指令: 60(KEYA) + 扇区编号 + UID + 12位HEX密钥
  39. if(pwd1!=""){
  40. cmd[0] = parseInt("0x60");
  41. }else{
  42. cmd[0] = parseInt("0x61");
  43. }
  44. cmd[1] = parseInt(parseInt(index)*4, 10);
  45. for (var j = 0; j < uid_arr.length; j++) {
  46. cmd[j + 2] = uid_arr[j];
  47. }
  48. if(pwd1!=""){
  49. cmd[6] = chipTool.hex2int(pwd1.substring(0, 2));
  50. cmd[7] = chipTool.hex2int(pwd1.substring(2, 4));
  51. cmd[8] = chipTool.hex2int(pwd1.substring(4, 6));
  52. cmd[9] = chipTool.hex2int(pwd1.substring(6, 8));
  53. cmd[10] = chipTool.hex2int(pwd1.substring(8, 10));
  54. cmd[11] = chipTool.hex2int(pwd1.substring(10, 12));
  55. }else{
  56. cmd[6] = chipTool.hex2int(pwd2.substring(0, 2));
  57. cmd[7] = chipTool.hex2int(pwd2.substring(2, 4));
  58. cmd[8] = chipTool.hex2int(pwd2.substring(4, 6));
  59. cmd[9] = chipTool.hex2int(pwd2.substring(6, 8));
  60. cmd[10] = chipTool.hex2int(pwd2.substring(8, 10));
  61. cmd[11] = chipTool.hex2int(pwd2.substring(10, 12));
  62. }
  63. var cmdBuffer = cmd.buffer;
  64. //console.log("认证指令为:", cmdBuffer);
  65. //console.log('使用密钥:'+pwd);
  66. mifare.transceive({//发送密钥
  67. data: cmdBuffer,
  68. success: function (res) {
  69. console.log('认证成功, 接收数据如下:', res);
  70. },
  71. fail: function (err) {
  72. //密钥认证失败
  73. mifare.close();
  74. adapter.offDiscovered(discoverHandlerAuthAndReadM1);
  75. adapter.stopDiscovery();
  76. console.log('认证失败', err);
  77. }
  78. });
  79. },
  80. fail: function (err) {
  81. //mifare连接失败
  82. mifare.close();
  83. adapter.offDiscovered(discoverHandlerAuthAndReadM1);
  84. adapter.stopDiscovery();
  85. //console.log('mifare连接失败');
  86. },
  87. complate: function (cp) {
  88. }
  89. })
  90. mifare.isConnected({
  91. success: function (isConnected) {//连接成功
  92. //读取该分区数据
  93. var areaNum=index;
  94. var max=parseInt(areaNum)*4+3;
  95. for (let i = 0; i < 4; i++) {
  96. if(pwd1!=""){
  97. getMifareClassicBlockData(parseInt(areaNum)*4+i,max,pwd1);
  98. }else{
  99. getMifareClassicBlockData(parseInt(areaNum)*4+i,max,pwd2);
  100. }
  101. }
  102. }
  103. });
  104. }
  105. //获取MifareClassic指定区块
  106. function getMifareClassicBlockData(blockNum,maxBlock,pwd){
  107. var cmd = new Int8Array(2);
  108. cmd[0] = parseInt("0x30");
  109. cmd[1] = parseInt(blockNum, 10);
  110. var cmdBuffer = cmd.buffer;
  111. let block_info_tmp = '';
  112. //console.log("读取指令为:", cmdBuffer);
  113. mifare.transceive({
  114. data: cmdBuffer,
  115. success: function (res) {
  116. //console.log('读取数据:', res);
  117. var block_tmp = new Uint8Array(res.data);
  118. for (var j = 0; j < block_tmp.length; j++) {
  119. var tmp = block_tmp[j].toString(16);
  120. if (tmp.length < 2) {
  121. tmp = '0' + tmp;
  122. }
  123. if(j==block_tmp.length-1){
  124. block_info_tmp += tmp;
  125. }else{
  126. block_info_tmp += tmp + ":";
  127. }
  128. }
  129. console.log('第'+blockNum+'块读取数据返回HEX',block_info_tmp);
  130. },
  131. fail: function (err) {
  132. console.log('读取区块'+blockNum+'失败', err);
  133. },
  134. complete:function(){
  135. if(blockNum==maxBlock){
  136. //console.log('M1分区读取结果:',blockInfosM1);
  137. mifare.close();
  138. adapter.offDiscovered(discoverHandlerAuthAndReadM1);
  139. adapter.stopDiscovery();
  140. console.log('关闭mifare连接');
  141. }
  142. }
  143. })
  144. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/571499
推荐阅读
相关标签
  

闽ICP备14008679号