Plugin.NFC icon indicating copy to clipboard operation
Plugin.NFC copied to clipboard

No event firing when NFC is scanned - Android

Open iaps opened this issue 3 years ago • 11 comments

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

  • Version with issue: 0.1.22
  • Last known good version: n/a
  • IDE: VS Community 2022
  • Platform Target Frameworks:
    • iOS: n/a
    • Android: Minimum 5.0, Target 12.0
    • UWP: n/a
  • Nuget Packages: NETStandard.Library, Newtonsoft.Json, Plugin.NFC, Xmarin.Essentials, Xamarin.Forms, ZXing.Net.Mobile, ZXing.Net.Mobile.Forms
  • Affected Devices: Samsung Galaxy Note10+

Screenshots

Reproduction Link

iaps avatar Jun 01 '22 16:06 iaps

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".

iaps avatar Jun 03 '22 19:06 iaps

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

wfahnestock avatar Jun 13 '22 19:06 wfahnestock

@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

saamerm avatar Jun 13 '22 23:06 saamerm

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.

iaps avatar Aug 15 '22 16:08 iaps

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

franckbour avatar Aug 20 '22 17:08 franckbour

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?

saamerm avatar Aug 20 '22 17:08 saamerm

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 avatar Aug 21 '22 15:08 bptf

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.

bptf avatar Aug 21 '22 15:08 bptf

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

saamerm avatar Aug 22 '22 03:08 saamerm

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.

bptf-iaps avatar Aug 22 '22 06:08 bptf-iaps

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

Mitarano avatar Sep 04 '22 14:09 Mitarano