chariotsolutions / phonegap-nfc

PhoneGap NFC Plugin
MIT License
706 stars 557 forks source link

I am not able to retrieve NFC tags #247

Closed jwillmer closed 7 years ago

jwillmer commented 7 years ago

This function:

nfc.addTagDiscoveredListener(function (nfcEvent) {
    console.log("discovered");
    console.log(JSON.stringify(nfcEvent));
}, function () {
    console.log("Discovering NFC Messages.");
},
function (error) {
    console.log("Failed to discover NDEF.");
});

outputs this:

{"isTrusted":false,"tag":{"id":[91,7,-33,-4],"techTypes":["android.nfc.tech.NfcA","android.nfc.tech.MifareClassic","android.nfc.tech.NdefFormatable"]}}

but the listener never fires a event:

nfc.addNdefListener(function (nfcEvent) {
    console.log("listener");
    console.log(JSON.stringify(nfcEvent));
}, function () {
    console.log("Listening for NFC Messages.");
},
function (error) {
    console.log("Failed to add NDEF listener.");
});

I tried to use addMimeTypeListener with one of this MIME types (android.nfc.tech.NfcA,android.nfc.tech.MifareClassic,android.nfc.tech.NdefFormatable) but I also don't get a result.

My test NFC card contains a number (plain text). Something like: 123456789

don commented 7 years ago

What phone are you using?

Based on the output above, you're scanning a Mifare Classic tag. Newer phones with Broadcom NFC chips don't support Mifare Classic tags for NDEF.

jwillmer commented 7 years ago

@don I use a Xiaomi Mi 5 but I cannot find any specs that tell me which NFC chip it has :(

I used this NFC reader from the Play Store and it outputs my number so I guess it is not a problem of the chipset.

don commented 7 years ago

What version of Android is on your phone?

jwillmer commented 7 years ago

Android Version 6.0

don commented 7 years ago

Maybe it's a permission problem. I haven't updated this plugin for Android 6 permission yet. Can you edit config.xml and target Android-22?

<platform name="android">
    <allow-intent href="market:*" />
    <preference name="android-targetSdkVersion" value="22" />
</platform>
jwillmer commented 7 years ago

If I change it in the project config.xml

 <platform name="android">
    <allow-intent href="market:*" />
    <preference name="android-targetSdkVersion" value="22" />
  </platform>
  <platform name="ios">
     ...

I get the following error: Failure [INSTALL_FAILED_PERMISSION_MODEL_DOWNGRADE]

I fixed it by deinstalling the app from the phone and adding the target API level via the VisualStudio configuration form. It is now placed at the end of the config file:

  ...
  <preference name="android-targetSdkVersion" value="22" />
</widget>

But the result of the readings does not change.

don commented 7 years ago

That error means you need to delete the app from your phone before installing the new one.

jwillmer commented 7 years ago

FYI this reader on GitHub by @nadam is available in the Play Store and can display the value of the NFC card maybe he can help?

jwillmer commented 7 years ago

Maybe your are missing references to Mifare? I found this question on StackOverflow about getting all NFC types.

@nadams source file: https://github.com/nadam/nfc-reader/blob/master/src/se/anyro/nfc_reader/TagViewer.java#L37-L38

don commented 7 years ago

That Java code has worked with Mifare for a long time. It's unlikely that is the problem.

What happened after you installed the android-22 version of the app? Attach more logs from adb if it's still failing.

don commented 7 years ago

@jwillmer try this sample project https://github.com/don/phonegap-nfc-issue-247

For phones that support Mifare Classic you'll get NDEF data. For other phones like Nexus 4 or Samsung S4 that don't support Mifare Classic, you'll get some tag metadata. Both screenshots below are from the same app scanning the same MifareClassic NFC tag.

Nexus 5X

nexus5x

Samsung S4

samsungs4

jwillmer commented 7 years ago

I tried your solution and the output is this; screenshot_2016-07-27-10-54-35_com example phonegap nfc issue247

What I don't undeerstand is why another NFC app can display the result/number (4242474843): screenshot_2016-07-27-10-57-24_se anyro nfc_reader

jwillmer commented 7 years ago

That Java code has worked with Mifare for a long time. It's unlikely that is the problem.

I don't say your code is bugy but your code might not cover all cases. I am not a Java programer but for me it looks like that the code of @nadam gets more information out of the tags. Wdyt?

don commented 7 years ago

According to your screenshot, your tag is NdefFormatable, meaning there's no NDEF message on the tag.

The nfc.addNdefListener will only be called when there's an NDEF message on the tag. You need to use nfc.addNdefFormatableListener or nfc.addTagDiscoveredListener to get an event for non-NDEF tags.

Use NXP TagInfo to view the data on the tag.

I updated the demo app to write a message to the tag when it sees a formatable tag. https://github.com/don/phonegap-nfc-issue-247/commit/441314813ed21a91105de338b0f21b5519953884

Alternatively you can write at text record to the tag using NXP TagWriter.

jwillmer commented 7 years ago

O.K. but there is still a number on the card I need to read and I am not able to get it with that plugin. Is that intended? For me it is a problem because my app needs to read this cards - not format them.

I used the NXP TagWriter and found out that my number is in the first sector of the memory. Is there a possibillity to get this number with the NFC plugin?

screenshot_2016-07-28-09-35-35_com nxp taginfolite

jwillmer commented 7 years ago

I was blind. I found the Id in the result. I needed to format the id to hex then I have it (reversed).

image

jwillmer commented 7 years ago

@don Thank you for your help, you kept me going :+1:

don commented 7 years ago

You're welcome. Since you only need the ID, you can use the tagDiscoveredListener in your app. It should find any tag.

riebschlager commented 7 years ago

I'm having a similar issue. I am using tagDiscoveredListener and it never seems to be fired. The tablet makes a sound like it is reading successfully, but the tagDiscoveredListener callback is never called. The tags I'm trying to scan are MifareUltralight. My android version is 4.4.4.

Any idea what might be happening?

jwillmer commented 7 years ago

@riebschlager can you open another issue? This one is closed.