Android – write nDef messages to the same tag multiple times?

On Android, as long as the NFC tag is close to the mobile phone, the system will send an intention to my application, which contains the object that allows me to read and write the nDef message of this tag. Specifically, I can write this tag anytime, anywhere, and it is still near the phone. The following java code gives you the impression that 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 write a single nDef message to a tag and then need to put it closer (move out of the RF field and return the tag)?

resolvent:

I can write the tag multiple times without separating it from the phone and click it again. See the following code:

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 just checked two devices. In fact, you can write and read multiple times without separating and clicking the phone again. Please refer to the following example. One device sends 5 messages and the other device receives all messages:

Device 1 (from):

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(() =>
            {
                Message@R_935_2419@.Show(e.DataAsString);
            });
    });

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>