Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/202.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
有时MifareClassic 1k NFC卡身份验证在使用android标记同一张卡时失败_Android_Nfc - Fatal编程技术网

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

有时MifareClassic 1k NFC卡身份验证在使用android标记同一张卡时失败,android,nfc,Android,Nfc,我使用MicareClassic 1k卡,该卡的数据位于扇区0块0和1中。数据格式为 我试着用下面的代码段来读取这些块 String resolveNfcIntent(Intent intent) { final StringBuilder out=new StringBuilder(); String action = intent.getAction(); String msg = ""; String cardStudentId=""; S

我使用MicareClassic 1k卡,该卡的数据位于扇区0块0和1中。数据格式为

我试着用下面的代码段来读取这些块

    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()函数时,有时会显示身份验证失败错误

我挖得更多,但我找不到确切的问题是什么。请给我指引正确的方向。 谢谢