Category: NFC

  • NFC support for all Uri types

    In order to save a few bytes, there is a special mechanism used in Uri NFC tags where one byte is reserved to match with specific Uri prefixes. A value of zero means the following text contains the full Uri, a value of one means the Uri starts with “http://www.”. There are actually 35 of these constants in total.

    How this behaves depends on the platform – Android handles this for you and returns a valid Uri, but on iOS you receive the raw bytes and must use this initial byte and the rest of the data to reconstruct the full Uri. InTheHand.Nfc needs to handle this transparently for you and support all the Uri types. On iOS we had previously only handled the first few common Uri types and so there might be situations where it wouldn’t return the full valid Uri. This is now resolved in v1.2.4 so if you need to scan a telnet: Uri from an NFC tag this now works as expected!

    You can download the latest version from NuGet. This release also adds specific .NET 10.0 builds. InTheHand.Nfc now provides a feature complete implementation of the WebNFC standard for your .NET mobile apps.

  • Writing NFC Tags in .NET

    I first added NFC code to 32feet.NET back in 2020. However, as other priorities came along it never got fully polished and released on NuGet. This year I finally got around to it and updated the code for .NET 8.0 as it had originally targeted Xamarin Forms.

    InTheHand.Nfc is available on NuGet and follows the API model of WebNFC – it supports reading and writing NDEF tags as this is the most widely used format for providing Urls, text and other custom data types. Although the original Windows codebase is still there it is largely untouched and untested because there is no definitive NFC hardware for Windows and the external Sony reader I had been using is no longer supported. I was able to install the drivers and get it running but I’m not currently able to read tags through the API. Since iOS and Android are more important platforms for this functionality I’ve not spent any more time on the Windows implementation yet.

    There is a sample .NET MAUI app to demonstrate how to use the library. However it should work on any other .NET based platform (Uno, Avalonia etc). There are some platform specific setup items to observe – NFC reading requires permission on both Android and iOS. For iOS you must also add the NFC Reading capability to your app id.

    The code is quite simple. It’s all async and uses Events for detecting tags. You can either pass a cancellation token to control when to stop scanning or if you don’t it will automatically stop after the first tag is scanned.

    Hopefully you’ll find this useful for adding NFC reading/writing to your .NET apps. I do want to re-enable the Windows functionality, and make sure everything described in the sample is also documented in the Wiki. The only missing functionality from WebNFC is the ability to make a tag read-only; if there is a demand I could look at adding this too.

  • Android deep-links and NFC

    Deep-linking is the ability to define a Url which will direct the user straight to a specific feature of your app. On Android these are called App-links, for iOS they tend to be called Universal links. These can be an app-specific uri scheme or you can, with the right configuration on your web server, link a web domain so that you can redirect web links to the equivalent page in your app. I’m going to be working with the first type here.

    .NET MAUI allows you to support deep-linking on both platforms once you’ve set up the necessary handler code in each native project. This passes the received Uri to the Application class so you can handle parsing the Uri and navigating in a single place. I’m not going to go over this process because it is described in the MAUI documented for Android and iOS.

    This allows your app to be launched from a browser, QR Code, or another app. However the default experience on Android for an NFC tag results in a “New tag collected” dialog which asks you if you want to follow the link. Luckily, there is a way around this. You have to specify an additional action in your IntentFilter. Alongside Intent.ActionView you can add NfcAdapter.ActionNdefDiscovered. With this added your app is correctly registered to receive intents from Uri tags and will launch immediately when presented with your specific app link.

    [IntentFilter(new [] { Intent.ActionView, Android.Nfc.NfcAdapter.ActionNdefDiscovered },
    Categories = new [] { Intent.CategoryDefault, Intent.CategoryBrowsable },
    DataScheme = "yourscheme")]

    The great thing about implementing this is you may already be using deep-linking and you can support links from NFC tags without actually having to implement any NFC reading code within your app. You can have a consistent mechanism for working with links from a variety of sources.

  • Introducing Charming NFC Share

    In my talk this week I demonstrated a simple library for sharing a single Uri over NFC. I’ve now completed packaging it up and it is available on NuGet – https://www.nuget.org/packages/InTheHand.Phone.Nfc/

    This is a very simple component to use in your app requiring just three lines of code to call. It requires the Windows Phone Toolkit from which it uses page transitions, if you don’t already have this in your project NuGet will add it for you. It also requires you to add the Proximity capability in your app metadata. The Windows Phone Emulator exposes a null NFC device so it is possible to interact with this feature on the emulator (it just won’t send anything). The look and feel of the task has been designed to match the platform so it will look instantly familiar and supports both the light and dark themes.

    To share a Uri from your app you’ll need to execute the following:-

    Add the InTheHand.Phone.Nfc package via NuGet

    Add the ID_CAP_PROXIMITY capability to your WMAppManifest.xml

    Add the following code when you want to share a Uri:-

    InTheHand.Phone.Tasks.ShareNfcLinkTask snlt = new InTheHand.Phone.Tasks.ShareNfcLinkTask();

    snlt.LinkUri = new Uri(“http://peterfoot.net“);

    snlt.Show();

    
    

    Obviously setting the LinkUri to whatever URI you want to share. This could be a web link or a custom URI which performs some task in your app.

    At the moment the resources in the library are not localised beyond English although I hope to add this soon.

    This is the first in a set of related “Charming” libraries for Windows Phone. I’ll be adding more to NuGet shortly. Hopefully the reason for the name will become clearer too…

  • Resources from Bluetooth and NFC for Windows Phone and Beyond

    As promised here are the resources from yesterday’s session on Bluetooth and NFC. I’ve begun uploading the slides and code to this SkyDrive folder:-

    http://sdrv.ms/143OZD7

    I have a few finishing touches to put on the sharing library for NFC – it will be up on NuGet and CodePlex shortly.

     

  • NFC Text tags on WinRT

    To follow the last post I thought I would quickly round it off by looking at the code required to listen for tags on a Windows 8/RT device. Devices with NFC built in are like hens teeth but one such RT device exists in the Asus VivoTab RT. Because it’s an RT device that means running the Remote Debugger and doing the development on a separate Windows 8 PC. The problem with the Windows 8/RT experience is it is not as set in stone as Windows Phone and so issues like this occur (http://social.msdn.microsoft.com/Forums/en-US/tailoringappsfordevices/thread/9da68387-e0cb-4e02-ac71-bad61f6ff7c6) where some devices don’t correctly advertise support for all tag types. The good news with the Asus is that it does support subscribing to NDEF messages and you can use the same code as in my previous Windows Phone example to read a Text tag. The same Ndef library is used as this supports both Windows 8/RT and Windows Phone 8.

    One thing which is a little frustrating when remote debugging is that you have to register for a developer license on the tablet before the debug session will continue. So even though you have a license on your development machine you also need one on the tablet (despite the fact you can’t actually compile an app on it). There doesn’t seem to be a limit to how many devices you can register for a license using your Microsoft ID so this shouldn’t become a problem. You will find that you have to renew it regularly though…

    In a future post we will delve into other tag types…

  • NFC – Simple text tags on Windows Phone

    This article follows on from the last post on NFC to dig a little deeper on Windows Phone and Windows 8 (both of which share the same APIs for NFC).

    Requirement or just Capability

    In Windows Phone you have to add the ID_CAP_PROXIMITY capability to your app to use NFC. What about the ID_REQ_NFC requirement? Well the difference here is that when you specify a requirement the app will only be made available to devices with that feature. Whether you use the requirement therefore depends on whether NFC is core to your application or is an optional feature. This means it is valid to submit an app with NFC capabilities to devices which don’t have the hardware. Therefore you must always check first if

    ProximityDevice.GetDefault() == null

    Types of NFC Message

    So you’ve got as far as calling the SubscribeForMessage API to listen for NFC messages, if you check the help file for this function you’ll see it refers to the partner method “PublishBinaryMessage” to document the message types supported. Then you look down the list of supported protocols and your heart sinks! It’s a long list and there seems to be a lot of duplication – why when we have the NDEF standards are there Windows versions too? Which ones can I use across different platforms? Not to mention the fact that you can’t subscribe to all of these message types on Windows Phone so the utopia of a shared NFC API across both Windows APIs is shattered.

    The good news is that the Windows message types are actually helpers over the top of NDEF types and are designed to simplify reading those message types, we’ll look into those on Windows 8/RT at a later stage. There is a very good NDEF library on CodePlex (and NuGet) to parse standard NDEF records. For example the way to read a plain text NDEF.Text message is actually using the NDEF message type because the API doesn’t provide a subtype specifically for Text tags. When the tag is tapped you’ll receive a message payload which you can parse with the NdefLibrary.Ndef.NdefMessage class.

    private void MessageReceived(ProximityDevice device, ProximityMessage message) 
    {
    NdefLibrary.Ndef.NdefMessage.FromByteArray(message.Data.ToArray());
    
    string s = System.Text.Encoding.UTF8.GetString(msg[0].Payload, 0, msg[0].Payload.Length).Substring(2);
    }
    

    The final thing you’ll notice from the received text is that it contains the two-character language code at the beginning. Note that there is no error checking in the above sample and it assumes that the text tag is the first record in the message. You can see from the NdefMessage class that it contains a collection of records which can be of a number of specific types. More to follow as we delve deeper…

  • Cross Platform NFC

    NFC support in Windows Phone provides an exciting new way to add interactivity to apps and games. This series of posts will look at the state of NFC across the Windows and Android platforms. There are a number of standard Tag types defined by the NDEF format, for the sake of simplicity we’ll ignore the fact that Android can read other tag types and just concentrate on NDEF as it provides the best interoperability. Representing Windows Phone 8 I’m using the Nokia Lumia 920 although the 820 Developer phone provides the same experience. Representing Windows 8/RT is the Asus VivoTab RT which is one of few tablets with NFC support built in. Representing Android is the Samsung Galaxy S3. Since I’m less familiar with Android there is a possibility that some of the functionality I have discovered is Samsung specific and not part of the core Android OS.

    Sending

    The first table here shows the formats for which sending is supported within the platform. I haven’t included app links as these are platform specific. I was disappointed to find that out of the box the Windows tablet has no capability to share items over NFC from the built in apps. The send behaviour on Android is very seamless – tap the device against another and if the current app can share the screen changes slightly and you tap to share the current item. On Windows Phone you have to go through a few layers of menu first before being prompted to tap a device.

     

     

    Windows Phone

    Windows 8/RT

    Android 4.1

    Uri

    Yes

    Yes

    Yes

    Mime (vCard)

    Yes

    Requires 3rd Party App

    Yes

    Text

    No

    No

    No

     

    Receiving

    The platforms can receive over NFC so long as the device is not pin locked. The following list shows what types of data (or pre-written tags) you can read on these devices without any specific third-party apps running.

     

    Windows Phone

    Windows 8/RT

    Android 4.1

    Uri

    Yes

    Yes

    Yes

    Mime (vCard)

    Yes

    Yes

    Yes

    Text

    No

    No

    Yes

     

    So from a very high level the three platforms support the basic tag types for Uris and Contacts across the board. Although Android has default behaviour when receiving a Text tag it just displays the text on screen – there is no functionality to do anything with the text. In a future investigation I’ll look into the tag types in more detail. The Uri tag for example is very powerful because you can register custom Uri schemes for your app so that a Uri tag could provide a deep link into your application. The systems maintain support for well-defined Uri schemas like http, tel and sms for example. There are additional platform specific ones on Windows Phone for launching items such as Wifi and Bluetooth Settings pages.