La0x1
La0x1

Reputation: 165

C# NFC Proximity - Handler is not fired

I have a serious problem with writting and reading NFC tags. The handler is not fired.

This is my code to write some text to a NFC tag and it's working:

// Writes a string to a NFC tag
    private bool WriteToNFCTag(string value)
    {

            var dataWriter = new DataWriter() { UnicodeEncoding = Windows.Storage.Streams.UnicodeEncoding.Utf8 };
            dataWriter.WriteString(value);

            ProximityDevice.GetDefault().PublishBinaryMessage("Windows:WriteTag.NokiaTest", dataWriter.DetachBuffer(), MesssageTransmitted);


    }

private void MesssageTransmitted(ProximityDevice sender, long messageId)
        {
            Debug.WriteLine("Message sent!");
        }

But I can't read the NFC tag:

ProximityDevice.GetDefault().SubscribeForMessage("Windows.NokiaTest", messageReceivedHandler);

doesn't fire the the messageReceivedHandler

void messageReceivedHandler(ProximityDevice device, ProximityMessage message)
    {
        Debug.WriteLine("Got the message");
        var byteBuffer = message.Data;
    }

I don't get any error messages or something like that. I would be great if someone could help me. Thanks!

Upvotes: 1

Views: 1079

Answers (1)

kennyzx
kennyzx

Reputation: 12993

I think you need to stop the publication in the MesssageTransmitted method.

private void MesssageTransmitted(ProximityDevice sender, long messageId)
{
    Debug.WriteLine("Message sent!");
    ProximityDevice.GetDefault().StopPublishingMessage(messageId);
}

Update: In recent months I have been developing a Win8.1 desktop application that writes/reads NFC tags, using NDEF format (protocols: "NDEF:WriteTag" to publish/"NDEF" to subscribe). I use NdefLibrary from https://ndef.codeplex.com/.

The "Windows" protocol, according to Nokia, is between devices, not between a device and a tag. So you might need to use NDEF protocols that are "between device and a tag".

enter image description here

also see according to Limitations with Windows Phone 8

Since Proximity API gives only high level access to the NFC protocol and Windows Phone adds some protection on top of that, your interaction with NFC tags is limited:

You cannot format a tag. Your tag must be formatted for NDEF message.

Your tag can contain only an NDEF message.

Proximity API does not give tools to manipulate directly NDEF messages. To manipulate raw NDEF messages you can use NDEF Library for Proximity APIs (NFC).

Upvotes: 1

Related Questions