Open iaps opened 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".
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();
...
}
@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
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 I agree with @saamerm , we need more info. Can you share your code or provide a sample to reproduce the issue?
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?
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.
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.
On your Galaxy Note 10+ did you have Developer Options and NFC turned on in the Settings?
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.
I have the same issue. For debugging I use the Xiaomi Redmi Note 10 5G (M2103K19G). https://github.com/Mitarano/Scan_Share/tree/master/
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:
MainActivity has CrossNFC.Init(this); in OnCreate, CrossNFC.OnResume(); in OnResume, and CrossNFC.OnNewIntent(intent); in OnNewIntent.
Steps to Reproduce
Expected Behavior
Actual Behavior
Basic Information
Screenshots
Reproduction Link