user4696837
user4696837

Reputation:

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.

Upvotes: 0

Views: 1130

Answers (1)

earthw0rmjim
earthw0rmjim

Reputation: 19417

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");
}

Upvotes: 1

Related Questions