左晓为主开发手持机充值管理机
zuoxiao
3 天以前 548dce0bcff06ee7a1795defe8e7789b9d7aad94
baselibrary/src/main/java/com/dayu/baselibrary/tools/nfc/NativeNfcReadHelper.java
@@ -31,9 +31,13 @@
    private static NativeNfcReadHelper helper;
    public NativeNfcReadHelper(Intent intent, Activity activity) {
    @Override
    public void setIntent(Intent intent) {
        this.tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
    }
    /**
     * 单例初始化
@@ -43,8 +47,9 @@
     */
    public static NativeNfcReadHelper getInstence(Intent intent, Activity activity) {
        if (helper == null) {
            helper = new NativeNfcReadHelper(intent, activity);
            helper = new NativeNfcReadHelper();
        }
        helper.setIntent(intent);
        return helper;
    }
@@ -53,8 +58,9 @@
     *
     * @return
     */
    @Override
    public BaseUserCardCard getUserCardData(BaseUserCardCard userCardCard) {
        if (userCardCard!=null){
        if (userCardCard != null) {
            BaseUserCardCard userCard = null;
            Map<String, List<byte[]>> map = new HashMap<>();
            MifareClassic mfc = MifareClassic.get(tag);
@@ -117,8 +123,9 @@
        return null;
    }
    public String getCradType() {
    @Override
    public String getCradType() {
        MifareClassic mfc = MifareClassic.get(tag);
        if (null != mfc) {
            try {
@@ -161,6 +168,7 @@
     *
     * @param callback
     */
    @Override
    public void getAllData(final NFCCallMapback callback) {
        Map<String, List<byte[]>> map = new HashMap<>();
        MifareClassic mfc = MifareClassic.get(tag);
@@ -232,6 +240,7 @@
     *
     * @param callback
     */
    @Override
    public void getOneSectorData(NFCCallListback callback) {
@@ -305,6 +314,7 @@
     * @param b        块
     * @param callback
     */
    @Override
    public void getData(final int a, final int b, final NFCCallByteback callback) {
        new Thread(new Runnable() {
            @Override
@@ -375,18 +385,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);
@@ -407,6 +429,55 @@
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        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 "";
@@ -543,6 +614,7 @@
     *
     * @return
     */
    @Override
    public BaseManagerToUserCard getManagerToUserCardData(BaseManagerToUserCard baseManagerToUserCard) {
        BaseManagerToUserCard managerToUserCard = null;
@@ -602,4 +674,8 @@
        }
        return null;
    }
    public boolean changePasword(int a, MifareClassic mfc) {
    }
}