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:
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); }
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; }
Once we have obtained the
Tag
instance...