Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NFCTagReader iOS Not hitting didDetect method

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);
                });
            });


        }




    }
like image 330
Matt Avatar asked Jan 30 '18 23:01

Matt


1 Answers

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.

like image 197
Matt Avatar answered Nov 29 '22 13:11

Matt