Search code examples
androidnfc

How to make NFC tag read-only in Android?


I face a problem to lock NFC tag. My code like bellow:

    public void makeReadOnly(Tag tag) {
    if (tag == null) {
        Log.e("tag", "Tag Is Null");
        return;
    }
    try {

        Ndef ndef = Ndef.get(tag);
        if (ndef != null) {
            Log.e("eeeeeee111111", "New1111");
            ndef.connect();

            if(ndef.canMakeReadOnly()){
                ndef.canMakeReadOnly();
                Log.e("Read Only", "Read Only");
            }
            Log.e("Lock.......", "Lock.......");

        //  ndef.canMakeReadOnly();
            ndef.close();
        //  Log.e("22222.......", "2222.......");

        }



    } catch (IOException e) {
        // TODO Auto-generated catch block
        Log.e("EEEEEEEEEEEEE", e.toString());
        e.printStackTrace();
    }

}

It doesn't show any error at all. But not locking NFC tag.


Solution

  • canMakeReadOnly() simply returns if the tag can be made read-only. Use makeReadOnly() to actually do so (guess it's a simple oversight).

    Replace the following:

    if(ndef.canMakeReadOnly()){
        ndef.canMakeReadOnly();
        Log.e("Read Only", "Read Only");
    }
    

    With this:

    if(ndef.canMakeReadOnly()){
        ndef.makeReadOnly();
        Log.e("Read Only", "Read Only");
    }