C# 在Windows Phone 8中读取NDEF文本标记

C# 在Windows Phone 8中读取NDEF文本标记,c#,windows-phone-8,nfc,ndef,C#,Windows Phone 8,Nfc,Ndef,我目前正在使用一些NFC标签,我已经确认使用的是NFC Interactitor应用程序以及Windows应用商店中提供的NFC标签编写器 我现在的问题是,我正在编写的应用程序无法读取标签中包含的NDEF文本,但手机检测到它很好,打开WP8版本的记事本来显示文本 我真正需要做的就是让NFC标签中包含的文本显示在我的应用程序页面上,但不管我怎么做,它似乎都不起作用 我对这个问题进行了一些研究,发现了用于解析NDEF消息的“NDEF库接近API”,但对于接收简单文本来说似乎有些过分了……或者是这样吗

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

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

我真正需要做的就是让NFC标签中包含的文本显示在我的应用程序页面上,但不管我怎么做,它似乎都不起作用

我对这个问题进行了一些研究,发现了用于解析NDEF消息的“NDEF库接近API”,但对于接收简单文本来说似乎有些过分了……或者是这样吗

我的代码如下:

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.Proximition API确实将NDEF作为订阅的消息类型处理,但它如何实际处理消息对我来说是个谜。。。我本希望message.DataAsString能起到作用,但它在我的应用程序中似乎没有任何作用

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

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

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

messageType变量返回“NDEF”并可以显示它。 扫描的消息变量返回空字符串。

自行解决

将消息订阅类型更改为“Windows”类型而不是“NDEF”类型,允许邻近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.
private void SubscribeForMessage()
{
Id=proximityDevice.SubscribeFormMessage(“WindowsMime”,messageReceived);
}
收到私有无效消息(ProximityDevice发送方,ProximityMessage消息)
{
var buffer=message.Data.ToArray();
int-mimesize=0;
//搜索第一个“\0”字符
用于(mimesize=0;mimesize<256&&buffer[mimesize]!=0;++mimesize)
{
};
//提取模版
var messageType=Encoding.UTF8.GetString(缓冲区,0,模拟);
//将数据转换为字符串。这取决于mimetype值。
var scanned_message=Encoding.UTF8.GetString(buffer,256,buffer.Length-256);
Dispatcher.BeginInvoke(()=>
{
如果(proximityDevice!=null)
{
proximityDevice.StopSubscribingFormMessage(Id);
locationdisplay.Text=已扫描的信息;
}
});
}
//为了让代码正常工作,我添加了
//使用System.Runtime.InteropServices.WindowsRuntime;
//用于访问ToArray()和AsBuffer()
//函数分别进行读/写操作。

我想做同样的事情。但在我的代码message.DataAsString中为空。知道吗?