如何在Windows Phone 8上使用NFC标签启动应用程序?

3
我有一个Windows Phone 8设备,几个NDEF格式的NFC标签,想知道是否可以使用这些标签在我的WP8上实现应用程序启动?我已经仔细阅读了Windows Phone 8内置应用程序URI方案的文章,但是我没有找到任何与实际启动第三方应用程序相关的链接。我可以启动各种设置屏幕、浏览器、电子邮件、短信等。
更有趣的是,WP商店中至少有两个NFC标签可以“编写标签以启动应用程序”,我尝试过它们,但启动就是不起作用。
所以问题是:是否可以在NFC标签上存储启动任何第三方应用程序的信息?如果是,这种URI方案的格式是什么,如何使用WP8将其写入标签?
2个回答

7
是的,你可以使用 NFC 标签启动任何 Windows Phone 8 应用程序。您需要在标签上放置一个 NDEF 消息,该消息具有作为第一个记录的 LaunchApp 记录。将 NDEF 记录中的平台 ID 设置为 "WindowsPhone",并将应用 ID 设置为 Windows Phone 商店 URL 末尾大括号 "{" 和 "}" 之间的十六进制字符串。例如,对于http://www.windowsphone.com/en-us/store/app/stackoverflow-feeds/226fcc72-69ff-4a85-b945-cbb7f5ea00af,应设置应用 ID 为 "{226fcc72-69ff-4a85-b945-cbb7f5ea00af}"。 这个库可以帮助创建此类 NDEF 记录。Microsoft 提供了有限的文档,点击这里查看。

嘿,它可以工作。我尝试了另一个应用程序,但它没有工作,然后尝试了NFC Interactor,它可以工作。但是我有一个问题,这个在MSDN页面上有记录吗? - Martin Suchan
我已经在我的回答中链接了我所知道的所有在线信息。 - NFC guy

6
要通过NFC标签启动您的应用程序,您需要在WMAppManifest.xml文件中添加一个扩展程序来为其注册URI关联,如下所示:

要通过NFC标签启动您的应用程序,您需要在WMAppManifest.xml文件中添加一个扩展程序来为其注册URI关联,如下所示:

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

然后,您需要创建一个URI映射器来处理URI关联,就像这样:

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;
    }
}

以下是编写启动应用程序的NFC标签的代码:

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

1
我知道这个协议扩展,但我想知道如果我知道应用程序ID,是否可以启动任何应用程序? - Martin Suchan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接