franckbour / Plugin.NFC

A Cross-Platform NFC (Near Field Communication) plugin to easily read and write NFC tags in your application.
MIT License
237 stars 68 forks source link

How to solve Tag is not NDEF-compliant? #134

Open ThomasCarltonInPerson opened 1 year ago

ThomasCarltonInPerson commented 1 year ago

Hello community,

I'm using Plugin.NFC in a .Net Maui 7 app.

I'm trying to write some NFC cards using the provided code on GitHub.

The code works properly, but Whenever I use a new card, when trying to write something on it, I'm getting the error : Tag is not NDEF-compliant.

Here is my code :

async Task Publish()
{               
    try
    {     
        CrossNFC.Current.StartPublishing(true);
    }
    catch (Exception ex)
    {
        await RaiseError(ex.Message);
    }
}

async void Current_OnTagDiscovered(ITagInfo tagInfo, bool format)
{
    if (!CrossNFC.Current.IsWritingTagSupported)
    {
        await RaiseMessage_("Writing cards is not supported by your device");
        return;
    }

    try
    {
        NFCNdefRecord record;

        record = new NFCNdefRecord
        {
            TypeFormat = NFCNdefTypeFormat.Mime,
            MimeType = "application/com.companyname.nfcsample",
            Payload = NFCUtils.EncodeToByteArray("Hithere");
        };

        tagInfo.Records = new[] { record };            
        CrossNFC.Current.PublishMessage(tagInfo, false);

    }
    catch (Exception Ex)
    {
        await RaiseError("Writing failed\n" + Ex.Message);            
    }
}

I also noticed that this happens only with new cards. If I take a card and I write something on it using NFC Tools app, then I'm able to write on it using my code. It becomes fully usable on my app.

It seems like there is something I'm missing on the "initialization" of the card.

Does anyone know how to solve that please ?

Thanks.

alpsware commented 1 year ago

That is because this plugin is expecting the tag to be already NDEF formatted. If you also want to format non NDEF tags with this plugin, then you should probably have a look here

Gekidoku commented 1 year ago

Its a few months later but i have the same issue, Starting a Journey in #144.

I have Security companies as customer and they use NFC tags for their security rounds, both classic and ultralight, although we try to recommend NTAG213 chips. You can imagine they stick around 20-100 chips in a building depending on the size and they dont want to first format them all and then make them "iOS compliant" (setting the serial key in the ndef message so older ios devices can access it).