franckbour / Plugin.NFC

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

Leave "NDEF not supported" evaluation to the user in iOS #64

Closed f-richter closed 3 years ago

f-richter commented 3 years ago

Description

With iOS, when reading a tag without any data on it (like factory new) the plugin will throw the NFCErrorNotSupportedTag message ("Tag is not supported"), because there is no NDEF data and therefore NFCNdefStatus is "NotSupported". But what if I am not interested in the NDEF data and just want to read the serial number of the tag? What if I have tags and just want to identify them via this unique id. There is no chance to get this information. With "NFC Tools" it is also possible to read the serial number of tags without NDEF data.

Expected Behavior

Because the "TagInfo" class already has an "IsSupported" flag where the NDEF status is represented as I understand, I would expect to leave the decision to the plugin user whether to throw errors if NDEF is not supported. With that the user has the chance to read the serial number.

I would suggest to enhance the method DidDetectTags(NFCTagReaderSession session, INFCTag[] tags) in NFC.iOS.cs like this

ndefTag.QueryNdefStatus((status, capacity, error) =>
{
    if (error != null)
    {
        Invalidate(session, error.LocalizedDescription);
        return;
    }

    var isNdefSupported = status != NFCNdefStatus.NotSupported;

    var identifier = GetTagIdentifier(ndefTag);
    var nTag = new TagInfo(identifier, isNdefSupported)
    {
        IsWritable = status == NFCNdefStatus.ReadWrite,
        Capacity = Convert.ToInt32(capacity)
    };

    if (!isNdefSupported)
    {
        // if ndef is not supported do not read or write
        // let the plugin user decide if ndef support is needed
        OnMessageReceived?.Invoke(nTag);
        Invalidate(session);
        return;
    }

    if (_isWriting)
    {
    ...

Actual Behaviour

How it is right now:

ndefTag.QueryNdefStatus((status, capacity, error) =>
{
    if (error != null)
    {
        Invalidate(session, error.LocalizedDescription);
        return;
    }

    if (status == NFCNdefStatus.NotSupported)
    {
        Invalidate(session, Configuration.Messages.NFCErrorNotSupportedTag);
        return;
    }

    var identifier = GetTagIdentifier(ndefTag);
    var nTag = new TagInfo(identifier)
    {
        IsWritable = status == NFCNdefStatus.ReadWrite,
        Capacity = Convert.ToInt32(capacity)
    };

    if (_isWriting)
    {
        ...

Basic Information

franckbour commented 3 years ago

I thought that I fixed this but apparently not...