如何通过 NFC 标签启动我的应用程序? [英] How to launch my app via NFC tag?

查看:66
本文介绍了如何通过 NFC 标签启动我的应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在将应用程序移植到 UWP.该应用程序有一个带有写入 NFC"按钮的页面.用户点击后,它会等待 NFC 标签并写入 LaunchApp:WriteTag 二进制消息.

I'm currently working on porting an app to UWP. The app has a page with a "Write to NFC" button. After the user taps it, it waits for an NFC tag and writes a LaunchApp:WriteTag binary message.

在 WP8.1 下运行良好,在 Windows 10 UWP 下根本不起作用:

What worked fine under WP8.1, doesn't work at all under Windows 10 UWP:

var proximityDevice = Windows.Networking.Proximity.ProximityDevice.GetDefault();

if (proximityDevice != null)
{
    var launchArgs = "user=default";

    var appId = "App";
    var appName = Windows.ApplicationModel.Package.Current.Id.FamilyName + "!" + appId;

    var launchAppMessage = launchArgs + "	Windows	" + appName;

    var dataWriter = new Windows.Storage.Streams.DataWriter();
    dataWriter.UnicodeEncoding = Windows.Storage.Streams.UnicodeEncoding.Utf16LE;
    dataWriter.WriteString(launchAppMessage);
    var launchAppPubId = proximityDevice.PublishBinaryMessage("LaunchApp:WriteTag", dataWriter.DetachBuffer());
}

不幸的是,这不起作用.NFC 功能已启用,WP8.1 应用程序可在同一部手机上运行,​​因此这应该不是问题.

Unfortunately this doesn't work. The NFC capability is enabled and the WP8.1 app works on the same phone, so this shouldn't be an issue.

我已经尝试了多种格式,因为问题似乎出在 launchAppMessage 上,我没有在其中找到 UWP 文档.有一个 Windows 8+ MSDN 文章,其中描述了要采用以下格式的字符串:

I already tried multiple formats as the problem seems to be the launchAppMessage, where I didn't find a UWP doc for. There's a Windows 8+ MSDN article, which describes the string to be in the format:

myArgs	Windows	AppFamilyName!App

我尝试了什么:

  1. myArgs 足够短 - 应该没有问题.
  2. WindowsWindowsPhone 没有任何区别.两者都不起作用.
  3. AppFamilyName 是我的应用清单中正确的应用系列名称.该应用与商店相关联,看起来这也不应该是问题.
  4. App 是我的应用清单中 <Application id="App" .../> 的内容.尝试 MyAppNamespace.App 效果不佳,调用 CurrentApp.AppId(WinRT 应用程序中使用的内容)会引发异常.
  1. myArgs is short enough - shouldn't be a problem.
  2. Windows or WindowsPhone doesn't make any difference. Both don't work.
  3. AppFamilyName is the correct app family name that's inside my app manifest. The app is associated to the store and it looks like this shouldn't be the problem as well.
  4. App is what's inside <Application id="App" ... /> in my app manifest. Trying MyAppNamespace.App didn't work as well and calling CurrentApp.AppId (what's used in WinRT apps) throws an exception.

不工作"是指它写入标签,但 Windows 10 根本无法识别该标签.

By "not working" I mean that it writes to the tag, but the tag is not recognized by Windows 10 at all.

我发现的另一件事是,对于 myArgs Windows AppFamilyName!App,应用程序抛出以下异常 - 没有任何进一步的细节:

One more thing I found, is that for myArgs Windows AppFamilyName!App the app throws the following exception - without any further details:

System.ExecutionEngineException was unhandled
Message: An unhandled exception of type 'System.ExecutionEngineException' occurred in Unknown Module.

我真的希望有人知道如何解决这个问题.不幸的是,目前还没有用于此的 UWP 示例,而且文档仍然是旧的... :/

I really hope someone has an idea on how to solve this. Unfortunately there are no UWP samples for this yet and the docs are still the old ones... :/

PS:将自定义协议与 WindowsUri:WriteTag 一起使用效果很好,但我只想使用 NFC 标签打开我的应用程序.此外,确认对话框看起来像您想打开与 mycustomprotocol 关联的应用程序吗?"- 这看起来不是很用户友好.所以这对我来说不是真正的解决方案,而是一种我不想使用的解决方法.

PS: using a custom protocol together with WindowsUri:WriteTag works fine but I want only my app to open with the NFC tag. Also, the confirmation dialog then looks like "Do you want to open the app associated with mycustomprotocol?" - which looks not very user friendly. So that's no real solution for me, more a workaround I don't want to use.

推荐答案

Windows 10 移动版 UWP

如果您只针对 Windows 10 移动版,那么 8.1 方式仍然有效,前提是您获得了正确的 App ID.可以通过以下方式检索:

If you are only targeting Windows 10 Mobile, the 8.1 way still works, given that you get the right App ID. It can be retrieved through:

Windows.ApplicationModel.Store.CurrentApp.AppId

但是,这仅在通过商店安装应用程序时才有效,因为 ID 是在商店关联/发布期间分配的.在开发人员部署的版本中,API 将崩溃并显示来自 HRESULT 的异常:0x803F6107".

However, that only works when the app is installed through the store, as the ID is assigned during store association / publishing. In developer deployed builds, the API will crash through with "Exception from HRESULT: 0x803F6107".

生成的 LaunchApp 记录随后需要平台WindowsPhone"和该应用 ID.以下代码通过开源 NFC/NDEF 库(https://github.com/andijakl/ndef-nfc) 并适用于 Windows 10 移动版 - 用于编写标签和启动应用程序.再次 - 鉴于它已经出版&通过商店安装:

The resulting LaunchApp record then needs the platform "WindowsPhone" and that app ID. The following code creates a LaunchApp tag through the open source NFC / NDEF library (https://github.com/andijakl/ndef-nfc) and works on Windows 10 Mobile - both for writing the tag and for launching the app. Again - given it has been published & installed through the store:

var record = new NdefLaunchAppRecord { Arguments = "Hello World" };
var appId = Windows.ApplicationModel.Store.CurrentApp.AppId;    // Note: crashes when app is not installed through app store!
record.AddPlatformAppId("WindowsPhone", appId);
var message = new NdefMessage { record };
proximityDevice.PublishBinaryMessage("NDEF:WriteTag", msgArray.AsBuffer(), MessageWrittenHandler);

Windows 10 PC

不幸的是,PC 的情况有所不同.上述方法在那里不起作用,Windows 8.1 的文档化方法也不起作用.

Unfortunately, things are different for PCs. The method above does not work there, neither does the documented method for Windows 8.1.

到目前为止,我最接近的是让 Windows 10 识别 LaunchApp 标签并在正确的页面上打开商店.但是Windows/商店没有意识到该应用程序已经安装,因此没有打开它.

The closest I could get so far is to get Windows 10 to recognize the LaunchApp tag and to open the store on the correct page. But Windows / the store does not realize that the app is already installed and therefore does not open it.

这是代码,再次使用 NFC/NDEF 库:

This is the code, again using the NFC / NDEF library:

var record = new NdefLaunchAppRecord { Arguments = "Hello World" };
var familyName = Windows.ApplicationModel.Package.Current.Id.FamilyName;
var appId = Windows.ApplicationModel.Store.CurrentApp.AppId;    // Note: crashes when app is not installed through app store!
record.AddPlatformAppId("Windows", "{" + familyName + "!" + appId + "}");
var message = new NdefMessage { record };
proximityDevice.PublishBinaryMessage("NDEF:WriteTag", msgArray.AsBuffer(), MessageWrittenHandler);

当然,您也可以将两个平台 ID 合并为一个 NFC 标签,前提是您有足够的可写内存,因为这些应用 ID 很大.

Of course, you can also combine the two platform IDs to a single NFC tag, given that you have enough writable memory, as those app IDs are huge.

这篇关于如何通过 NFC 标签启动我的应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆