android 通过NFC读写15693格式的RFID标签
生活随笔
收集整理的這篇文章主要介紹了
android 通过NFC读写15693格式的RFID标签
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
首先需要在Manifest文件中添加權(quán)限<uses-permission android:name="android.permission.NFC" /><uses-permission android:name="android.permission.NFC_PREFERRED_PAYMENT_INFO" />
NFC根據(jù)不同標簽類型分為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已經(jīng)連接,可以開始讀寫 UID:" + ID,Toast.LENGTH_SHORT).show();try {nfcV = NfcV.get(tag);if(nfcV != null) {nfcV.connect();//建立連接util = new NfcVUtil(nfcV);//創(chuàng)建工具類,操作連接}} catch (Exception e) {e.printStackTrace();}}}/** 寫入RFID卡數(shù)據(jù),** @param v*/public void write(){if(!isEnable()){return;}try {String info = etIdinput.getText().toString();String writeStr = //為a-f的8長度的字符串,轉(zhuǎn)換成byte數(shù)組長度為4。//第一個參數(shù)為塊索引,第二個參數(shù)為4長度的byte數(shù)組。如數(shù)據(jù)不夠,需要補位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位置的內(nèi)容//("讀取結(jié)果 : " + 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數(shù)組行式*/private byte[] ID;private String UID;private String DSFID;private String AFI;/**block的個數(shù)*/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];//可使用的數(shù)據(jù)塊數(shù)量,一般為27oneBlockSize = infoRmation[13];//每個塊byte數(shù)組的長度,一般為4AFI = printHexString(new byte[]{infoRmation[11]});//AFI值在某些領(lǐng)域會需要使用DSFID = printHexString(new byte[]{infoRmation[10]});//DSFID值return infoRmation;}/*** 寫入AFI數(shù)據(jù)* @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數(shù)據(jù)* @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 返回內(nèi)容字符串* @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轉(zhuǎn)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[]轉(zhuǎn)換成16進制字符串* @param data 要轉(zhuǎn)換成字符串的字節(jié)數(shù)組* @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();}//結(jié)果大寫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;}/*** 將數(shù)據(jù)寫入到block,* @param position 要寫內(nèi)容的block位置* @param data 要寫的內(nèi)容,必須長度為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;}}總結(jié)
以上是生活随笔為你收集整理的android 通过NFC读写15693格式的RFID标签的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 用ISAPI方式实现Web页面的自动更新
- 下一篇: php打补丁,PHPMailer库打补丁