简体   繁体   中英

NFC - Writing to a MiFare Classic 1K

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!

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.

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