左晓为主开发手持机充值管理机
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,10 +33,6 @@
    private static NativeNfcReadHelper helper;
    public NativeNfcReadHelper(Intent intent, Activity activity) {
    }
    @Override
    public void setIntent(Intent intent) {
        this.tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
@@ -49,7 +47,7 @@
     */
    public static NativeNfcReadHelper getInstence(Intent intent, Activity activity) {
        if (helper == null) {
            helper = new NativeNfcReadHelper(intent, activity);
            helper = new NativeNfcReadHelper();
        }
        helper.setIntent(intent);
        return helper;
@@ -128,7 +126,6 @@
    @Override
    public String getCradType() {
        MifareClassic mfc = MifareClassic.get(tag);
        if (null != mfc) {
            try {
@@ -388,6 +385,9 @@
    @Override
    public String getCardNumber() {
        if (tag == null) {
            return "";
        }
        MifareClassic mfc = MifareClassic.get(tag);
        if (null != mfc) {
            try {
@@ -402,7 +402,9 @@
                        }
                    }
                } else if (!listA_PS.isEmpty()) {
                    if (mfc.authenticateSectorWithKeyA(0, listA_PS.get(0))) {
                    if (mfc.authenticateSectorWithKeyA(0, defauleKey)) {
                        isOpen = true;
                    } else if (mfc.authenticateSectorWithKeyA(0, listA_PS.get(0))) {
                        isOpen = true;
                    }
                }
@@ -434,6 +436,9 @@
    public String getCardNumberNoClose() {
        if (tag == null) {
            return "";
        }
        MifareClassic mfc = MifareClassic.get(tag);
        if (null != mfc) {
            try {
@@ -448,7 +453,9 @@
                        }
                    }
                } else if (!listA_PS.isEmpty()) {
                    if (mfc.authenticateSectorWithKeyA(0, listA_PS.get(0))) {
                    if (mfc.authenticateSectorWithKeyA(0, defauleKey)) {
                        isOpen = true;
                    } else if (mfc.authenticateSectorWithKeyA(0, listA_PS.get(0))) {
                        isOpen = true;
                    }
                }
@@ -473,13 +480,19 @@
        return "";
    }
    @Override
    public String getCradTypeAndCardNumber() {
        return getCradTypeAndCardNumber(1);
    }
    /**
     * 获取卡片类型和卡号
     *
     * @return
     */
    @Override
    public String getCradTypeAndCardNumber() {
    public String getCradTypeAndCardNumber(int sectorIndex) {
        MifareClassic mfc = MifareClassic.get(tag);
        if (null != mfc) {
@@ -488,10 +501,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) {
@@ -507,15 +528,23 @@
                    }
                }
                //获取卡片类型
                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);
                    byte[] data = mfc.readBlock(bIndex + 0);
                    int bIndex = mfc.sectorToBlock(sectorIndex);
                    byte[] data = mfc.readBlock(bIndex + sectorIndex);
                    if (data != null && data.length > 0) {
                        String hex = HexUtil.byteToHex(data[0]);
                        strData.append(hex);
@@ -664,4 +693,8 @@
        }
        return null;
    }
}