【问题标题】:Mifare Classic 1k Card write Failed with Error java.ioexception transceive failed in Android Nexus SMifare Classic 1k Card write Failed with Error java.ioexception transceive failed in Android Nexus S
【发布时间】:2011-07-31 09:12:48
【问题描述】:

当我尝试在具有 android 2.3.3 API 级别 10 的 Nexus S 上的 Mifare 经典 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");
        }
    }
};

任何指针

【问题讨论】:

    标签: android nfc nexus-s


    【解决方案1】:

    您似乎正试图将 NDEF 数据直接写入 mifare 卡的低级块。

    这不起作用有两个原因:

    1. 在写入 mifare 块之前,您必须使用 authenticateSectorWithKeyA 或 authenticateSectorWithKeyB 函数验证自己的写入权限。 NDEF 格式的 mifare 卡的密钥在 MifareClassic 类中可用。

    2. 您不能直接将 NDEF 数据写入卡并期望其他应用程序能够解释数据。 NDEF 是一种用户格式。写入卡的数据包含额外的数据,例如应用程序目录、大小信息等。

    我建议你使用通用 Tag 接口的 NDEF 写函数。这些将为您完成所有额外的格式化。

    如果您想尝试对卡进行低级写入,我建议您首先下载 mifare 数据表并阅读哪些扇区和块。此外,了解芯片的身份验证和安全概念也很重要。否则很容易不小心覆盖验证块并破坏标签。

    【讨论】:

    • 感谢指点,如果我的理解有误请指正,所以我必须使用 NDEF 写入和读取函数对 MIFare Classic 1k 卡进行 IO 操作?
    • 我能够使用默认密钥对扇区进行身份验证,现在我面临一个问题,出现空指针异常,在调试时我发现 ndef.get(tag) 返回空值。你能指导我吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-11-06
    • 1970-01-01
    • 1970-01-01
    • 2021-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多