How to launch my app via NFC tag?

后端 未结 3 1273
日久生厌
日久生厌 2021-01-01 10:27

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 Launch

相关标签:
3条回答
  • 2021-01-01 11:02

    Here is a way :

    1. Register a background task and using a NFC trigger
    2. Register your app for uri activation.
    3. Program the backgroundtask to activate when tapped with a NFC tag and then launch the app suing URI schemes

      // 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);
      
    0 讨论(0)
  • 2021-01-01 11:09

    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.

    0 讨论(0)
  • 2021-01-01 11:25

    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. This is done by opening the Windows Dev Center open the corresponding app, click App Management and then App identity. Then under the point URL for Windows Phone 8.1 and earlier copy the GUID and replace Windows.ApplicationModel.Store.CurrentApp.AppId

    0 讨论(0)
提交回复
热议问题