We can use NFC tags for many things, such as storing tiny amounts of data, typically ranging from a few bytes to a few megabytes, or using it to interact with other devices.
If we want to write to a tag, we need to obtain the tag and create a message to write:
Tag
instance, we need to start listening for NFC tags:var adapter = NfcAdapter.GetDefaultAdapter(this); if (adapter != null && adapter.IsEnabled) { var tagDetected = new IntentFilter( NfcAdapter.ActionTagDiscovered); var pendingIntent = PendingIntent.GetActivity( this, 0, new Intent(this, GetType()), 0); adapter.EnableForegroundDispatch( this, pendingIntent, new[]{ tagDetected }, null); }
OnNewIntent()
method:protected override void OnNewIntent(Intent intent) { var tag = intent.GetParcelableExtra( NfcAdapter.ExtraTag) as Tag; }
Tag
instance from the extras in the OnNewIntent()
method, we can start writing by creating the message. In this case, a custom data tag will be created for our custom MIME type:var payload = Encoding.UTF8.GetBytes("Xamarin Cookbook!"); string mimeType = "application/vnd.xamarincoockbook.nfcrecipe"; var mimeBytes = Encoding.UTF8.GetBytes(mimeType); var record = new NdefRecord( NdefRecord.TnfMimeMedia, mimeBytes, new byte[0], payload); var message = new NdefMessage(new[] { record });
var ndef = Ndef.Get(tag);
await ndef.ConnectAsync();
await ndef.WriteNdefMessageAsync(message);
ndef.Close();
var format = NdefFormatable.Get(tag);
await format.ConnectAsync();
await format.FormatAsync(message);
format.Close();
NFC tags can be read by Android and can perform many actions such as opening apps, changing settings, or starting services. NFC tags require no power as the radio is powered solely by the RF field of the device. Not only do they not require power, but passive NFC tags are tiny, lightweight, and paper thin.
NFC tags can be written using a special format, NDEF messages, or custom formats. Android has the best support for tags written in the NDEF format. NDEF data is encapsulated inside a message, an NdefMessage
instance, which contains one or more records, NdefRecord
. Each record must be formed according to the type of record we want to create.
Each message can have multiple records, but the first record is used to determine how to interpret all the records. Thus, the first record should contain several fields:
Variable Length Type
field, which could be a URI, a custom type, or a well-known type.Once we have created a properly formatted NDEF message, we can write it to the tag. In order to do this, we have to first obtain the Ndef
instance from a tag. The Ndef
instance provides access to NDEF content and operations.
With the Ndef
instance, we can start writing to the tag. Before we start, we have to open a connection. To do this, we invoke the ConnectAsync
method. After the connection is opened, we can write the data to the tag using the WriteNdefMessageAsync
method. When writing has completed, we need to close the connection in order to free up any resources that may have been used. This is done via the Close
method.
If, for some reason, the write fails, it may be due to the fact that the tag itself is incorrectly formatted. If this is the case, we can format the tag with our message. As with writing a tag, we need to obtain the formatter, the NdefFomatable
type, from the tag.
Once we have the formatter, we can perform operations on the tag. As with the write process, we have to connect to the tag using ConnectAsync
. Then, instead of writing, we format the tag via the FormatAsync
method. Once the format has completed, we must close the connection to the tag with the Close
method.
If our app is targeted at Android version 4.0 and above, we can write tags to launch other apps. To do this, we write a special type of NdefRecord
instance to the tag, an Android Application Record:
NdefRecord.CreateApplicationRecord("com.xamarincookbook.example");
When a tag is scanned with this record, Android will try and launch the app. If the app is not installed, Android will open the Play Store at the app's page.