justsomeoneelse
justsomeoneelse

Reputation: 49

Arduino NFC Reading NDEF Message

I am trying to read an ndef message from arduino to arduino. When i try to read that ndef message from an android phone it reads well. But when i read with an arduino an error occurs like:

Unknown TLV 1
Error. Can't decode message length.
NFC Tag - ERROR
UID 08 11 22 43

No NDEF Message

I use pn532 and my arduino code:

#include <SPI.h>
#include <PN532_SPI.h>
#include <PN532.h>
#include <NfcAdapter.h>

PN532_SPI pn532spi(SPI, 10);
NfcAdapter nfc = NfcAdapter(pn532spi);

void setup(void) {
    Serial.begin(9600);
    Serial.println("NDEF Reader");
    nfc.begin();
}

void loop(void) {
    Serial.println("\nScan a NFC tag\n");
    if (nfc.tagPresent())
    {
        NfcTag tag = nfc.read();
        tag.print();
    }
    delay(1000);
}

What is the problem? Thank you.

Upvotes: 2

Views: 1412

Answers (1)

Bolt UIX
Bolt UIX

Reputation: 7052

Obtain information from a smartphone The program below is used to obtain information from an Android application.

To make it work, you must first activate the NFC functionality and the Android Beam data exchange.

Then, bring the smartphone to the NFC shield ...

#include "SPI.h"
#include "PN532_SPI.h"
#include "snep.h"
#include "NdefMessage.h"
 
PN532_SPI pn532spi(SPI, 10);
SNEP nfc(pn532spi);
uint8_t ndefBuf[128];
uint8_t recordBuf[128];
 
void setup()
{
    Serial.begin(115200);
}
 
void loop()
{
    Serial.println("Attente d'un message d'un Android");
    int msgSize = nfc.read(ndefBuf, sizeof(ndefBuf));
    if (msgSize > 0) {
        NdefMessage msg  = NdefMessage(ndefBuf, msgSize);
        Serial.println("\nSucces");
        NdefRecord record = msg.getRecord(0);
        int recordLength = record.getPayloadLength();
        if (recordLength <= sizeof(recordBuf)) {
            record.getPayload(recordBuf);
            Serial.write(recordBuf, recordLength);
            for (int i = 0; i < 5; i++) {
                Serial.write('\n');
            }
            delay(2000);
        }
    } else {
        Serial.println("Echec");
    }
    delay(1000);
}

Can you try this solution & let me know.

read more : https://arduino.blaisepascal.fr/nfc/

Upvotes: 1

Related Questions