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

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.