有时候MifareClassic 1k NFC卡身份验证失败,而标记同一张卡使用android

时间:2016-09-28 09:28:26

标签: android nfc

我使用的MicareClassic 1k卡在扇区0块0和1中有数据。数据的格式为 tagdata

我尝试使用以下代码段读取这些块

    String resolveNfcIntent(Intent intent) {
    final StringBuilder out=new StringBuilder();
    String action = intent.getAction();
    String msg = "";
    String cardStudentId="";
    String cardStudentName="";
    String mifareKeyA="first 12 hex chars";

    if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(action)
            || NfcAdapter.ACTION_TECH_DISCOVERED.equals(action)
            || NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)) {
        final Tag tagFromIntent = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
        final byte keyByte[] = Util.hexStringToByteArray(mifareKeyA);
        final Handler mHandler = new Handler();
        new Thread(new Runnable() {
            @Override
            public void run() {
                MifareClassic mfc = MifareClassic.get(tagFromIntent);
                if (mfc == null) {
                    out.append("card not supported\n");
                } else {
                    String msg;
                    try {
                        byte[] data;
                        out.append("Try to connect card\n");
                        mfc.connect();// caused IOException when close called from another thread;
                        out.append("card connected\n");
                        boolean authenticated = false;
                        final int SECTOR_INDEX = 0;
                        out.append("Try to authenticate with key A\n");
                        authenticated = mfc.authenticateSectorWithKeyA(SECTOR_INDEX, keyByte);
                        out.append("isCardAuthorizedWith Key A: " + authenticated + "\n");
                        if (authenticated) {
                            final int BLOCK_FIRST = mfc.sectorToBlock(SECTOR_INDEX);//Return the first block of a given sector.
                            final int BLOCK_ID = BLOCK_FIRST + 1;
                            final int BLOCK_NAME = BLOCK_FIRST + 2;
                            out.append("try to read id block " + BLOCK_ID + " \n");
                            data = mfc.readBlock(BLOCK_ID);//causes TagLostException, IOException
                            String cardStudentId = new String(data, Charset.forName("UTF-8")).trim();
                            out.append("id read: '" + cardStudentId + "'\ntry to read name block " + BLOCK_NAME + "\n");
                            data = mfc.readBlock(BLOCK_NAME);
                            String cardStudentName = new String(data, Charset.forName("UTF-8")).trim();
                            out.append("name read '" + cardStudentName + "'\n");
                        } else { // Authentication failed - Handle it
                            msg = "Nfc card Authentication failed";
                            out.append(msg + "\n");
                            Toast.makeText(MainActivity.this, "Nfc card Authentication failed", Toast.LENGTH_SHORT).show();
                        }
                    } catch (TagLostException e) {
                        msg = "Nfc card leaves before read";
                        out.append(msg + "\n");
                    } catch (IOException e) {
                        msg = "Nfc card IO error, try again later";
                        Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();
                    } catch (NullPointerException e) {
                        StringWriter sw = new StringWriter();
                        PrintWriter pw = new PrintWriter(sw);
                        e.printStackTrace(pw);
                        out.append(sw.toString());
                    } catch (Exception e) {
                        StringWriter sw = new StringWriter();
                        PrintWriter pw = new PrintWriter(sw);
                        e.printStackTrace(pw);
                        out.append(sw.toString());
                    } finally {
                        if (mfc != null) {
                            try {
                                out.append("try to close tag\n");
                                mfc.close();
                                out.append("tag closed\n");
                            } catch (IOException e) {
                                out.append("IOError in closing tag connection\n");
                                Log.e(TAG, "Error closing tag...", e);
                            }
                        }
                    }
                }
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        txt.setText(out.toString());
                    }
                });
            }
        }).start();
    }
    return  out.toString();
}

大多数情况下它运行良好但有时它会在authenticateWithKeyA()函数调用here时显示身份验证失败错误。

我正在挖掘更多,但我找不到确切的问题。请指导我正确的方向。 感谢

0 个答案:

没有答案