Skip to content Skip to sidebar Skip to footer

How To Stop Overwrite To An Nfc Tag Using Android

I'm using PN512 in card emulation mode as a NFC tag and am developing an Android application to read and write into the tag. The problem is, as long as the tag is in the NFC field,

Solution 1:

After you write in the tag you can do this but keep in mind that this process is not reversible.

    Ndef ndef = Ndef.get(tag);  
    if (ndef != null) { 
        ndef.makeReadOnly();
    }

Solution 2:

Yes, it is possible to make an activity read and write into tag only once. I solved my problem by declaring a flag as a global variable and used the flag to control my tag reading and writing.

IN your class declare a flag as Boolean. suppose :

publicclassYourClass{
Boolean flag= false;
.
.
.
.
void resolveIntent(Intent intent)
{
    NdefMessage msg1, msg2;
// Parse the intentString action = intent.getAction();
if(flag== false){
flag = true;
    if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(action) ||
        NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)) {

        try
        {
            mRecord=createRecord();
        }
        catch (Exception e)
        {
            Toast toaste = Toast.makeText(getApplicationContext(), 
"exception in call to create record", Toast.LENGTH_SHORT);
            toaste.show();
        }

        // When a tag is discovered we send it to the service to be save. We// include a PendingIntent for the service to call back onto. This// will cause this activity to be restarted with onNewIntent(). At// that time we read it from the database and view it.
       Parcelable[] rawMsgs = 
intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
       NdefMessage[] msgs;

       if (rawMsgs != null)
       {
            msgs = new NdefMessage[rawMsgs.length];
            for (int i = 0; i < rawMsgs.length; i++)
            {
                msgs[i] = (NdefMessage) rawMsgs[i];
            }

            msg1 = msgs[0];
            myPayload=msg1.getRecords()[0].getPayload();
        } 
        else {
            // Unknown tag type
            byte[] empty = new byte[] {};
            NdefRecord record = new NdefRecord(NdefRecord.TNF_UNKNOWN, 
empty, empty, empty);
            msg2 = new NdefMessage(new NdefRecord[] {record});
            msgs = new NdefMessage[] {msg2};
        }
        writeToNfcTag(intent);
       }
    }
.
.
.
.
}

Hope it helps.

Post a Comment for "How To Stop Overwrite To An Nfc Tag Using Android"