Giter Site home page Giter Site logo

Comments (8)

franckbour avatar franckbour commented on August 15, 2024 2

You probably need to unsubscribe and stop listening in the main thread like this:

Device.BeginInvokeOnMainThread(() =>
{
    CrossNFC.Current.OnMessageReceived -= Current_OnMessageReceived;
    CrossNFC.Current.StopListening();
});

from plugin.nfc.

kberawala avatar kberawala commented on August 15, 2024 2

You probably need to unsubscribe and stop listening in the main thread like this:

Device.BeginInvokeOnMainThread(() =>
{
    CrossNFC.Current.OnMessageReceived -= Current_OnMessageReceived;
    CrossNFC.Current.StopListening();
});

It worked. Thanks

from plugin.nfc.

nanorie avatar nanorie commented on August 15, 2024 1

instead of stop listening directly in Current_OnMessageReceived, i call it in another task not waited

private async void Current_OnMessageReceived(ITagInfo tagInfo)
{
   // do stuff
   await Task.Run(() => StopListening()).ConfigureAwait(false);
}
private async Task StopListening()
{
   try
   {
      MainThread.BeginInvokeOnMainThread(() =>
      {
         CrossNFC.Current.StopListening();
         CrossNFC.Current.OnMessageReceived -= Current_OnMessageReceived;
      });
   }
   catch (Exception ex)
   {
   }
}

All this code is in a service and works for me on android and Maui (but I don't understand the underlying mechanism that makes it work)
Hope it can help someone

from plugin.nfc.

pvanroos avatar pvanroos commented on August 15, 2024

I added the above code at the end of the 'Current_OnMessageReceived' event handler and I'm still getting the error: You must disable foreground dispatching while your activity is still resumed

from plugin.nfc.

kberawala avatar kberawala commented on August 15, 2024

I added the above code at the end of the 'Current_OnMessageReceived' event handler and I'm still getting the error: You must disable foreground dispatching while your activity is still resumed

check if your page is => var page = new navigationpage(new scanpage());
and not var page = new scanpage(); (this didn't work for me)

from plugin.nfc.

saamerm avatar saamerm commented on August 15, 2024

This solution worked for me too. @pvanroos were you able to figure out your issue?

from plugin.nfc.

pvanroos avatar pvanroos commented on August 15, 2024

Yes, this helped. I make sure that events are subscribed and unsubscribed cleanly when loading a page or exiting a page where scanning is supposed to happen too.

Also, (off topic), I still wish I knew what causes the 'Stack Error' message on iOS. Once in a while, my users will see this message when attempting to scan an NFC tag. I think it's caused when they don't hold the phone still to allow the data transfer to execute cleanly. I wish we could hook an event handler into this error and handle it gracefully.

from plugin.nfc.

 avatar commented on August 15, 2024

I added the above code at the end of the 'Current_OnMessageReceived' event handler and I'm still getting the error: You must disable foreground dispatching while your activity is still resumed

check if your page is => var page = new navigationpage(new scanpage()); and not var page = new scanpage(); (this didn't work for me)

scan worked on my homepage but not certain pages, however it worked on a page which used
"await Navigation.PushAsync(new pageName());" but now that i've changed my way to navigate , it works on every pages.

do we know why this way works and not the other?

from plugin.nfc.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.