NFCTagReader iOS Not hitting didDetect method
Asked Answered
G

1

9

Im doing a POC of implementing the NFCTagReader into a xamarin.ios app.

https://developer.xamarin.com/samples/monotouch/ios11/NFCTagReader/

I've taken the NFCTagReader from the xamarin site and set all the appropriate provision settings to get access to the tag reader. The problem is that when i click scan the "Ready to Scan" window pops up as expected then i scan a tag and it shows the little tick on the screen to show that it found but it never breaks into the DidDetect method of my code in the delegate. It will hit the DidInvalidate method and give the code for ReaderSessionInvalidationErrorUserCanceled.

Any ideas what i'm missing. Following is my code snippet:

 partial void Scan(UIBarButtonItem sender)
    {
        InvokeOnMainThread(() =>
        {                
            Session = new NFCNdefReaderSession(this, null, true);
            if (Session != null)
            {
                Session.BeginSession();
            }
        });

    }

    #endregion        

    #region NFCNDEFReaderSessionDelegate

    public void DidDetect(NFCNdefReaderSession session, NFCNdefMessage[] messages)
    {

        foreach (NFCNdefMessage msg in messages)
        {
            DetectedMessages.Add(msg);
        }
        DispatchQueue.MainQueue.DispatchAsync(() =>
        {
            this.TableView.ReloadData();
        });

    }


    public void DidInvalidate(NFCNdefReaderSession session, NSError error)
    {

        var readerError = (NFCReaderError)(long)error.Code;

        if (readerError != NFCReaderError.ReaderSessionInvalidationErrorFirstNDEFTagRead &&
            readerError != NFCReaderError.ReaderSessionInvalidationErrorUserCanceled)
        {
            InvokeOnMainThread(() =>
            {
                var alertController = UIAlertController.Create("Session Invalidated", error.LocalizedDescription, UIAlertControllerStyle.Alert);
                alertController.AddAction(UIAlertAction.Create("Ok", UIAlertActionStyle.Default, null));
                DispatchQueue.MainQueue.DispatchAsync(() =>
                {
                    this.PresentViewController(alertController, true, null);
                });
            });


        }




    }
Gide answered 30/1, 2018 at 23:19 Comment(2)
What device are you using because it requires an iPhone 7, 7 Plus, 8, 8 Plus, or X running iOS 11? Also, I'd check the Xamarin docs one more time to make sure your testing setup fits within all the restrictions and the configuration is correct.Avent
Do you have found a solution? In my case it shows also the little tick like it worked, but then it never hits either of didinvalidate or diddetect.Persson
G
2

Little bit of a blonde moment and was just going to close this question but thought would answer this just in case someone else comes across the same problem as I did.

The problem ended up being caused by the fact that the tags that I was given were blank. Therefore the phone would click when it detected the tag but would never hit the didDetect method. As soon as I wrote something to the NFC tag with an Android tag writer app then DidDetect fired as expected.

Gide answered 7/2, 2018 at 23:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.