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 ar
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());
}
}
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.