多次将 NDEF 消息写入同一个标签? [英] Write NDEF message multiple times to same tag?

查看:17
本文介绍了多次将 NDEF 消息写入同一个标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Android 上,只要 NFC 标签靠近手机,系统就会向我的应用程序发送一个意图,其中包含一个对象,允许我读取和写入此标签的 NDEF 消息.具体来说,我可以根据需要随时写入此标签,而它仍在手机附近.下面的 Java 代码让您对我的意思有一个印象:

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

我可以在 Windows Phone 8.x 上做同样的事情吗,或者我可以只对标签执行一个 NDEF-message-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);
            });
        });              
}

我刚刚检查了两个设备,事实上,可以多次读写,而无需再次分离和敲击手机.请参见下面的示例,其中一台设备发送 5 条消息,另一台设备接收所有消息:

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:

设备 1(发送方):

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);
                });
        }
    };

设备 2(接收器):

ProximityDevice device = ProximityDevice.GetDefault();

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

这篇关于多次将 NDEF 消息写入同一个标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆