3
votes

I got Windows Phone 8 device, few NDEF formatted NFC tags and I'm wondering, is it possible to implement app launching on my WP8 using these tags? I've read thoroughly this article about URI schemes for launching built-in apps for Windows Phone 8, but I haven't found any link related to actual launching of 3rd party apps. I can launch various settings screens, or browser, email, SMS...

What is more interesting, that there are at least two NFC tags on WP Store that can "write tags for launching apps", I've tried them, but the launching is just not working.

So the question is: is it possible to store on NFC tag information for launching any 3rd party app on WP8? If yes, what is the format of such URI scheme and how to write it to a tag using WP8?

2

2 Answers

7
votes

Yes, you can launch any Windows Phone 8 app using an NFC tag. You need to put an NDEF message on the tag that has a LaunchApp record as the first record. Set the platform ID in the payload of the NDEF record to "WindowsPhone" and set the app ID to the hex string at the end of Windows Phone store URL between "{" and "}" For example, for http://www.windowsphone.com/en-us/store/app/stackoverflow-feeds/226fcc72-69ff-4a85-b945-cbb7f5ea00af to "{226fcc72-69ff-4a85-b945-cbb7f5ea00af}".

This library can help to create such NDEF records. Limited documentation from MS is available here.

6
votes

To launch your app via a NFC tag, you need to Register it for a URI association by adding an extension in the WMAppManifest.xml file, like this:

<Extensions>
  <Protocol Name="mynfcapp" NavUriFragment="encodedLaunchUri=%s" TaskID="_default" />
</Extensions>

Then you will need to create a URI mapper that can handle the URI association, like this:

public class AssociationUriMapper : UriMapperBase
{
    public override Uri MapUri(Uri uri)
    {
        string url = HttpUtility.UrlDecode(uri.ToString());

        if (url.Contains("mynfcapp:MainPage"))
        {
            int paramIndex = url.IndexOf("source=") + 7;
            string paramValue = url.Substring(paramIndex);

            return new Uri("/MainPage.xaml?source=" + paramValue, UriKind.Relative);
        }

        return uri;
    }
}

And here is the code to write the NFC tag that will launch the app:

public partial class MainPage : PhoneApplicationPage
{
    private readonly ProximityDevice _proximityDevice;
    private long subId = 0;
    private long pubId = 0;

    public MainPage()
    {
        InitializeComponent();
        _proximityDevice = ProximityDevice.GetDefault();
    }

    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        if (_proximityDevice != null)
            subId = _proximityDevice.SubscribeForMessage("WriteableTag", OnWriteableTagArrived);

        base.OnNavigatedTo(e);
    }
    private void OnWriteableTagArrived(ProximityDevice sender, ProximityMessage message)
    {
        var dataWriter = new DataWriter();
        dataWriter.UnicodeEncoding = Windows.Storage.Streams.UnicodeEncoding.Utf16LE;
        string appLauncher = string.Format(@"mynfcapp:MainPage?source=mynfctest");
        dataWriter.WriteString(appLauncher);
        pubId = sender.PublishBinaryMessage("WindowsUri:WriteTag", dataWriter.DetachBuffer());
    }
}