Question

I'm currently working with some NFC tags that I've confirmed to be working using the NFC Interactor app as well as the NFC Tag Writer available in the Windows Store.

My problem right now is that the app I'm writing is unable to read the NDEF Text that is contained within the tag, but the phone is detecting it just fine, opening some WP8 version of Notepad to display the text.

All I really need to do is get the text contained within the NFC tag to display on my app page, but it doesn't seem to work no matter what I try.

I've looked into the problem a little and I've found the "NDEF Library for Proximity API" for parsing NDEF messages, but it seems like it's overkill for receiving simple text...or is it?

My code is as follows:

private void messageReceived(ProximityDevice sender, ProximityMessage message)
    {
        var scanned_message = message.DataAsString;
        var messageType = message.MessageType;

        //message received handler. 
        Dispatcher.BeginInvoke(() =>
            {
                if (proximityDevice != null)
                {

                    locationdisplay.Text = "Tag found! Scanning...";
                    proximityDevice.StopSubscribingForMessage(Id);
                    locationdisplay.Text = "Type = " + messageType + "\nMessage = " + scanned_message;

                }

            });

    }

I know for a fact that the Windows.Networking.Proximity API does handle NDEF as a subscribed message type, but how it actually handles the message is a mystery to me... I'd hoped that message.DataAsString would have done the trick, but it doesn't seem to do anything in my app.

I've managed to read the data using another app and it does give me the raw payload as

"4e 00 6f 00 64 00 65 00 20 00 31"

which is the hex code for "Node 1" which is the text I've written in the tag. What I'm wondering is if the hex code is there...why can't it even show the numbers? (The 00 seems to be a custom spacer code for the app "NFC Tag Writer" in the Windows Store)

The messageType variable returns "NDEF" and can display it. The scanned_message variable returns a null string.

Was it helpful?

Solution

Self-solved.

Changing the message subscribe type to "Windows" type instead of "NDEF" type allowed the Proximity API to handle the message natively.

private void SubscribeForMessage()
        {
            Id = proximityDevice.SubscribeForMessage("WindowsMime", messageReceived);
        }
private void messageReceived(ProximityDevice sender, ProximityMessage message)
{
            var buffer = message.Data.ToArray();
            int mimesize = 0;
            //search first '\0' charactere
            for (mimesize = 0; mimesize < 256 && buffer[mimesize] != 0; ++mimesize)
            {
            };

            //extract mimetype
            var messageType = Encoding.UTF8.GetString(buffer, 0, mimesize);

            //convert data to string. This depends on mimetype value.
            var scanned_message = Encoding.UTF8.GetString(buffer, 256, buffer.Length - 256);

                Dispatcher.BeginInvoke(() =>
                    {
                        if (proximityDevice != null)
                        {
                            proximityDevice.StopSubscribingForMessage(Id);
                            locationdisplay.Text = scanned_message;

                        }
                    });
}


// For the code to work, I added 
// using System.Runtime.InteropServices.WindowsRuntime;
// for access to the ToArray() and AsBuffer() 
// functions to Read/Write respectively.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top