简体   繁体   中英

Write NDEF message multiple times to same tag?

On Android, as soon as an NFC tag gets in proximity of the phone, the system delievers an intent to my application that contains an objects that allows me to read and write the NDEF message of this tag. Specifically, I can write to this tag as often as I want, while it's still in proxmity of the phone. The Java code below gives you an impression of what i mean:

Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
Ndef ndef = Ndef.get(tag);
ndef.writeNdefMessage(/* some NDEF data */); // first write
ndef.writeNdefMessage(/* some NDEF data */); // second write
// further writes
ndef.writeNdefMessage(/* some NDEF data */); // n-th write

Can I do the same on Windows Phone 8.x , or can I only carry out a single NDEF-message-write operation to the tag and then need to bring it into proximity again (move off RF field and come back into with the tag)?

I'm able to write to a tag more than once without separating it from the phone and tapping it again. See following code for example:

ProximityDevice device = ProximityDevice.GetDefault();
device.SubscribeForMessage("WriteableTag", WriteableTagHandler);


private void WriteableTagHandler(ProximityDevice sender, ProximityMessage message)
{
    var message1= Encoding.Unicode.GetBytes("http://1stUrl.com");
    var message2 = Encoding.Unicode.GetBytes("http://secondUrl.com");

    sender.PublishBinaryMessage("WindowsUri:WriteTag", message1.AsBuffer(), (s, e) =>
        {
            s.StopPublishingMessage(e);
            sender.PublishBinaryMessage("WindowsUri:WriteTag", message2.AsBuffer(), (se,r)=>
            {
                se.StopPublishingMessage(r);
            });
        });              
}

EDIT:

I have just checked with two devices, and in fact, it is possible to write-read more than once without separating and tapping the phones again. See the example below, where one device sends 5 messages and the other receives all of them:

Device 1 (sender):

ProximityDevice device = ProximityDevice.GetDefault();

device.DeviceArrived += (e) =>
    {
        for (int i = 1; i <= 5; i++)
        {
            e.PublishMessage("Windows.mySubType", "message " + i.ToString(), (s, m) =>
                {
                    s.StopPublishingMessage(m);
                });
        }
    };

Device 2 (receiver):

ProximityDevice device = ProximityDevice.GetDefault();

device.SubscribeForMessage("Windows.mySubType", (s, e) =>
    {
        Dispatcher.BeginInvoke(() =>
            {
                MessageBox.Show(e.DataAsString);
            });
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM