Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to launch my app via NFC tag?

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.

like image 464
sibbl Avatar asked Dec 11 '15 10:12

sibbl


People also ask

Can NFC launch an app?

iPhone users are finally getting a convenient and powerful feature that's long been available on Android phones: the ability to tap an NFC tag and have their phone automatically perform certain actions, like opening an app or playing music.

How do I use the NFC app?

If you have a Samsung Android phone, check under settings > connections > tap NFC and contactless payments > tap the switch to turn NFC on. Once this is turned on for your device, you can adjust your settings for contactless payments and select your preferred mobile payment service, such as Google Pay or Samsung Pay.


1 Answers

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.

like image 149
Andreas Jakl Avatar answered Oct 04 '22 06:10

Andreas Jakl