简体   繁体   English

Arduino NFC 读取 NDEF 消息

[英]Arduino NFC Reading NDEF Message

I am trying to read an ndef message from arduino to arduino.我正在尝试读取从 arduino 到 arduino 的 ndef 消息。 When i try to read that ndef message from an android phone it reads well.当我尝试从 Android 手机读取该 ndef 消息时,它读起来很好。 But when i read with an arduino an error occurs like:但是当我用 arduino 阅读时,会出现如下错误:

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:我使用 pn532 和我的 arduino 代码:

#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.谢谢。

Obtain information from a smartphone The program below is used to obtain information from an Android application.从智能手机获取信息 下面的程序用于从 Android 应用程序获取信息。

To make it work, you must first activate the NFC functionality and the Android Beam data exchange.要使其工作,您必须首先激活 NFC 功能和 Android Beam 数据交换。

Then, bring the smartphone to the NFC shield ...然后,将智能手机带到 NFC 盾牌...

#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/阅读更多: https : //arduino.blaisepascal.fr/nfc/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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