Writing NFC tags

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.

How to do it...

If we want to write to a tag, we need to obtain the tag and create a message to write:

  1. In order to write to a 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);
    }
  2. Once the listener has started, we can wait for the tag to be discovered and passed to the OnNewIntent() method:
    protected override void OnNewIntent(Intent intent) {
      var tag = intent.GetParcelableExtra(
        NfcAdapter.ExtraTag) as Tag;
    }
  3. Once we have obtained the 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 });
  4. Now that we have our message, we can attempt to write to the tag:
    var ndef = Ndef.Get(tag);
    await ndef.ConnectAsync();
    await ndef.WriteNdefMessageAsync(message);
    ndef.Close();
  5. If this fails for some reason, it could be that the tag is not formatted, so we can try formatting the tag with the message:
    var format = NdefFormatable.Get(tag);
    await format.ConnectAsync();
    await format.FormatAsync(message);
    format.Close();

How it works...

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.

Tip

The same NDEF messages used to write to a tag can be used to send data to another device.

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:

  • Type Name Format (TNF), which describes how to interpret the Variable Length Type field, which could be a URI, a custom type, or a well-known type.
  • Type, which describes the type of record. If the tag is marked as a well-known type, this field is used to specify the Record Type Definition (RTD) such as URI or text.
  • ID, which provides a unique identifier for the tag itself.
  • Payload, which contains the actual data of the tag.

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.

Note

Regardless of whether the tag is being written to or formatted, the connection needs to be opened before operations are performed and then closed after operations have completed.

There's more...

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.

See also

  • The Receiving NFC events recipe
  • The Transferring data via NFC recipe
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset