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

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

问题描述

我目前工作的一个移植应用UWP。该应用程序有一个页面有写入NFC按钮。用户点击后,它会等待一个NFC标签,并写了一个 LaunchApp:WriteTag 二进制消息



什么在WP8.1工作得很好,并没有在所有的工作在Windows 10 UWP:

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

如果(!proximityDevice = NULL)
{
VAR launchArgs =用户=默认值;

VAR的appid =应用程序;
变种的appName = Windows.ApplicationModel.Package.Current.Id.FamilyName +! + APPID;

VAR launchAppMessage = launchArgs +\tWindows\t+的appName;

变种dataWriter =新Windows.Storage.Streams.DataWriter();
dataWriter.UnicodeEncoding = Windows.Storage.Streams.UnicodeEncoding.Utf16LE;
dataWriter.WriteString(launchAppMessage);
变种launchAppPubId = proximityDevice.PublishBinaryMessage(LaunchApp:WriteTag,dataWriter.DetachBuffer());
}



可惜,这是行不通的。该NFC功能被启用和WP8.1应用程序的工作原理相同的电话,所以这不应该是一个问题。



我已经尝试过多种格式的问题,看来是 launchAppMessage ,在那里我没有找到一个UWP文档。有一个的的Windows 8+ 的MSDN文章,其中介绍的字符串,格式为:

  myArgs\tWindows \tAppFamilyName应用

我想什么:




  1. myArgs 足够短 - 不应该是一个问题

  2. 视窗的WindowsPhone 没有任何区别。这两个不工作。

  3. AppFamilyName 是正确的应用姓那是我的应用程序清单中。该应用程序相关联的商店,它看起来像这不应该是这个问题为好。

  4. 应用是里面有什么<应用ID =应用程序... /> 我的应用程序清单。试图 MyAppNamespace.App 没有工作,以及和呼叫 CurrentApp.AppId (什么在WinRT的应用程序使用)抛出例外。



这是不工作我的意思是写入标签,但标签不能被Windows 10的认可可言。



还有一件事,我发现,是, myArgs\tWindows\tAppFamilyName!应用应用程序抛出以下情况例外 - 没有任何进一步的细节:

  System.ExecutionEngineException是未处理的
消息:类型的未处理的异常系统.ExecutionEngineException'发生在未知模块。



我真的希望有人对如何解决这个问题的想法。不幸的是这没有UWP样品还和文档仍然是旧的...:/



PS:使用自定义的协议连同 WindowsUri:WriteTag 工作正常,但我只希望我的应用程序与NFC标签打开。此外,确认对话框,然后看起来像你要开mycustomprotocol相关联的应用程序? - 这看起来不是很方便。所以这对我来说没有真正的解决方案,更多的是解决办法,我不想使用。


解决方案


<强>视窗10移动UWP



如果你只针对Windows 10手机,8.1的方式仍然有效,因为你得到正确的应用程序ID。它可以通过检索:



<预类=郎-CS prettyprint-覆盖> Windows.ApplicationModel.Store.CurrentApp.AppId

然而,只有当通过商店安装的应用程序,作为ID的商店会/出版过程中分配工作。在开发部署的版本,该API将从HRESULT异常:0x803F6107撞车经过。



由此产生的LaunchApp记录则需要平台的WindowsPhone,而应用程序ID。下面的代码创建通过开源NFC / NDEF库LaunchApp标签( https://github.com/andijakl / NDEF-NFC )和Windows 10的移动工作 - 无论是写标签和启动应用程序。再次 - 给它已经出版和放大器;通过商店安装:



<预类=郎-CS prettyprint-覆盖> VAR纪录=新NdefLaunchAppRecord {参数=Hello World的};
变种的appid = Windows.ApplicationModel.Store.CurrentApp.AppId; //注:当应用程序不是通过应用商店安装的崩溃!
record.AddPlatformAppId(WindowsPhone的APPID);
VAR消息=新NdefMessage {录音};
proximityDevice.PublishBinaryMessage(NDEF:WriteTag,msgArray.AsBuffer(),MessageWrittenHandler);



的Windows 10 PC



不幸的是,事情是个人电脑不同。上面的方法并没有在那里工作,同样没有为Windows 8.1所记录的方法。



我能得到到目前为止最接近的是要获得Windows 10来识别LaunchApp标记和打开正确的页面上的商店。但在Windows /店里没有意识到该应用程序已安装,因此不能打开它。



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



<预类=郎-CS prettyprint-覆盖> VAR纪录=新NdefLaunchAppRecord {参数=Hello World的};
VAR familyName = Windows.ApplicationModel.Package.Current.Id.FamilyName;
变种的appid = Windows.ApplicationModel.Store.CurrentApp.AppId; //注:当应用程序不是通过应用商店安装的崩溃!
record.AddPlatformAppId(窗口,{+ familyName + + + APPID!});
VAR消息=新NdefMessage {录音};
proximityDevice.PublishBinaryMessage(NDEF:WriteTag,msgArray.AsBuffer(),MessageWrittenHandler);



当然,你也可以将二者结合起来的平台ID来一个NFC标签,因为你有足够的可写存储器,因为这些应用程序ID是巨大的。


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.

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 + "\tWindows\t" + 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());
}

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.

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\tWindows\tAppFamilyName!App

What I tried:

  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.

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

One more thing I found, is that for myArgs\tWindows\tAppFamilyName!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.

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: 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 Mobile UWP

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

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".

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

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

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.

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

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天全站免登陆