簡體   English   中英

Mifare Classic 1k卡寫入失敗,並出現錯誤java.ioexception收發在Android Nexus S中失敗

[英]Mifare Classic 1k Card write Failed with Error java.ioexception transceive failed in Android Nexus S

當我嘗試在具有Android 2.3.3 API級別10的Nexus S上的Mifare classic 1k卡中寫入數據時,出現此錯誤。 收發失敗。 我的代碼是

private View.OnClickListener write_butClickListener =new View.OnClickListener() {

    @Override
    public void onClick(View v) {


        IntentFilter ndef=new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED );
        try{
            ndef.addDataType("*/*");
        }catch(MalformedMimeTypeException e){
            throw new RuntimeException("fail",e);
        }

        mFilters=new IntentFilter[]{ndef,};
        // Setup a tech list for all NfcF tags
        mTechLists = new String[][] { new String[] { MifareClassic.class
                .getName() } };

        Intent intent=getIntent();
        String action=intent.getAction();
        if(NfcAdapter.ACTION_TECH_DISCOVERED.equals(action) ){
        String msg="Discovered Tag with Intent " + intent;
        status_Data.setText(msg);
        Tag tagFromintent=intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);

        MifareClassic mfc=MifareClassic.get(tagFromintent);
        byte[] data;
        try{
            mfc.connect();
            boolean auth = false;
            String cardData = null;
            status_Data.setText("Authenticating the Tag..");

            auth = mfc.authenticateSectorWithKeyA(1,
                    MifareClassic.KEY_DEFAULT);
            if (auth){
                status_Data.setText("Authenticated");

                 String text       = "Hello, World!";
                    String lang       = "en";
                    byte[] textBytes  = text.getBytes();
                    byte[] langBytes  = lang.getBytes("US-ASCII");
                    int    langLength = langBytes.length;
                    int    textLength = textBytes.length;
                    byte[] payload    = new byte[1 + langLength + textLength];

                    // set status byte (see NDEF spec for actual bits)
                    payload[0] = (byte) langLength;

                    // copy langbytes and textbytes into payload
                    System.arraycopy(langBytes, 0, payload, 1,              langLength);
                    System.arraycopy(textBytes, 0, payload, 1 + langLength, textLength);
                    if (!mfc.isConnected()) mfc.connect();

                                    mfc.writeBlock(1, payload);
                                    mfc.close();
                showMessage("written");
            }else{
                showMessage("Authetication Failed");
                status_Data.setText("");
            }

        }
        catch(IOException e){
            status_Data.setText(e.toString());

        }



        }else{
            showMessage("Nothing to read");
        }
    }
};

任何指針

似乎您正在嘗試將NDEF數據直接寫入到mifare卡的低級塊中。

這不能工作有兩個原因:

  1. 在寫入mifare塊之前,您必須使用authenticateSectorWithKeyA或authenticateSectorWithKeyB函數對自己進行寫訪問身份驗證。 NDEF格式的mifare卡的密鑰在MifareClassic類中可用。

  2. 您不能直接將NDEF數據寫入卡並期望其他應用程序能夠解釋該數據。 NDEF是一種用戶格式。 寫入卡的數據包含其他數據,例如應用程序目錄,大小信息等。

我建議您使用通用Tag接口的NDEF寫入功能。 這些將為您完成所有額外的格式化。

如果要嘗試對卡進行低級寫入,我建議您首先下載mifare數據表,然后閱讀什么扇區和塊。 了解芯片的身份驗證和安全概念也很重要。 否則,很容易意外覆蓋身份驗證塊並破壞標簽。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM