franckbour / Plugin.NFC

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

No event firing when NFC is scanned - Android #111

Open iaps opened 2 years ago

iaps commented 2 years ago

Description

I have copied the code setup as described. The following are all true: CrossNFC.IsSupported, CrossNFC.Current.IsAvailable, CrossNFC.Current.IsEnabled, CrossNFC.Current.IsWritingTagSupported All events subscribed in OnAppearing: CrossNFC.Legacy = false; // Event raised when a ndef message is received. CrossNFC.Current.OnMessageReceived += Current_OnMessageReceived; // Event raised when a ndef message has been published. CrossNFC.Current.OnMessagePublished += Current_OnMessagePublished; // Event raised when a tag is discovered. Used for publishing. CrossNFC.Current.OnTagDiscovered += Current_OnTagDiscovered; // Event raised when NFC listener status changed CrossNFC.Current.OnTagListeningStatusChanged += Current_OnTagListeningStatusChanged; // Android CrossNFC.Current.OnNfcStatusChanged += Current_OnNfcStatusChanged;

Listening: CrossNFC.Current.StartListening();

Perms are set:

Screenshot 2022-06-01 125402

MainActivity has CrossNFC.Init(this); in OnCreate, CrossNFC.OnResume(); in OnResume, and CrossNFC.OnNewIntent(intent); in OnNewIntent.

Steps to Reproduce

  1. Scan NFC card

Expected Behavior

  1. Listening status changes to enabled
  2. OnTagListeningStatusChanged fires
  3. Scan NFC card
  4. OnTagDiscovered fires

Actual Behavior

  1. Listening status changes to enabled
  2. OnTagListeningStatusChanged fires
  3. Scan NFC card
  4. Nothing happens

Basic Information

Screenshots

Reproduction Link

iaps commented 2 years ago

Furthermore, the NFC card I'm scanning is empty. When I scan it with my phone it says "New tag scanned" and "Empty tag". Is it possible that the issue is due to the card not being formatted a certain way?

I also gave my friend the project code and he tried it on his phone and it successfully scanned a previously encoded card, receiving event "Current_OnMessageReceived".

wfahnestock commented 2 years ago

I was just dealing with this issue as well.

The way I solved it was to put CrossNFC.Current.StartListening() in my page constructor, and then subscribe to the event in OnAppearing, for example:

public MyPage() {
    InitializeComponent();

    CrossNFC.Current.StartListening();
}

protected override void OnAppearing() {
    CrossNFC.Legacy = false;
    CrossNFC.Current.OnMessageReceived += Current_OnMessageReceived;
    ...
}

private void Current_OnMessageReceived(ITagInfo tagInfo) {
    ...
}

Worth noting, I ran into this issue while trying to solve a different problem of cards being scanned multiple times when held to the back of the phone. If you run into this issue, the way I solved it was to make sure to stop CrossNFC from listening to scans in your pages OnDisappearing, or after you're done scanning the card if you don't have a dedicated page for scanning:

protected override void OnDisappearing() {
    CrossNFC.Current.StopListening();
    ...
}
saamerm commented 2 years ago

@iaps can you please try the solution above or better format and share more of your code. It’s not enough to debug the issue

iaps commented 2 years ago

Thanks @wfahnestock. I put CrossNFC.Current.StartListening() in my ContentPage constructor, but when launched it said: System.NullReferenceException: 'Object reference not set to an instance of an object.'

I should note that I tried with an older phone and everything worked as it should (NFC reader read the card). But on my Galaxy Note10+ the card wouldn't read or be recognized within the app; NFC cards are able to be read with the app closed - on main home screen.

franckbour commented 2 years ago

@iaps I agree with @saamerm , we need more info. Can you share your code or provide a sample to reproduce the issue?

saamerm commented 2 years ago

So @iaps you’re saying that you used the example in this repository and you had the error on one device but not the other?

bptf commented 2 years ago

I'm saying that I used my original solution on a different phone, and everything worked. It doesn't work on my Galaxy Note10+

@wfahnestock solution didn't work on either device since it threw a NullReferenceException when in the constructor.

bptf commented 2 years ago

So @iaps you’re saying that you used the example in this repository and you had the error on one device but not the other?

Exactly right.

saamerm commented 2 years ago

On your Galaxy Note 10+ did you have Developer Options and NFC turned on in the Settings?

bptf-iaps commented 2 years ago

On your Galaxy Note 10+ did you have Developer Options and NFC turned on in the Settings?

I do indeed. I can scan an NFC when the app is closed, and since I'm using my phone to debug, developer options is enabled.

Mitarano commented 2 years ago

I have the same issue. For debugging I use the Xiaomi Redmi Note 10 5G (M2103K19G). https://github.com/Mitarano/Scan_Share/tree/master/