简体   繁体   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

when i am trying to write a data in Mifare classic 1k card, on Nexus S with android 2.3.3 API level 10, I am having this error. 当我尝试在具有Android 2.3.3 API级别10的Nexus S上的Mifare classic 1k卡中写入数据时,出现此错误。 transceive failed. 收发失败。 my code is 我的代码是

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");
        }
    }
};

any pointers 任何指针

It seems like you're trying to directly write NDEF data onto the low-level blocks of the mifare card. 似乎您正在尝试将NDEF数据直接写入到mifare卡的低级块中。

This won't work for two reasons: 这不能工作有两个原因:

  1. Before you write to mifare blocks you have to authenticate yourself for write access using the authenticateSectorWithKeyA or authenticateSectorWithKeyB function. 在写入mifare块之前,您必须使用authenticateSectorWithKeyA或authenticateSectorWithKeyB函数对自己进行写访问身份验证。 The keys for NDEF formatted mifare cards are available in the MifareClassic class. NDEF格式的mifare卡的密钥在MifareClassic类中可用。

  2. You cannot directly write NDEF data onto the card and expect that other applications are able to interpret the data. 您不能直接将NDEF数据写入卡并期望其他应用程序能够解释该数据。 NDEF is a user format. NDEF是一种用户格式。 The data that gets written to the card contains extra data such as application directories, size information etc. 写入卡的数据包含其他数据,例如应用程序目录,大小信息等。

I suggest that you use the NDEF write functions of the generic Tag interface. 我建议您使用通用Tag接口的NDEF写入功能。 These will do all the extra formatting for you. 这些将为您完成所有额外的格式化。

If you want to experiment with low-level writing to the card I suggest that you first download the mifare datasheet and read what sectors and blocks are. 如果要尝试对卡进行低级写入,我建议您首先下载mifare数据表,然后阅读什么扇区和块。 Also it is important that you understand the authentication and security concept of the chip. 了解芯片的身份验证和安全概念也很重要。 Otherwise it is all to easy to accidentally overwrite the authentication blocks and destroy the tag. 否则,很容易意外覆盖身份验证块并破坏标签。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM