The Nexus S can write to NFC tags. How do you stop anyone with a Nexus S from overwriting your tag?
eg. If you have an NFC tag on a flyer that launches a video, can a guy with a Nexus S overwrite it and replace it with a link to his website?
Beginner here, just trying to get a handle on NFC, thanks!
Yes, many tag types permit write protection, some globally, and other more sophisticated ones by memory section inside the tag.
When you get your tag detected in your Android NFC app, then you can get a Tag object and read and write to the tag, including protecting it from further writing.
This is all described in the developer documentation for NFC classes on the Android developer site.
For example, if you are working at higher level and reading/writing Ndef
messages:
Setup an intent filter (in code or manifest) so you detect Ndef
or NDefFormatable
tags. You can get a Tag
object from the Intent
passed to you (in onCreate()
or onNewIntent()
).
Tag tag = intent.getParcelableExtra( NfcAdapter.EXTRA_TAG );
From your Tag
try to get an Ndef
tag object from the intent.
If successful in getting an Ndef
object from the tag, then check it is writable, check it has enough space, write to it and then use the makeReadOnly()
of Ndef
....
If getting an Ndef
object failed (exception) then you will need to format the Tag first....so...
get an NdefFormattable
object from the detected Tag
and write a message to it and protect it, using the formatReadOnly(NdefMessage firstMessage)
, or just format it and then continue with code to get Ndef
...
If you can't get an NdefFormattable
object then something is seriously wrong, as that was what you requested in your filter. Formatting might fail if it is read-only already.
The developer documentation is not too bad, once you get your head around setting the IntentFilter
by "Tag Technology" (including NdefFormatable
and Ndef
) and then getting those objects from the base Tag to do different operations.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With