NFC标签阅读问题

时间:2011-09-21 04:12:40

标签: android authentication nfc

这是我阅读NFC标签的代码。为什么身份验证失败?它正在检测卡但不读取数据。请你帮助我好吗?为什么if块没有执行?我哪里错了?

void resolveIntent(Intent intent)
{ 
String action = intent.getAction();

if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(action))
{ 
    Tag tagFromIntent = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);

    MifareClassic mfc = MifareClassic.get(tagFromIntent); 
    byte[] data;

    try
    {    
        mfc.connect();
        boolean auth = false;
        String cardData = "";

        int sectorCount = mfc.getSectorCount();
        int blockCount = 0;
        int blockIndex = 0;
        for(int j = 0; j < sectorCount; j++)
        { 
            auth = mfc.authenticateSectorWithKeyA(j, MifareClassic.KEY_DEFAULT);
            if(auth)
            {

                blockCount = mfc.getBlockCountInSector(j);
                blockIndex = 0;
                for(int i = 0; i < blockCount; i++)
                {
                    blockIndex = mfc.sectorToBlock(j);

                    data = mfc.readBlock(blockIndex);    

                    cardData = cardData + getHexString(data, data.length);
                    blockIndex++;
                }
            }

            else
            { 
                // Authentication failed - Handle it
                showAlert(AUTH); //this alert message is executing always
            }
        } 
        Toast.makeText(getApplicationContext(), cardData, Toast.LENGTH_LONG).show();
    }
    catch (IOException e)
    { 
        Log.e(TAG, e.getLocalizedMessage());
        showAlert(NETWORK);
    }
   }//end of if
}// End of method

2 个答案:

答案 0 :(得分:3)

由于它不是新标记,并且已由其他应用程序编写,因此我怀疑身份验证密钥已更改。您正在使用默认密钥,但其他应用程序可能已更改它们。较旧的诺基亚手机一直都是这样做的。在这种情况下,您需要弄清楚keyA的新密钥是什么,而不是使用MifareClasic.KEY_DEFAULT

答案 1 :(得分:0)

尝试使用MifareClassic.KEY_NFC_FORUM作为keyA

相关问题