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 ...

Get Xamarin Mobile Development for Android Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.