NFC - Writing to a MiFare Classic 1K

10,226

Solution 1

There is example code availble at http://nearfieldcommunication.com/developers/android/

Solution 2

If you have an intent of a NFC discovering, you can use this snippet:

private void WriteCard(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);
        try {
            mfc.connect();
            boolean authA = mfc.authenticateSectorWithKeyA(1,
                    MifareClassic.KEY_DEFAULT);
            Log.d("MainActivity.WriteCard()", String.valueOf(authA) + " ");
            mfc.writeBlock(mfc.sectorToBlock(1), new byte[] { 'A', 'l','v', 'a', 'r', 'e', 'z', ' ', ' ', ' ', ' ', ' ', ' ',' ', ' ', ' ' });
            mfc.close();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return;
}

At this example I'm writting at Sector 1 Block 0. Be sure your trying to write in an "valid" sector with the appropiate key.

Share:
10,226
Eran Tal
Author by

Eran Tal

Updated on November 21, 2022

Comments

  • Eran Tal
    Eran Tal over 1 year

    I want to write data to a mifare classic 1K tags. does anyone have a working sample code to do that? I can't find enough information on that on the web. Thanks!

    • Nils Pipenbrinck
      Nils Pipenbrinck over 12 years
      what reader/hardware/os/library are you using?
    • Eran Tal
      Eran Tal over 12 years
      sorry. i forgot to mention. i am talking about android ( java) using Nexus S as the NFC device for reading the tags. Can you help?