chariotsolutions / phonegap-nfc

PhoneGap NFC Plugin
MIT License
710 stars 566 forks source link

Add function to make tags readonly #6

Closed don closed 9 years ago

don commented 13 years ago

Ndef.makeReadOnly() and possibly NdefFormatable.formatReadOnly(message)

navigator.nfc.makeReadOnly()

// consider options object for writeTag navigator.nfc.writeTag(ndefMessage, win, lose, { read-only: true })

Skielex commented 11 years ago

Is this something that is being worked on?

don commented 11 years ago

Not actively. Do you need it? Which platform?

Skielex commented 11 years ago

I'm working on a project in which we're considering using PhoneGap and this NFC plug-in as a part of the solution. We want to be able to lock tags. Right now Android seems like the platform to go with since we don't have any interest in Blackberry, WP8 doesn't support locking tags and iOS doesn't even support NFC.

Make read-only support for Android could very easily be added (see code below). However, is seems like the plug-in code base might be undergoing some big changes. Thus I'm not sure if you'd like me to try and commit these addition.

NfcPlugin.java

    private void eraseTag(CallbackContext callbackContext) throws JSONException {
        Tag tag = savedIntent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
        NdefRecord[] records = {
            new NdefRecord(NdefRecord.TNF_EMPTY, new byte[0], new byte[0], new byte[0])
        };
        writeNdefMessage(new NdefMessage(records), tag, callbackContext, false);
    }

    private void writeTag(JSONArray data, CallbackContext callbackContext) throws JSONException {
        if (getIntent() == null) {  // TODO remove this and handle LostTag
            callbackContext.error("Failed to write tag, received null intent");
        }

        Tag tag = savedIntent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
        NdefRecord[] records = Util.jsonToNdefRecords(data.getString(0));
        boolean makeRealOnly = data.optBoolean(1);
        writeNdefMessage(new NdefMessage(records), tag, callbackContext, makeRealOnly);
    }

    private void writeNdefMessage(final NdefMessage message, final Tag tag, final CallbackContext callbackContext, final boolean makeReadOnly) {
        cordova.getThreadPool().execute(new Runnable() {
            @Override
            public void run() {
                try {
                    Ndef ndef = Ndef.get(tag);
                    if (ndef != null) {
                        ndef.connect();

                        if (ndef.isWritable()) {
                            int size = message.toByteArray().length;
                            if (ndef.getMaxSize() < size) {
                                callbackContext.error("Tag capacity is " + ndef.getMaxSize() +
                                        " bytes, message is " + size + " bytes.");
                            } else {        
                                ndef.writeNdefMessage(message);
                                if(makeReadOnly) {
                                    ndef.makeReadOnly(); // canMakeReadOnly() returns false for I-CODE tags even though makeReadOnly works fine
                                }
                                callbackContext.success();
                            }
                        } else {
                            callbackContext.error("Tag is read only");
                        }
                        ndef.close();
                    } else {
                        NdefFormatable formatable = NdefFormatable.get(tag);
                        if (formatable != null) {
                            formatable.connect();
                            if(makeReadOnly) {
                                formatable.formatReadOnly(message);
                            } else {
                                formatable.format(message);
                            }
                            callbackContext.success();
                            formatable.close();
                        } else {
                            callbackContext.error("Tag doesn't support NDEF");
                        }
                    }
                } catch (FormatException e) {
                    callbackContext.error(e.getMessage());
                } catch (TagLostException e) {
                    callbackContext.error(e.getMessage());
                } catch (IOException e) {
                    callbackContext.error(e.getMessage());
                }
            }
        });
    }

phonegap-nfc.js

    write: function (ndefMessage, win, fail, makeReadOnly) {
        cordova.exec(win, fail, "NfcPlugin", "writeTag", [ndefMessage, makeReadOnly]);
    },
ashish447 commented 10 years ago

My application i is attendance system i want to nfc tag read -write protected for me and read proteted for other how it implement tell me

JohnMcLear commented 10 years ago

@ashish447 this isn't available right now, you can attempt to add it yourself by editing the Nfcplugin.java file with the code above but if I was you I'd wait for @don to implement it. Alternatively you can make the chagnes to teh NfcPlugin.java file and open a pull request with your changes in.

ashish447 commented 10 years ago

JohnMcLear thanks .u know about Rfid card tag i have nexsus 7 device but it can't read Rfid Card jaycon_systems_white_rfid_card_125khz

what specification of rfid tag

mcotter commented 10 years ago

Hi,

Is there any time line for this to be added in? Would be very useful for a project we are looking to start.

Cheers Mark

ashish447 commented 10 years ago

what You want to say ?

On Wed, Dec 4, 2013 at 8:08 PM, mcotter notifications@github.com wrote:

Hi,

Is there any time line for this to be added in? Would be very useful for a project we are looking to start.

Cheers Mark

— Reply to this email directly or view it on GitHubhttps://github.com/chariotsolutions/phonegap-nfc/issues/6#issuecomment-29809347 .

ashish447 commented 10 years ago

You are hiring Project

On Wed, Dec 4, 2013 at 10:07 PM, Ashish Kasodariya < ashish.kasodariya@gmail.com> wrote:

what You want to say ?

On Wed, Dec 4, 2013 at 8:08 PM, mcotter notifications@github.com wrote:

Hi,

Is there any time line for this to be added in? Would be very useful for a project we are looking to start.

Cheers Mark

— Reply to this email directly or view it on GitHubhttps://github.com/chariotsolutions/phonegap-nfc/issues/6#issuecomment-29809347 .

mcotter commented 10 years ago

@ashish447 Just wondering if the functionality is going to be added into the plugin?

don commented 10 years ago

I'll try to get this in the next round of fixes.

mcotter commented 10 years ago

@don that would be great. If there is anything I can do to help let me know and I will do my best. cheers

don commented 10 years ago

http://developer.android.com/reference/android/nfc/tech/Ndef.html#makeReadOnly()

maran001 commented 10 years ago

@don, i am working on a project where i have to write the data to the tag and then if someone tries to read the tag, user has to provide the pin. Reading and writing the tag is not an issue, issue is if someone want to read the tag after first time write, pin should be required. Do you have any suggestions how i can achieve this.

maran001 commented 10 years ago

@don, i also tried to encrypt the payload using my unique AES key and decrypt using the same key to protect my payload.

zhimeng9 commented 9 years ago

hi don, do we have an API to set password to protect the NFC tag, such as the function which is python version http://nfcpy.org/latest/modules/tag.html#nfc.tag.Tag.protect ?