| | |
| | | * email 1058083107@qq.com |
| | | * description |
| | | */ |
| | | public class NFCWriteHelper extends BaseNFCHelper{ |
| | | public class NFCWriteHelper extends BaseNFCHelper { |
| | | |
| | | private Tag tag; |
| | | private NFCWriteHelper.NFCCallback callback; |
| | | |
| | | private static NFCWriteHelper helper; |
| | | private static int PASSWORD_LENTH = 6; |
| | | |
| | |
| | | /** |
| | | * 写卡 |
| | | * |
| | | * @param str 书写内容,16个字节 |
| | | * @param a 书写的扇区 (从0开始数) |
| | | * @param b 书写的块(从0开始数) |
| | | * @param callback 返回监听 |
| | | * @param str 书写内容,16个字节 |
| | | * @param a 书写的扇区 (从0开始数) |
| | | * @param b 书写的块(从0开始数) |
| | | * @param |
| | | */ |
| | | public void writeData(byte[] str, int a, int b, NFCWriteHelper.NFCCallback callback) { |
| | | public boolean writeData(byte[] str, int a, int b) { |
| | | if (str.length <= 16) { |
| | | try { |
| | | MifareClassic mfc = MifareClassic.get(tag); |
| | |
| | | int count = mfc.getSectorCount(); |
| | | //如果传进来的扇区大了或者小了直接退出方法 |
| | | if (a > count - 1 || a < 0) { |
| | | callback.isSusses(false); |
| | | return; |
| | | return false; |
| | | } |
| | | //获取写的扇区的块的数量 |
| | | int bCount = mfc.getBlockCountInSector(a); |
| | | //如果输入的块大了或者小了也是直接退出 |
| | | if (b > bCount - 1 || b < 0) { |
| | | callback.isSusses(false); |
| | | return; |
| | | return false; |
| | | } |
| | | //验证扇区密码 |
| | | boolean isOpen = mfc.authenticateSectorWithKeyA(a, bytes); |
| | |
| | | int bIndex = mfc.sectorToBlock(a); |
| | | //写卡 |
| | | mfc.writeBlock(bIndex + b, str); |
| | | callback.isSusses(true); |
| | | return; |
| | | return true; |
| | | } |
| | | callback.isSusses(false); |
| | | return false; |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | callback.isSusses(false); |
| | | return false; |
| | | } finally { |
| | | try { |
| | | mfc.close(); |
| | |
| | | } |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | return false; |
| | | } |
| | | } |
| | | return false; |
| | | } |
| | | |
| | | |