左晓为主开发手持机充值管理机
zuoxiao
1 天以前 2b02b6e854a56a511588e4865ddf2c6597675329
baselibrary/src/main/java/com/dayu/baselibrary/tools/nfc/NativeNfcReadHelper.java
@@ -7,6 +7,8 @@
import android.nfc.tech.MifareClassic;
import android.util.Log;
import androidx.annotation.NonNull;
import com.dayu.baselibrary.bean.BaseManagerToUserCard;
import com.dayu.baselibrary.bean.BaseUserCardCard;
import com.dayu.baselibrary.tools.HexUtil;
@@ -31,9 +33,11 @@
    private static NativeNfcReadHelper helper;
    public NativeNfcReadHelper(Intent intent, Activity activity) {
    @Override
    public void setIntent(Intent intent) {
        this.tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
    }
    /**
     * 单例初始化
@@ -43,9 +47,15 @@
     */
    public static NativeNfcReadHelper getInstence(Intent intent, Activity activity) {
        if (helper == null) {
            helper = new NativeNfcReadHelper(intent, activity);
            helper = new NativeNfcReadHelper();
        }
        helper.setIntent(intent);
        return helper;
    }
    public BaseUserCardCard getUserCardData( BaseUserCardCard userCardCard) {
        return getUserCardData(1, userCardCard);
    }
    /**
@@ -54,8 +64,8 @@
     * @return
     */
    @Override
    public BaseUserCardCard getUserCardData(BaseUserCardCard userCardCard) {
        if (userCardCard!=null){
    public BaseUserCardCard getUserCardData(int sectorIndex, BaseUserCardCard userCardCard) {
        if (userCardCard != null) {
            BaseUserCardCard userCard = null;
            Map<String, List<byte[]>> map = new HashMap<>();
            MifareClassic mfc = MifareClassic.get(tag);
@@ -63,26 +73,31 @@
                try {
                    //链接NFC
                    mfc.connect();
                    //获取扇区数量
                    int count = mfc.getSectorCount();
                    //存储空间
                    int size = mfc.getSize();
                    //用于判断时候有内容读取出来
                    boolean flag = false;
                    List<byte[]> list = new ArrayList<>();
                    //验证扇区密码,否则会报错(链接失败错误)
                    boolean isOpen = false;
                    for (int i = 0; i < listKeyA.size(); i++) {
                        if (mfc.authenticateSectorWithKeyA(1, listKeyA.get(i))) {
                    if (!listKeyA.isEmpty()) {
                        for (int i = 0; i < listKeyA.size(); i++) {
                            if (mfc.authenticateSectorWithKeyA(sectorIndex, listKeyA.get(i))) {
                                isOpen = true;
                                break;
                            }
                        }
                    } else if (!listA_PS.isEmpty()) {
                        if (mfc.authenticateSectorWithKeyA(sectorIndex, defauleKey)) {
                            isOpen = true;
                            break;
                        } else if (mfc.authenticateSectorWithKeyA(sectorIndex, listA_PS.get(sectorIndex))) {
                            isOpen = true;
                        }
                    }
                    if (isOpen) {
                        //获取扇区里面块的数量
                        int bCount = mfc.getBlockCountInSector(1);
                        int bCount = mfc.getBlockCountInSector(sectorIndex);
                        //获取扇区第一个块对应芯片存储器的位置(我是这样理解的,因为第0扇区的这个值是4而不是0)
                        int bIndex = mfc.sectorToBlock(1);
                        int bIndex = mfc.sectorToBlock(sectorIndex);
                        //String data1 = "";
                        for (int j = 0; j < bCount; j++) {
                            //读取数据
@@ -118,9 +133,9 @@
        return null;
    }
    @Override
    public String getCradType() {
        MifareClassic mfc = MifareClassic.get(tag);
        if (null != mfc) {
            try {
@@ -380,18 +395,30 @@
    @Override
    public String getCardNumber() {
        if (tag == null) {
            return "";
        }
        MifareClassic mfc = MifareClassic.get(tag);
        if (null != mfc) {
            try {
                mfc.connect();
                //获取当前卡号
                boolean isOpen = false;
                for (int i = 0; i < listKeyA.size(); i++) {
                    if (mfc.authenticateSectorWithKeyA(0, listKeyA.get(i))) {
                if (!listKeyA.isEmpty()) {
                    for (int i = 0; i < listKeyA.size(); i++) {
                        if (mfc.authenticateSectorWithKeyA(0, listKeyA.get(i))) {
                            isOpen = true;
                            break;
                        }
                    }
                } else if (!listA_PS.isEmpty()) {
                    if (mfc.authenticateSectorWithKeyA(0, defauleKey)) {
                        isOpen = true;
                        break;
                    } else if (mfc.authenticateSectorWithKeyA(0, listA_PS.get(0))) {
                        isOpen = true;
                    }
                }
                if (isOpen) {
                    int bIndex = mfc.sectorToBlock(0);
                    byte[] data = mfc.readBlock(bIndex + 0);
@@ -417,13 +444,65 @@
        return "";
    }
    public String getCardNumberNoClose() {
        if (tag == null) {
            return "";
        }
        MifareClassic mfc = MifareClassic.get(tag);
        if (null != mfc) {
            try {
                mfc.connect();
                //获取当前卡号
                boolean isOpen = false;
                if (!listKeyA.isEmpty()) {
                    for (int i = 0; i < listKeyA.size(); i++) {
                        if (mfc.authenticateSectorWithKeyA(0, listKeyA.get(i))) {
                            isOpen = true;
                            break;
                        }
                    }
                } else if (!listA_PS.isEmpty()) {
                    if (mfc.authenticateSectorWithKeyA(0, defauleKey)) {
                        isOpen = true;
                    } else if (mfc.authenticateSectorWithKeyA(0, listA_PS.get(0))) {
                        isOpen = true;
                    }
                }
                if (isOpen) {
                    int bIndex = mfc.sectorToBlock(0);
                    byte[] data = mfc.readBlock(bIndex + 0);
                    if (data != null && data.length > 0) {
                        String hex = HexUtil.bytesToHex(Arrays.copyOfRange(data, 0, 4));
                        hex = HexUtil.spaceHex(hex);
                        hex = HexUtil.HighLowHex(hex);
                        Log.i("NFCWreatActivity", "hex===" + hex);
                        return hex.toUpperCase();
                    }
                }
            } catch (IOException e) {
                Log.i("NFCWreatActivity", e.toString());
                return BaseCommon.CARD_TYPE_ERROR1;
            }
        }
        return "";
    }
    @Override
    public String getCradTypeAndCardNumber() {
        return getCradTypeAndCardNumber(1, 0, 0);
    }
    /**
     * 获取卡片类型和卡号
     *
     * @return
     */
    @Override
    public String getCradTypeAndCardNumber() {
    public String getCradTypeAndCardNumber(int sectorIndex, int blockIndex, int cardTypeIndex) {
        MifareClassic mfc = MifareClassic.get(tag);
        if (null != mfc) {
@@ -432,10 +511,18 @@
                StringBuilder strData = new StringBuilder();
                //获取当前卡号
                boolean isOpen = false;
                for (int i = 0; i < listKeyA.size(); i++) {
                    if (mfc.authenticateSectorWithKeyA(0, listKeyA.get(i))) {
                if (!listKeyA.isEmpty()) {
                    for (int i = 0; i < listKeyA.size(); i++) {
                        if (mfc.authenticateSectorWithKeyA(0, listKeyA.get(i))) {
                            isOpen = true;
                            break;
                        }
                    }
                } else if (!listA_PS.isEmpty()) {
                    if (mfc.authenticateSectorWithKeyA(0, listA_PS.get(0))) {
                        isOpen = true;
                        break;
                    } else if (mfc.authenticateSectorWithKeyA(0, defauleKey)) {
                        isOpen = true;
                    }
                }
                if (isOpen) {
@@ -451,17 +538,25 @@
                    }
                }
                //获取卡片类型
                for (int i = 0; i < listKeyA.size(); i++) {
                    if (mfc.authenticateSectorWithKeyA(1, listKeyA.get(i))) {
                if (!listKeyA.isEmpty()) {
                    for (int i = 0; i < listKeyA.size(); i++) {
                        if (mfc.authenticateSectorWithKeyA(sectorIndex, listKeyA.get(i))) {
                            isOpen = true;
                            break;
                        }
                    }
                } else if (!listA_PS.isEmpty()) {
                    if (mfc.authenticateSectorWithKeyA(sectorIndex, listA_PS.get(sectorIndex))) {
                        isOpen = true;
                        break;
                    } else if (mfc.authenticateSectorWithKeyA(sectorIndex, defauleKey)) {
                        isOpen = true;
                    }
                }
                if (isOpen) {
                    int bIndex = mfc.sectorToBlock(1);
                    int bIndex = mfc.sectorToBlock(sectorIndex);
                    byte[] data = mfc.readBlock(bIndex + 0);
                    if (data != null && data.length > 0) {
                        String hex = HexUtil.byteToHex(data[0]);
                        String hex = HexUtil.byteToHex(data[cardTypeIndex]);
                        strData.append(hex);
                        Log.i("NFCWreatActivity", "hex===" + hex);
                        return strData.toString().toUpperCase();
@@ -471,6 +566,7 @@
                    return BaseCommon.CARD_TYPE_ERROR2;
                }
            } catch (IOException e) {
                e.printStackTrace();
                return BaseCommon.CARD_TYPE_ERROR1;
            } finally {
                try {
@@ -608,4 +704,6 @@
        }
        return null;
    }
}