17. NDEF Tag - Reading public class MyNFCActivity extends Activity { @ Override public void onNewIntent ( Intent intent ) { Parcelable [] raw = intent . getParcelableArrayExtra ( NfcAdapter . EXTRA_NDEF_MESSAGES ) ; NdefMessage message = ( NdefMessage ) raw [ 0 ] ; NdefRecord record = message . getRecords ()[ 0 ] ; byte [] payload = record . getPayload () ; ... } }
18. NDEF Tag - Writing public class MyNFCActivity extends Activity { @ Override public void onNewIntent ( Intent intent ) { Tag t = intent . getParcelableExtra ( NfcAdapter . EXTRA_TAG ) ; Ndef ndef = Ndef . get ( t ) ; ndef . connect () ; final byte [] data = … ; NdefRecord record = new NdefRecord ( NdefRecord . TNF_MIME_MEDIA , "text/plain" . getBytes (), new byte [ 0 ], data ) ; NdefRecord [] records = new NdefRecord [] { record }; NdefMessage message = new NdefMessage ( records ) ; ndef . writeNdefMessage ( message ) ; } }
19.
20.
21.
22.
23.
Editor's Notes
corto raggio per i pagamenti e la sicurezza
lento rispetto a bluetooth
differenza principale tra NFC e RFID è NFC permette di fare una comunicazione bidirezionale.
tag nfc è diverso da rfid
The intent dispatch system is to launch a program from scratch. For instance, you turn your phone on and hover it over a tag. The phone launches an intent, identifying it as a Mifare card. Your app has an intent dispatch for Mifare cards, so it is one of the apps that can be selected. If your intent dispatch is specific enough(compared to other apps), it will be the only app to select, and will run at that point. Foreground dispatch is used by your app while it is running. This way, your app won't be turned off if another app has a similar intent dispatch setup. Now with your app running, it can discover a tag and handle the intent over other apps on the phone, and the phone will not prompt you to select from a bunch of apps that have a similar intent dispatch.
API level 9 only supports limited tag dispatch via ACTION_TAG_DISCOVERED , and only gives access to NDEF messages via the EXTRA_NDEF_MESSAGES extra. No other tag properties or I/O operations are accessible. API level 10 includes comprehensive reader/writer support as well as foreground NDEF pushing, and API level 14 provides an easier way to push NDEF messages to other devices with Android Beam and extra convenience methods to create NDEF records.
3-bit TNF (Type Name Format) field: Indicates how to interpret the type field EXTRA_TAG (required): A Tag object representing the scanned tag. EXTRA_NDEF_MESSAGES (optional): An array of NDEF messages parsed from the tag. This extra is mandatory on intents.