capawesome-team / capacitor-nfc

⚡️ Capacitor plugin for reading and writing NFC tags.
https://capawesome.io/plugins/nfc/
MIT License
74 stars 14 forks source link

bug: Can't read ISO14443-3A (Mifare Classic) on iOS #14

Closed mrrrk closed 1 year ago

mrrrk commented 1 year ago

Plugin version: "@capawesome-team/capacitor-nfc": "^0.3.1",

Platform(s): iPhone 8, iOS 16.4

(I do not have access to other iOS platforms to test NFC with)

Current behavior: "nfcTagScanned" handler will not fire.

Expected behavior: nfcTagScanned should fire when device is next to NFC tag.

Steps to reproduce:

Related code:

    export function addListeners() {
        console.log("+++ SCAN - add listener");
        Nfc.addListener('nfcTagScanned', (event) => {
            console.log("+++ SCANNED tag = ", JSON.stringify( event?.nfcTag));
            const idHex = event?.nfcTag?.id?.map(n => utils.TwosComplementHex(n)).join("-") ?? "";
            console.log("+++ SCANNED idHex = ", idHex);
            eventBus.$emit("nfc-tag-detected", idHex);
        });

        Nfc.addListener('scanSessionCanceled', () => {
            console.log('~~~~ scanSessionCanceled');
        });

        Nfc.addListener('scanSessionError', event => {
            console.log('~~~~ scanSessionError', { event });
        });
    }
    export async function startScanning() {
        await Nfc.startScanSession({ 
            alertMessage: "Approach the touch pad",
            pollingOptions: [ PollingOption.iso14443, PollingOption.iso15693 ],
         });
    }

Other information:

Works well on Android. Code runs without errors on iOS and app shows "Ready to Scan" panel. Cancel event fires if cancelled - but I am unable to detect a scanned tag. "nfcTagScanned" handler will not fire.

I have looked at the code in the demo app and read the docs but am unable to see anything I'm missing.

The card/tag scans successfully using NFCTools app.

Type of card is ISO 14443-3A.

Do you have any ideas what I can try next?

Capacitor doctor:

💊   Capacitor Doctor  💊 

Latest Dependencies:

  @capacitor/cli: 4.7.3
  @capacitor/core: 4.7.3
  @capacitor/android: 4.7.3
  @capacitor/ios: 4.7.3

Installed Dependencies:

  @capacitor/cli: 4.7.3
  @capacitor/android: 4.7.3
  @capacitor/core: 4.7.3
  @capacitor/ios: 4.7.3

[success] iOS looking great! 👌
[error] app/src/main/assets directory is missing in android
robingenz commented 1 year ago

Thank you for creating this detailed issue. Please try to reproduce the issue with our NFC Demo App. If you are able to reproduce the issue with this app, please copy/paste the whole XCode log and share a screen recording. Please also share a screenshot of the NFC Tools app with your scanned tag, so that I have the tag details.

mrrrk commented 1 year ago

Hi Robin. Thanks for the quick reply. I can confirm that I’m seeing the same problem with the demo app.

I've included a photo of the card being scanned by the demo app rather than a screen recording since it gives as much information :-) I have included screenshots from both iOS and Android versions of NFC tools because the Android version seems to show quite a bit more data. I wonder if there is a difficulty with this type of card and the iPhone?

Cheers

Mark

robingenz commented 1 year ago

...The files can be accessed here, in case the attachment in the email reply did not make it through: http://mrrrk.net/nfcbugreport.zip

:-)

Please upload the images directly to GitHub (just copy/paste in comment field) so that people with similar problems don't all have to download and unzip the file first.

robingenz commented 1 year ago

Please also copy/paste the XCode log.

mrrrk commented 1 year ago

demo-running

android-nfc-tools

ios-nfc-tools

demo-xcode-output.txt

(as you can probably tell, I don't post many of these!)

mrrrk commented 1 year ago

XCode log as not-an-attachment:

2023-04-06 07:04:14.377116+0100 App[557:20866] DiskCookieStorage changing policy from 2 to 0, cookie file: file:///private/var/mobile/Containers/Data/Application/B9478645-1218-4660-863D-5D757496C8AE/Library/Cookies/Cookies.binarycookies 2023-04-06 07:04:14.614337+0100 App[557:20866] KeyboardPlugin: resize mode - native ⚡️ Loading app at capacitor://localhost... ⚡️ To Native -> StatusBar setStyle 43246804 ⚡️ TO JS {} ⚡️ WebView loaded ⚡️ To Native -> App addListener 43246805 ⚡️ To Native -> Nfc removeAllListeners 43246806 ⚡️ TO JS undefined ⚡️ To Native -> Nfc addListener 43246807 ⚡️ To Native -> Nfc addListener 43246808 ⚡️ To Native -> Nfc addListener 43246809 ⚡️ To Native -> Nfc isSupported 43246810 ⚡️ TO JS {"isSupported":true} ⚡️ To Native -> Nfc startScanSession 43246811 ⚡️ TO JS undefined ⚡️ TO JS {"message":"Session timeout"} ⚡️ [log] - scanSessionError {"event":{"message":"Session timeout"}}

mrrrk commented 1 year ago

More information: I'm seeing a few suggestions that MIFARE CLASSIC cards are not supported in CoreNFC. Is this the case as far as you know?

https://developer.apple.com/forums/thread/133179 https://stackoverflow.com/questions/56453525/missing-required-entitlement-for-nfctagreadersession https://stackoverflow.com/questions/44572827/detect-iso-iec-14443-mifare-ultralight-or-classic-nfc-cards-with-corenfc

robingenz commented 1 year ago

Thank you for all the information. I will try to reproduce the issue this week and get back to you.

mrrrk commented 1 year ago

Thanks. In the meantime, I've ordered some cards from a different manufacturer which are scheduled to arrive by Tuesday. If they work, I'll cancel this report.

robingenz commented 1 year ago

I just tested this MIFARE Classic 1k tag with an iPhone 13 and was able to reproduce your problem. Unfortunately, I could not read the tag even with the NFC Tools app, neither in normal mode nor in compatibility mode. I assume based on your iOS screenshot and this post in the Apple Developer Forum that MIFARE Classic 1k tags are not supported by iOS or, depending on the tag, only partially supported.

Is there a reason why you use exactly this card and not, for example, an NTAG215 card?

mrrrk commented 1 year ago

Hi Robin.

I think we have come to similar conclusions - see earlier messages. I've got some some NTAG215 cards on order, which should arrive today. I'll try one of those out and if successful, I'll cancel this issue.

Thanks for looking into it.

mrrrk commented 1 year ago

...NTAG215 cards work with Demo App but still no luck with my own app. Looks like I need to do some debugging.

mrrrk commented 1 year ago

...ok, it turns out my code does work but I couldn't figure out why the iOS scan dialogue would not dismiss after it had collected a scan. I had not understood that you need to call .stopScanSession! (I'm not too familar with Angular or RxJS but I figured this out from the Demo App code.)

I suppose you'd know this if you were familiar with CoreNFC but many using a Capacitor plugin will not be (like me!) I wonder if this might be worth an addition to readme.md? :-)

robingenz commented 1 year ago

I'm glad it works now and thank you for your feedback. I've just updated the documentation.