Transferring data via NFC

Chunks of data can be transferred from one device to another with NFC, using the same messages used when writing to tags.

How to do it...

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:

  1. If we are targeting Android version 2.3 and above, we need to manually create the correct NDEF message payload:
    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);
  2. Once we have the payload, we create the message:
    var record = new NdefRecord(
      NdefRecord.TnfWellKnown, 
      NdefRecord.RtdUri.ToArray(), 
      new byte[0],
      payload);
    var message = new NdefMessage(new[] { record });
  3. If we are targeting Android version 4.0 and above, it is far easier to create the 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:

  1. If we are targeting Android version 2.3 and above, we have to register the message with the NFC adapter when the app is in the foreground:
    adapter.EnableForegroundNdefPush(this, message);
  2. When the app leaves the foreground, we need to disable it:
    adapter.DisableForegroundNdefPush(this);
  3. Android version 4.0 and above does things a bit differently. We set the message once and let Android take over:
    adapter.SetNdefPushMessage(message, this);
  4. If we want the message to be more dynamic, we can create the message on the fly using the NfcAdapter.ICreateNdefMessageCallback interface on our activity:
    public class MainActivity : Activity, 
      NfcAdapter.ICreateNdefMessageCallback
    {
      public NdefMessage CreateNdefMessage(NfcEvent e)
      {
        var message = ...
        return message;
      }
    }
  5. We then register this with the adapter as well:
    adapter.SetNdefPushMessageCallback(this, this);
  6. We can also be notified when the app has sent a message through the NfcAdapter.IOnNdefPushCompleteCallback interface:
    public class MainActivity : Activity, 
      NfcAdapter.ICreateNdefMessageCallback
    {
      public void OnNdefPushComplete(NfcEvent e)
      {
        // message sent
      }
    }
  7. Finally, we register this with the adapter:
    adapter.SetOnNdefPushCompleteCallback(this, this);

How it works...

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.

..................Content has been hidden....................

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