Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No event firing when NFC is scanned - Android #111

Open
iaps opened this issue Jun 1, 2022 · 11 comments
Open

No event firing when NFC is scanned - Android #111

iaps opened this issue Jun 1, 2022 · 11 comments
Labels
need-info Extra information is needed

Comments

@iaps
Copy link

iaps commented Jun 1, 2022

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
Copy link
Author

iaps commented Jun 3, 2022

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
Copy link

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
Copy link
Contributor

saamerm commented Jun 13, 2022

@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
Copy link
Author

iaps commented Aug 15, 2022

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
Copy link
Owner

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

@franckbour franckbour added the need-info Extra information is needed label Aug 20, 2022
@saamerm
Copy link
Contributor

saamerm commented Aug 20, 2022

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
Copy link

bptf commented Aug 21, 2022

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
Copy link

bptf commented Aug 21, 2022

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
Copy link
Contributor

saamerm commented Aug 22, 2022

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

@bptf-iaps
Copy link

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
Copy link

Mitarano commented Sep 4, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
need-info Extra information is needed
Projects
None yet
Development

No branches or pull requests

7 participants