anderZubi
anderZubi

Reputation: 6424

Read from NFC tag with NDEF text

I'm trying to read from a NFC tag using following code:

    private void SubscribeToNFCMessage()
    {
        device.SubscribeForMessage("NDEF", (s, e) =>
            {
                DataReader reader = DataReader.FromBuffer(e.Data);
                string str = reader.ReadString(reader.UnconsumedBufferLength);
            });
    }

When tapping the phone with the card the MessageReceivedHandler is executed. However, it throws a System.ArgumentOutOfRangeException exception when calling reader.ReadString(reader.UnconsumedBufferLength);

e.Data.Length value is 12, so is the value of reader.UnconsumedBufferLength

How should I read the data from the tag?

Upvotes: 1

Views: 4368

Answers (1)

Codo
Codo

Reputation: 78815

A NDEF message consists of several parts. Some parts of it might contain a string. But it cannot be directly converted into a string. So the error message is no surprise.

As decoding a NDEF message isn't trivial, I recommend using a library for it such as NDEF Library for Proximity APIs (NFC)..

Examples how to use the library can be found directly on their home page.

Upvotes: 2

Related Questions