Introducing Charming NFC Share

In my talk this week I demonstrated a simple library for sharing a single Uri over NFC. I’ve now completed packaging it up and it is available on NuGet – https://www.nuget.org/packages/InTheHand.Phone.Nfc/

This is a very simple component to use in your app requiring just three lines of code to call. It requires the Windows Phone Toolkit from which it uses page transitions, if you don’t already have this in your project NuGet will add it for you. It also requires you to add the Proximity capability in your app metadata. The Windows Phone Emulator exposes a null NFC device so it is possible to interact with this feature on the emulator (it just won’t send anything). The look and feel of the task has been designed to match the platform so it will look instantly familiar and supports both the light and dark themes.

To share a Uri from your app you’ll need to execute the following:-

Add the InTheHand.Phone.Nfc package via NuGet

Add the ID_CAP_PROXIMITY capability to your WMAppManifest.xml

Add the following code when you want to share a Uri:-

InTheHand.Phone.Tasks.ShareNfcLinkTask snlt = new InTheHand.Phone.Tasks.ShareNfcLinkTask();

snlt.LinkUri = new Uri(“http://peterfoot.net“);

snlt.Show();


Obviously setting the LinkUri to whatever URI you want to share. This could be a web link or a custom URI which performs some task in your app.

At the moment the resources in the library are not localised beyond English although I hope to add this soon.

This is the first in a set of related “Charming” libraries for Windows Phone. I’ll be adding more to NuGet shortly. Hopefully the reason for the name will become clearer too…

Advertisements

NFC Text tags on WinRT

To follow the last post I thought I would quickly round it off by looking at the code required to listen for tags on a Windows 8/RT device. Devices with NFC built in are like hens teeth but one such RT device exists in the Asus VivoTab RT. Because it’s an RT device that means running the Remote Debugger and doing the development on a separate Windows 8 PC. The problem with the Windows 8/RT experience is it is not as set in stone as Windows Phone and so issues like this occur (http://social.msdn.microsoft.com/Forums/en-US/tailoringappsfordevices/thread/9da68387-e0cb-4e02-ac71-bad61f6ff7c6) where some devices don’t correctly advertise support for all tag types. The good news with the Asus is that it does support subscribing to NDEF messages and you can use the same code as in my previous Windows Phone example to read a Text tag. The same Ndef library is used as this supports both Windows 8/RT and Windows Phone 8.

One thing which is a little frustrating when remote debugging is that you have to register for a developer license on the tablet before the debug session will continue. So even though you have a license on your development machine you also need one on the tablet (despite the fact you can’t actually compile an app on it). There doesn’t seem to be a limit to how many devices you can register for a license using your Microsoft ID so this shouldn’t become a problem. You will find that you have to renew it regularly though…

In a future post we will delve into other tag types…

NFC – Simple text tags on Windows Phone

This article follows on from the last post on NFC to dig a little deeper on Windows Phone and Windows 8 (both of which share the same APIs for NFC).

Requirement or just Capability

In Windows Phone you have to add the ID_CAP_PROXIMITY capability to your app to use NFC. What about the ID_REQ_NFC requirement? Well the difference here is that when you specify a requirement the app will only be made available to devices with that feature. Whether you use the requirement therefore depends on whether NFC is core to your application or is an optional feature. This means it is valid to submit an app with NFC capabilities to devices which don’t have the hardware. Therefore you must always check first if

ProximityDevice.GetDefault() == null

Types of NFC Message

So you’ve got as far as calling the SubscribeForMessage API to listen for NFC messages, if you check the help file for this function you’ll see it refers to the partner method “PublishBinaryMessage” to document the message types supported. Then you look down the list of supported protocols and your heart sinks! It’s a long list and there seems to be a lot of duplication – why when we have the NDEF standards are there Windows versions too? Which ones can I use across different platforms? Not to mention the fact that you can’t subscribe to all of these message types on Windows Phone so the utopia of a shared NFC API across both Windows APIs is shattered.

The good news is that the Windows message types are actually helpers over the top of NDEF types and are designed to simplify reading those message types, we’ll look into those on Windows 8/RT at a later stage. There is a very good NDEF library on CodePlex (and NuGet) to parse standard NDEF records. For example the way to read a plain text NDEF.Text message is actually using the NDEF message type because the API doesn’t provide a subtype specifically for Text tags. When the tag is tapped you’ll receive a message payload which you can parse with the NdefLibrary.Ndef.NdefMessage class.

private void MessageReceived(ProximityDevice device, ProximityMessage message) 
{
NdefLibrary.Ndef.NdefMessage.FromByteArray(message.Data.ToArray());

string s = System.Text.Encoding.UTF8.GetString(msg[0].Payload, 0, msg[0].Payload.Length).Substring(2);
}

The final thing you’ll notice from the received text is that it contains the two-character language code at the beginning. Note that there is no error checking in the above sample and it assumes that the text tag is the first record in the message. You can see from the NdefMessage class that it contains a collection of records which can be of a number of specific types. More to follow as we delve deeper…

Cross Platform NFC

NFC support in Windows Phone provides an exciting new way to add interactivity to apps and games. This series of posts will look at the state of NFC across the Windows and Android platforms. There are a number of standard Tag types defined by the NDEF format, for the sake of simplicity we’ll ignore the fact that Android can read other tag types and just concentrate on NDEF as it provides the best interoperability. Representing Windows Phone 8 I’m using the Nokia Lumia 920 although the 820 Developer phone provides the same experience. Representing Windows 8/RT is the Asus VivoTab RT which is one of few tablets with NFC support built in. Representing Android is the Samsung Galaxy S3. Since I’m less familiar with Android there is a possibility that some of the functionality I have discovered is Samsung specific and not part of the core Android OS.

Sending

The first table here shows the formats for which sending is supported within the platform. I haven’t included app links as these are platform specific. I was disappointed to find that out of the box the Windows tablet has no capability to share items over NFC from the built in apps. The send behaviour on Android is very seamless – tap the device against another and if the current app can share the screen changes slightly and you tap to share the current item. On Windows Phone you have to go through a few layers of menu first before being prompted to tap a device.

 

 

Windows Phone

Windows 8/RT

Android 4.1

Uri

Yes

Yes

Yes

Mime (vCard)

Yes

Requires 3rd Party App

Yes

Text

No

No

No

 

Receiving

The platforms can receive over NFC so long as the device is not pin locked. The following list shows what types of data (or pre-written tags) you can read on these devices without any specific third-party apps running.

 

Windows Phone

Windows 8/RT

Android 4.1

Uri

Yes

Yes

Yes

Mime (vCard)

Yes

Yes

Yes

Text

No

No

Yes

 

So from a very high level the three platforms support the basic tag types for Uris and Contacts across the board. Although Android has default behaviour when receiving a Text tag it just displays the text on screen – there is no functionality to do anything with the text. In a future investigation I’ll look into the tag types in more detail. The Uri tag for example is very powerful because you can register custom Uri schemes for your app so that a Uri tag could provide a deep link into your application. The systems maintain support for well-defined Uri schemas like http, tel and sms for example. There are additional platform specific ones on Windows Phone for launching items such as Wifi and Bluetooth Settings pages.