Chunks of data can be transferred from one device to another with NFC, using the same messages used when writing to tags.
If we want to send messages from a device, we need to register a message with the NFC push system. We could create a custom message, as in the previous recipe, but here we will just send an HTTP URI to another device:
byte httpType = 0x01; // 'http://www.' var theUri = Encoding.UTF8.GetBytes("xamarin.com"); var payload = new byte[theUri.Length + 1]; payload[0] = httpType; Array.Copy(theUri, 0, payload, 1, theUri.Length);
var record = new NdefRecord( NdefRecord.TnfWellKnown, NdefRecord.RtdUri.ToArray(), new byte[0], payload); var message = new NdefMessage(new[] { record });
var uri = "http://www.xamarin.com"; var record = NdefRecord.CreateUri(uri); var message = new NdefMessage(new[] { record });
Now that we have the message, we need to let Android know that it needs to send it:
adapter.EnableForegroundNdefPush(this, message);
adapter.DisableForegroundNdefPush(this);
adapter.SetNdefPushMessage(message, this);
NfcAdapter.ICreateNdefMessageCallback
interface on our activity:public class MainActivity : Activity, NfcAdapter.ICreateNdefMessageCallback { public NdefMessage CreateNdefMessage(NfcEvent e) { var message = ... return message; } }
adapter.SetNdefPushMessageCallback(this, this);
NfcAdapter.IOnNdefPushCompleteCallback
interface:public class MainActivity : Activity, NfcAdapter.ICreateNdefMessageCallback { public void OnNdefPushComplete(NfcEvent e) { // message sent } }
adapter.SetOnNdefPushCompleteCallback(this, this);
Sending data to another device using NFC is a two-step process: first, we create the message, and then we register the message with the NFC adapter. This very simply makes the device an NFC tag that can be read by another device.
The way we register a message was changed in Android version 4.0 from a manual process to one that is more automatic. If we are targeting Android version 2.3, we need to register the message with the NFC adapter by using the EnableForegroundNdefPush()
method. If we are targeting Android version 4.0 and above, we use the SetNdefPushMessage()
method.
Messages can only be sent from an app when the app is in the foreground. If the app is paused, the message is removed (manually for Android version 2.3 and below and automatically for Android version 4.0 and above). To manually remove the message on Android version 2.3, we use the DisableForegroundNdefPush()
method.
Android version 4.0 also introduces several other enhancements in the form of callbacks. If we want our message to be created at the time the data is to be transferred, we register a type that implements the NfcAdapter.ICreateNdefMessageCallback
interface with the adapter. This is done instead of using the SetNdefPushMessage()
method. When the message is needed, the CreateNdefMessage()
method is invoked and we return the message we want to transmit to the other device.
There is also a callback for when the message has been transferred. Similarly, we register a type that implements the NfcAdapter.IOnNdefPushCompleteCallback
interface using the SetOnNdefPushCompleteCallback
method. When the message has been sent, the OnNdefPushComplete()
method is invoked.
For both callbacks, we can just implement the interfaces on the actual activity. This allows us to easily handle NFC operations.