首页 > 解决方案 > 无法解码 NFC 消息

问题描述

我正在开发一个 Xamarin Forms 应用程序,它打开一个视图,其中视图模型由使用 Plugins.NFC 包通过 NFC 标签传输的数据确定。我具有将 ID 编码到标签以及读取标签的功能。我的问题是,似乎在读取 NFC 标签时,我无法对结果字符串做任何事情,因为我认为它没有被正确解码而引发错误。

我尝试了两种不同的方法来构建 NFC 芯片上的数据,例如 JSON 对象以及对象的 ID。奇怪的是,当我在读取标签后设置断点时,字符串看起来很好并显示出来,当我尝试在标签中使用 JSON 时,JsonLint 甚至响应 JSON 有效。一旦我开始以任何方式使用标签内容的字符串,就会引发错误。当我尝试在标签中使用 JSON 并且我将对其进行反序列化时,我会收到一条错误消息,指出在第 0 行位置 0 处存在解析错误。

当我尝试在标签中使用 ID (int) 时,当我尝试解析它时,我收到错误“输入字符串的格式不正确”现在我确定它必须与编码/解码有关NFC 标签,因为如果我使用应用程序向标签写入内容,它可以完美运行,但是当我在我的应用程序中写入标签时,就会出现问题。

这是读取标签的功能。请注意注释代码作为先前的解码尝试。

async void Current_OnMessageReceived(ITagInfo tagInfo)
    {
        if (tagInfo == null)
        {
            await ShowAlert("No tag found");
            return;
        }

        // Customized serial number
        var identifier = tagInfo.Identifier;
        var serialNumber = NFCUtils.ByteArrayToHexString(identifier, ":");
        var title = !string.IsNullOrWhiteSpace(serialNumber) ? $"Tag [{serialNumber}]" : "Tag Info";

        if (!tagInfo.IsSupported)
        {
            await ShowAlert("Unsupported tag (app)", title);
        }
        else if (tagInfo.IsEmpty)
        {
            await ShowAlert("Empty tag", title);
        }
        else
        {
            IDatabaseManager db = TinyIoCContainer.Current.Resolve<IDatabaseManager>() as IDatabaseManager;
            //var first = tagInfo.Records[0];
            //string msg = first.Message;
            //string msgHex = NFCUtils.ByteArrayToHexString(tagInfo.Records[0].Payload);
            //string msg = Convert.ToString(msgHex);
            //string msg = NFCUtils.GetMessage(tagInfo.Records[0]);
            string msg = NFCUtils.GetMessage(tagInfo.Records[0]);
            try
            { 
                //tring messageEncoded = Encoding.UTF8.GetString(tagInfo.Records[0].Payload);
                int cardId = int.Parse(msg);
                var card = await db.GetCard(cardId);
                //Entities.Card card = JsonConvert.DeserializeObject<Entities.Card>(msg);
                var model = await CardModel.FromCard(card);
                await Application.Current.MainPage.Navigation.PushAsync(new ViewCardPage(model));
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
    }

这是写入标签的函数

void Current_OnTagDiscovered(ITagInfo tagInfo, bool format)
    {
        if (!CrossNFC.Current.IsWritingTagSupported)
        {
            return;
        }

        try
        {
            var record = new NFCNdefRecord
            {
                TypeFormat = NFCNdefTypeFormat.WellKnown,
                MimeType = "text/plain",
                Payload = NFCUtils.EncodeToByteArray(selectedCard.Card.Id.ToString())
                //Payload = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(selectedCard.Card);)
            };

            if (!format && record == null)
                throw new Exception("Record can't be null.");

            tagInfo.Records = new[] { record };

            //if (format)
            //{ 
            //  CrossNFC.Current.ClearMessage(tagInfo);
            //}

            CrossNFC.Current.PublishMessage(tagInfo, false);
        }
        catch (System.Exception ex)
        {
            //await ShowAlert(ex.Message);
            return;
        }
    }

读取标签断点命中

标签: xamarinxamarin.formsencodingnfc

解决方案


所以这似乎是 Plugin.NFC 包的问题。我发现的解决方法是,如果我使用 URI 而不是文本,它会被正确编码。


推荐阅读