简体   繁体   中英

NFC reading Android Java - null object from getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES)

I'm trying to realize a simple Android application to read from an NFC tag. I followed the official documentation ( https://developer.android.com/guide/topics/connectivity/nfc ) to actually realize an application that is almost equal to the one created by "codexpedia" -> Source code .

When a tag is near the smartphone the onNewIntent method is call, but, when I try to get data through the method getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES) , I always obtain null.

You can find the code that is causing the problem at the following direct link link

Is there anyone that knows why it happens?

NdefFormatable technology means the card is capable of storing a Ndef message once it has been formatted.

So basically there is no Ndef message on this card, once it has been formatted and a Ndef message put on it then the code will be able to read this card.

You can use Apps like NFC tools or NXP TagWriter App to Format and add an Ndef message

Once formatted it should show the NfcV, Ndef technologies.

The bug in the code https://github.com/codexpedia/android_nfc_read_write/blob/master/app/src/main/java/com/example/peng/nfcreadwrite/MainActivity.java

Line 80 - IntentFilter tagDetected = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED);

Should be IntentFilter tagDetected = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);

As it assumes that all cards have a Ndef message to read from, this is not the case for unformatted cards, bank cards, A lot of Transport cards, etc

Also line 91 to 93 - if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(action) || NfcAdapter.ACTION_TECH_DISCOVERED.equals(action) || NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)) {

Should be if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)) {

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM