简体   繁体   English

如何通过NFC标签启动我的应用程序?

[英]How to launch my app via NFC tag?

I'm currently working on porting an app to UWP. 我目前正致力于将应用程序移植到UWP。 The app has a page with a "Write to NFC" button. 该应用程序有一个页面,其中包含“写入NFC”按钮。 After the user taps it, it waits for an NFC tag and writes a LaunchApp:WriteTag binary message. 在用户点击它之后,它等待NFC标签并写入LaunchApp:WriteTag二进制消息。

What worked fine under WP8.1, doesn't work at all under Windows 10 UWP: 什么在WP8.1下工作正常,在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. NFC功能已启用,WP8.1应用程序可在同一部手机上运行,​​因此这不应成为问题。

I already tried multiple formats as the problem seems to be the launchAppMessage , where I didn't find a UWP doc for. 我已经尝试了多种格式,因为问题似乎是launchAppMessage ,我没有找到UWP文档。 There's a Windows 8+ MSDN article, which describes the string to be in the format: 有一篇Windows 8+ MSDN文章,它描述了格式为的字符串:

myArgs\tWindows\tAppFamilyName!App

What I tried: 我尝试了什么:

  1. myArgs is short enough - shouldn't be a problem. myArgs很短 - 不应该是个问题。
  2. Windows or WindowsPhone doesn't make any difference. WindowsWindowsPhone没有任何区别。 Both don't work. 两者都不起作用。
  3. AppFamilyName is the correct app family name that's inside my app manifest. AppFamilyName是我的应用清单中正确的应用系列名称。 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. App是我的应用程序清单中的<Application id="App" ... /> Trying MyAppNamespace.App didn't work as well and calling CurrentApp.AppId (what's used in WinRT apps) throws an exception. 尝试MyAppNamespace.App也不起作用,调用CurrentApp.AppId (WinRT应用程序中使用的内容)会引发异常。

By "not working" I mean that it writes to the tag, but the tag is not recognized by Windows 10 at all. “不工作”我的意思是它写入标签,但Windows 10根本无法识别标签。

One more thing I found, is that for myArgs\\tWindows\\tAppFamilyName!App the app throws the following exception - without any further details: 我发现的另一件事是,对于myArgs\\tWindows\\tAppFamilyName!App ,应用程序抛出以下异常 - 没有任何进一步的细节:

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... :/ 不幸的是,目前还没有UWP样本,而且文档仍旧是旧的......:/

PS: using a custom protocol together with WindowsUri:WriteTag works fine but I want only my app to open with the NFC tag. PS:与WindowsUri:WriteTag一起使用自定义协议WindowsUri:WriteTag工作正常,但我只想用NFC标签打开我的应用程序。 Also, the confirmation dialog then looks like "Do you want to open the app associated with mycustomprotocol?" 此外,确认对话框看起来像“你想打开与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 Windows 10移动UWP

If you are only targeting Windows 10 Mobile, the 8.1 way still works, given that you get the right App ID. 如果您只针对Windows 10移动版,那么8.1方式仍然有效,因为您获得了正确的应用程序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. 但是,这仅适用于通过商店安装应用程序时,因为在商店关联/发布期间分配了ID。 In developer deployed builds, the API will crash through with "Exception from HRESULT: 0x803F6107". 在开发人员部署的构建版本中,API将通过“来自HRESULT的异常:0x803F6107”崩溃。

The resulting LaunchApp record then needs the platform "WindowsPhone" and that app ID. 然后,生成的LaunchApp记录需要平台“WindowsPhone”和该应用程序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. 以下代码通过开源NFC / NDEF库( https://github.com/andijakl/ndef-nfc )创建LaunchApp标记,并在Windows 10 Mobile上运行 - 用于编写标记和启动应用程序。 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 Windows 10 PC

Unfortunately, things are different for PCs. 不幸的是,PC的情况有所不同。 The method above does not work there, neither does the documented method for Windows 8.1. 上面的方法在那里不起作用,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. 我到目前为止最接近的是让Windows 10识别LaunchApp标签并在正确的页面上打开商店。 But Windows / the store does not realize that the app is already installed and therefore does not open it. 但Windows /商店没有意识到该应用程序已经安装,因此无法打开它。

This is the code, again using the NFC / NDEF library: 这是代码,再次使用NFC / NDEF库:

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. 当然,您还可以将两个平台ID组合到一个NFC标签中,因为您有足够的可写内存,因为这些应用ID很大。

Here is a way : 这是一种方式:

  1. Register a background task and using a NFC trigger 注册后台任务并使用NFC触发器
  2. Register your app for uri activation. 注册您的应用程序以激活uri。
  3. Program the backgroundtask to activate when tapped with a NFC tag and then launch the app suing URI schemes 编程后台任务以在使用NFC标签轻触时激活,然后启动app suing URI方案

    // Set the recommended app //设置推荐的应用程序

     var options = new Windows.System.LauncherOptions(); options.PreferredApplicationPackageFamilyName= "Contoso.URIApp_8wknc82po1e"; options.PreferredApplicationDisplayName = "Contoso URI Ap"; // Launch the URI and pass in the recommended app // in case the user has no apps installed to handle the URI var success = await Windows.System.Launcher.LaunchUriAsync(uriContoso, options); 

If you have a WP8.1 app and running this app on Windows Phone 10 and want to write NFC tags on your phone you have to retrieve the AppId . 如果您有一个WP8.1应用程序并在Windows Phone 10上运行此应用程序并想要在手机上编写NFC标签,则必须检索AppId This is done by opening the Windows Dev Center open the corresponding app, click App Management and then App identity . 这是通过打开Windows开发中心打开相应的应用程序,单击App Management然后单击App Management App identity Then under the point URL for Windows Phone 8.1 and earlier copy the GUID and replace Windows.ApplicationModel.Store.CurrentApp.AppId 然后在URL for Windows Phone 8.1 and earlier版本的点URL for Windows Phone 8.1 and earlier下复制GUID并替换Windows.ApplicationModel.Store.CurrentApp.AppId

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 通过NFC启动未发布的WP8应用程序 - Launch Unpublished WP8 app via NFC 如何使NFC应用程序成为Windows Phone中的“受信任的应用程序”? - How to make my NFC app a “trusted app” in Windows Phone? Windows Phone 8.1:如何从我的应用程序启动已安装的应用程序 - Windows Phone 8.1 : how to Launch an Installed app From My app 如何以编程方式从我的 Windows 10 应用程序启动相机应用程序? - How to launch camera app from my windows 10 app programmatically? 如何在浏览器中单击按钮启动Windows通用应用程序? - How to launch my windows universal app with button click in browser? 如何在Azure函数中启动我的控制台应用程序可执行文件 - How to launch my console app executable in an Azure-Function 如何在阻止WP启动当前存储在NFC标签上的动作的同时编写NFC标签 - How to write NFC tags while preventing WP from launching the action which is currently stored on NFC tag 是否可以使用NDEFLaunchApp记录启动系统/第三方应用程序以获取URI并使用不带NFC标签的LaunchUriAsync? - Is it possible to launch system/third party app using an NDEFLaunchApp record to get the URI and by using LaunchUriAsync without NFC Tags? 如何从Windows Phone 8中的Nfc标签读取多个NdefTextRecord - How to read multiple NdefTextRecord from Nfc tag in windows phone 8 如何确定 nfc 标签是否已被锁定而不覆盖其上的任何内容? - How to find out if nfc tag is locked without overwriting anything on it already?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM