我可以多次写入标签,而无需将其与手机分开并再次点击它。例如,请参见以下代码:
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 条消息,另一台接收所有消息:
设备 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);
});
});