简体   繁体   中英

Write on a MifareClassic card with our own android application

I am currently trying to develop an android application which will write a smal text on a Mifare card. I tried to program an application but the code doest't work. Someone knows what do I have to write on the Android Manifest? Here is my code, if somebody could help me it would be very kind. Thank you very much

public static final Tag CREATOR = null;

public void ecrirenfc(Tag tag) {

    MifareClassic mfc = MifareClassic.get(tag);

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

        auth = mfc.authenticateSectorWithKeyA(1, MifareClassic.KEY_DEFAULT);

        if (auth) {
            String text = "Hello, World!";
            byte[] value = text.getBytes();
            byte[] toWrite = new byte[MifareClassic.BLOCK_SIZE];

            for (int i = 0; i < MifareClassic.BLOCK_SIZE; i++) {
                if (i < value.length)
                    toWrite[i] = value[i];
                else
                    toWrite[i] = 0;
            }

            mfc.writeBlock(1, toWrite);
        }

    } catch (IOException e) {
    }
}

public void onClick(View view) {
    if (view.getId() == R.id.button1) {
        ecrirenfc(CREATOR);
    }
}

You need to use the

sectorToBlock(int sectorIndex)

method to determine the first block of the sector. Then use

getBlockCountInSector(int sectorIndex)

to determine how many blocks (-1) you can write to in that sector.

For your above code, you are trying to write to sector 0 when you have authenticated sector 1.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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