无法解码 NFC 消息

Cannot Decode NFC Message

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

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

存在解析错误

当我尝试在标签中使用 ID (int) 时,当我尝试解析它时出现错误 "Input string was not in the correct format" 现在我确定它必须是编码/解码 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;
        }
    }

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