在Windows Phone 8中读取NDEF

编程入门 行业动态 更新时间:2024-10-28 09:26:20
Windows Phone 8中读取NDEF-Text标记(Reading NDEF-Text tags in Windows Phone 8)

我目前正在使用一些NFC标签,我已经确认使用NFC Interactor应用程序以及Windows应用商店中提供的NFC标签编写器。

我现在的问题是我正在编写的应用程序无法读取标签中包含的NDEF文本,但是手机正好检测它,打开一些WP8版本的记事本来显示文本。

我真正需要做的就是让NFC标签中包含的文字显示在我的应用页面上,但无论我尝试什么,它似乎都不起作用。

我已经仔细研究了这个问题,并且我发现了用于解析NDEF消息的“用于Proximity API的NDEF库”,但看起来接收简单文本有点过分......或者是它?

我的代码如下:

private void messageReceived(ProximityDevice sender, ProximityMessage message) { var scanned_message = message.DataAsString; var messageType = message.MessageType; //message received handler. Dispatcher.BeginInvoke(() => { if (proximityDevice != null) { locationdisplay.Text = "Tag found! Scanning..."; proximityDevice.StopSubscribingForMessage(Id); locationdisplay.Text = "Type = " + messageType + "\nMessage = " + scanned_message; } }); }

我知道Windows.Networking.Proximity API确实将NDEF作为订阅消息类型处理,但它实际上如何处理消息对我来说是一个谜......我希望message.DataAsString可以做到这一点,但它似乎没有在我的应用程序中做任何事情。

我已经设法使用另一个应用程序读取数据,它确实给我原始有效负载

“4e 00 6f 00 64 00 65 00 20 00 31”

这是我在标签中写的文本“节点1”的十六进制代码。 我想知道的是,如果十六进制代码在那里......为什么它甚至不显示数字? (00似乎是Windows应用程序中“NFC Tag Writer”的自定义间隔代码)

messageType变量返回“NDEF”并可以显示它。 scans_message变量返回空字符串。

I'm currently working with some NFC tags that I've confirmed to be working using the NFC Interactor app as well as the NFC Tag Writer available in the Windows Store.

My problem right now is that the app I'm writing is unable to read the NDEF Text that is contained within the tag, but the phone is detecting it just fine, opening some WP8 version of Notepad to display the text.

All I really need to do is get the text contained within the NFC tag to display on my app page, but it doesn't seem to work no matter what I try.

I've looked into the problem a little and I've found the "NDEF Library for Proximity API" for parsing NDEF messages, but it seems like it's overkill for receiving simple text...or is it?

My code is as follows:

private void messageReceived(ProximityDevice sender, ProximityMessage message) { var scanned_message = message.DataAsString; var messageType = message.MessageType; //message received handler. Dispatcher.BeginInvoke(() => { if (proximityDevice != null) { locationdisplay.Text = "Tag found! Scanning..."; proximityDevice.StopSubscribingForMessage(Id); locationdisplay.Text = "Type = " + messageType + "\nMessage = " + scanned_message; } }); }

I know for a fact that the Windows.Networking.Proximity API does handle NDEF as a subscribed message type, but how it actually handles the message is a mystery to me... I'd hoped that message.DataAsString would have done the trick, but it doesn't seem to do anything in my app.

I've managed to read the data using another app and it does give me the raw payload as

"4e 00 6f 00 64 00 65 00 20 00 31"

which is the hex code for "Node 1" which is the text I've written in the tag. What I'm wondering is if the hex code is there...why can't it even show the numbers? (The 00 seems to be a custom spacer code for the app "NFC Tag Writer" in the Windows Store)

The messageType variable returns "NDEF" and can display it. The scanned_message variable returns a null string.

最满意答案

自我解决。

将消息订阅类型更改为“Windows”类型而不是“NDEF”类型允许Proximity API本机处理消息。

private void SubscribeForMessage() { Id = proximityDevice.SubscribeForMessage("WindowsMime", messageReceived); } private void messageReceived(ProximityDevice sender, ProximityMessage message) { var buffer = message.Data.ToArray(); int mimesize = 0; //search first '\0' charactere for (mimesize = 0; mimesize < 256 && buffer[mimesize] != 0; ++mimesize) { }; //extract mimetype var messageType = Encoding.UTF8.GetString(buffer, 0, mimesize); //convert data to string. This depends on mimetype value. var scanned_message = Encoding.UTF8.GetString(buffer, 256, buffer.Length - 256); Dispatcher.BeginInvoke(() => { if (proximityDevice != null) { proximityDevice.StopSubscribingForMessage(Id); locationdisplay.Text = scanned_message; } }); } // For the code to work, I added // using System.Runtime.InteropServices.WindowsRuntime; // for access to the ToArray() and AsBuffer() // functions to Read/Write respectively.

Self-solved.

Changing the message subscribe type to "Windows" type instead of "NDEF" type allowed the Proximity API to handle the message natively.

private void SubscribeForMessage() { Id = proximityDevice.SubscribeForMessage("WindowsMime", messageReceived); } private void messageReceived(ProximityDevice sender, ProximityMessage message) { var buffer = message.Data.ToArray(); int mimesize = 0; //search first '\0' charactere for (mimesize = 0; mimesize < 256 && buffer[mimesize] != 0; ++mimesize) { }; //extract mimetype var messageType = Encoding.UTF8.GetString(buffer, 0, mimesize); //convert data to string. This depends on mimetype value. var scanned_message = Encoding.UTF8.GetString(buffer, 256, buffer.Length - 256); Dispatcher.BeginInvoke(() => { if (proximityDevice != null) { proximityDevice.StopSubscribingForMessage(Id); locationdisplay.Text = scanned_message; } }); } // For the code to work, I added // using System.Runtime.InteropServices.WindowsRuntime; // for access to the ToArray() and AsBuffer() // functions to Read/Write respectively.

更多推荐

NDEF,NFC,app,message,电脑培训,计算机培训,IT培训"/> <meta name="descript

本文发布于:2023-07-29 15:55:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1317635.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Windows   Phone   NDEF

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!