0

我想将我的 PN532 用作 NFC 标签,其中包含一些我的 Android 手机应用程序可以读取和保存的附加数据。如您所见,不幸的是,android 无法识别仿真标签。我使用示例中的 emulate_tag_ndef。这是我使用的确切示例:

#include "NdefMessage.h"
#include <SPI.h>
#include <PN532_SPI.h>
#include "PN532.h"
PN532_SPI pn532spi(SPI, 10);
EmulateTag nfc(pn532spi);
uint8_t ndefBuf[120];
NdefMessage message;
int messageSize;
uint8_t uid[3] = { 0x12, 0x34, 0x56 };
void setup()
{
  Serial.begin(115200);
  Serial.println("------- Emulate Tag --------");
  message = NdefMessage();
  //message.addUriRecord("http://www.elechouse.com");
  message.addTextRecord("Hi");
  messageSize = message.getEncodedSize();
  if (messageSize > sizeof(ndefBuf)) {
      Serial.println("ndefBuf is too small");
      while (1) { }
  }  
  Serial.print("Ndef encoded message size: ");
  Serial.println(messageSize);
  message.encode(ndefBuf);  
  // comment out this command for no ndef message
  nfc.setNdefFile(ndefBuf, messageSize);  
  // uid must be 3 bytes!
  nfc.setUid(uid);  
  nfc.init();
}

void loop(){
    // uncomment for overriding ndef in case a write to this tag occured
    //nfc.setNdefFile(ndefBuf, messageSize);    
    // start emulation (blocks)
    nfc.emulate();        
    // or start emulation with timeout
    if(!nfc.emulate(1000)){ // timeout 1 second
      Serial.println("timed out");
    }    
    // deny writing to the tag
    // nfc.setTagWriteable(false);    
    if(nfc.writeOccured()){
       Serial.println("\nWrite occured !");
       uint8_t* tag_buf;
       uint16_t length;       
       nfc.getContent(&tag_buf, &length);
       NdefMessage msg = NdefMessage(tag_buf, length);
       msg.print();
    }
    delay(1000);

}

这是我的数组,它位于 emulatetag.cpp 内部

      PN532_COMMAND_TGINITASTARGET,
      0x0,                  // MODE: 0x04 = PICC only, 0x01 = Passive only (0x02 = DEP only)

      // MIFARE PARAMS
      0x08, 0x00,         // SENS_RES (seeeds studio set it to 0x04, nxp to 0x08)
      0x00, 0x00, 0x00,   // NFCID1t    (is set over sketch with setUID())
      0x60,               // SEL_RES    (0x20=Mifare DelFire, 0x60=custom)

      // FELICA PARAMS
      0x01, 0xFE,         // NFCID2t (8 bytes)
      0xA2, 0xA3, 0xA4,
      0xA5, 0xA6, 0xA7,
      0xC0, 0xC1, 0xC2,
      0xC3, 0xC4, 0xC5,
      0xC6, 0xC7, 0xFF,
      0xFF,
      0xAA, 0x99, 0x88, //NFCID3t (10 bytes)
      0x77, 0x66, 0x55, 0x44,
      0x33, 0x22, 0x11,

      0x00, // length of general bytes
      0x00  // length of historical bytes
  };

这是我使用带有 NFC 工具或 NXP Tag info 应用程序的 Android 手机读取仿真卡时的串行输出:

22:17:42.442 -> Ndef encoded message size: 9
22:17:42.442 -> SAMConfig
22:17:42.442 -> write:  14 01 14 01
22:17:42.475 -> read:   15
22:17:42.475 -> write:  8C 00 08 00 12 34 56 60 01 FE A2 A3 A4 A5 A6 A7 C0 C1 C2 C3 C4 C5 C6 C7 FF FF AA 99 88 77 66 55 44 33 22 11 00 00
22:17:54.656 -> read:   8D 05 25 D4 00 6D 76 32 0D D3 9D 4B 72 23 C7 00 00 00 32 46 66 6D 01 01 12 02 02 07 FF 03 02 00 03 04 01 64 07 01 03
22:17:54.656 -> write:  86
22:17:54.689 -> read:   87 29
22:17:54.689 -> status is not ok
22:17:54.689 -> tgGetData failed!
22:17:54.689 -> write:  52 00
22:17:54.689 -> read:   53 00
22:17:54.689 -> write:  8C 00 08 00 12 34 56 60 01 FE A2 A3 A4 A5 A6 A7 C0 C1 C2 C3 C4 C5 C6 C7 FF FF AA 99 88 77 66 55 44 33 22 11 00 00
22:17:55.733 -> tgInitAsTarget timed out!timed out
22:17:56.716 -> write:  8C 00 08 00 12 34 56 60 01 FE A2 A3 A4 A5 A6 A7 C0 C1 C2 C3 C4 C5 C6 C7 FF FF AA 99 88 77 66 55 44 33 22 11 00 00`

如果我改变

PN532_COMMAND_TGINITASTARGET,
      0x0, 

PN532_COMMAND_TGINITASTARGET,
      0x04,

https://www.nxp.com/docs/en/nxp/application-notes/AN133910.pdf第 57 页 中的内容然后我的 android 手机没有响应,没有任何反应

Iphone XR 适用于

PN532_COMMAND_TGINITASTARGET,
      0x0,
22:19:29.307 -> ------- Emulate Tag --------
22:19:29.307 -> Ndef encoded message size: 9
22:19:29.307 -> SAMConfig
22:19:29.307 -> write:  14 01 14 01
22:19:29.307 -> read:   15
22:19:29.307 -> write:  8C 00 08 00 12 34 56 60 01 FE A2 A3 A4 A5 A6 A7 C0 C1 C2 C3 C4 C5 C6 C7 FF FF AA 99 88 77 66 55 44 33 22 11 00 00
22:19:35.770 -> read:   8D 08 E0 80
22:19:35.770 -> write:  86
22:19:35.770 -> read:   87 00 00 A4 04 00 07 D2 76 00 00 85 01 01 00
22:19:35.804 -> write:  8E 90 00
22:19:35.804 -> read:   8F 00
22:19:35.804 -> write:  86
22:19:35.837 -> read:   87 00 00 A4 04 00 07 D2 76 00 00 85 01 01 00
22:19:35.837 -> write:  8E 90 00
22:19:35.837 -> read:   8F 00
22:19:35.837 -> write:  86
22:19:35.871 -> read:   87 00 90 60 00 00 00
22:19:35.871 -> Command not supported! 60
22:19:35.871 -> write:  8E 6A 81
22:19:35.871 -> read:   8F 00
22:19:35.871 -> write:  86
22:19:35.871 -> read:   87 00 00 A4 04 00 10 A0 00 00 03 96 56 43 41 FF FF FF FF FF FF FF FF 00
22:19:35.904 -> function not supported
22:19:35.904 -> write:  8E 6A 81
22:19:35.904 -> read:   8F 00
22:19:35.904 -> write:  86
22:19:35.904 -> read:   87 00 90 60 00 00 00
22:19:35.904 -> Command not supported! 60
22:19:35.904 -> write:  8E 6A 81
22:19:35.938 -> read:   8F 00
22:19:35.938 -> write:  86
22:19:35.938 -> read:   87 00 00 A4 04 00 07 D2 76 00 00 85 01 00 00
22:19:35.938 -> function not supported
22:19:35.938 -> write:  8E 6A 81
22:19:35.938 -> read:   8F 00
22:19:35.971 -> write:  86
22:19:35.971 -> read:   87 00 90 60 00 00 00
22:19:35.971 -> Command not supported! 60
22:19:35.971 -> write:  8E 6A 81
22:19:35.971 -> read:   8F 00
22:19:35.971 -> write:  86
22:19:36.005 -> read:   87 00 00 A4 04 00 07 D2 76 00 00 85 01 01 00
22:19:36.005 -> write:  8E 90 00
22:19:36.005 -> read:   8F 00
22:19:36.005 -> write:  86
22:19:36.039 -> read:   87 00 00 A4 00 0C 02 E1 03
22:19:36.039 -> write:  8E 90 00
22:19:36.039 -> read:   8F 00
22:19:36.039 -> write:  86
22:19:36.039 -> read:   87 00 00 B0 00 00 0F
22:19:36.073 -> write:  8E 00 0F 20 00 54 00 FF 04 06 E1 04 00 80 00 00 90 00
22:19:36.073 -> read:   8F 00
22:19:36.073 -> write:  86
22:19:36.073 -> read:   87 00 00 A4 00 0C 02 E1 04
22:19:36.073 -> write:  8E 90 00
22:19:36.106 -> read:   8F 00
22:19:36.106 -> write:  86
22:19:36.106 -> read:   87 00 00 B0 00 00 02
22:19:36.106 -> write:  8E 00 09 90 00
22:19:36.106 -> read:   8F 00
22:19:36.106 -> write:  86
22:19:36.139 -> read:   87 00 00 A4 00 0C 02 E1 04
22:19:36.139 -> write:  8E 90 00
22:19:36.139 -> read:   8F 00
22:19:36.139 -> write:  86
22:19:36.139 -> read:   87 00 00 B0 00 00 09
22:19:36.174 -> write:  8E 00 09 D1 01 05 54 02 65 6E 90 00
22:19:36.174 -> read:   8F 00
22:19:36.174 -> write:  86
22:19:36.174 -> read:   87 00 00 B0 00 09 02
22:19:36.174 -> write:  8E 48 69 90 00
22:19:36.207 -> read:   8F 00
22:19:36.207 -> write:  86
22:19:36.241 -> read:   87 13
22:19:36.241 -> status is not ok
22:19:36.241 -> tgGetData failed!
22:19:36.275 -> write:  52 00
22:19:36.275 -> read:   53 00
22:19:36.275 -> write:  8C 00 08 00 12 34 56 60 01 FE A2 A3 A4 A5 A6 A7 C0 C1 C2 C3 C4 C5 C6 C7 FF FF AA 99 88 77 66 55 44 33 22 11 00 00
22:19:37.316 -> tgInitAsTarget timed out!timed out
22:19:38.325 -> write:  8C 00 08 00 12 34 56 60 01 FE A2 A3 A4 A5 A6 A7 C0 C1 C2 C3 C4 C5 C6 C7 FF FF AA 99 88 77 66 55 44 33 22 11 00 00

Iphone XR 也适用于PN532_COMMAND_TGINITASTARGET,0x04,https: //www.nxp.com/docs/en/nxp/application-notes/AN133910.pdf第 57 页中的内容

22:27:13.584 -> Ndef encoded message size: 9
22:27:13.584 -> SAMConfig
22:27:13.584 -> write:  14 01 14 01
22:27:13.584 -> read:   15
22:27:13.584 -> write:  8C 04 08 00 12 34 56 60 01 FE A2 A3 A4 A5 A6 A7 C0 C1 C2 C3 C4 C5 C6 C7 FF FF AA 99 88 77 66 55 44 33 22 11 00 00
22:27:24.286 -> read:   8D 08 E0 80
22:27:24.286 -> write:  86
22:27:24.320 -> read:   87 00 00 A4 04 00 07 D2 76 00 00 85 01 01 00
22:27:24.320 -> write:  8E 90 00
22:27:24.320 -> read:   8F 00
22:27:24.320 -> write:  86
22:27:24.353 -> read:   87 00 00 A4 04 00 07 D2 76 00 00 85 01 01 00
22:27:24.353 -> write:  8E 90 00
22:27:24.353 -> read:   8F 00
22:27:24.353 -> write:  86
22:27:24.353 -> read:   87 00 90 60 00 00 00
22:27:24.353 -> Command not supported! 60
22:27:24.387 -> write:  8E 6A 81
22:27:24.387 -> read:   8F 00
22:27:24.387 -> write:  86
22:27:24.387 -> read:   87 00 00 A4 04 00 10 A0 00 00 03 96 56 43 41 FF FF FF FF FF FF FF FF 00
22:27:24.420 -> function not supported
22:27:24.420 -> write:  8E 6A 81
22:27:24.420 -> read:   8F 00
22:27:24.420 -> write:  86
22:27:24.420 -> read:   87 00 90 60 00 00 00
22:27:24.420 -> Command not supported! 60
22:27:24.420 -> write:  8E 6A 81
22:27:24.454 -> read:   8F 00
22:27:24.454 -> write:  86
22:27:24.454 -> read:   87 00 00 A4 04 00 07 D2 76 00 00 85 01 00 00
22:27:24.454 -> function not supported
22:27:24.454 -> write:  8E 6A 81
22:27:24.454 -> read:   8F 00
22:27:24.488 -> write:  86
22:27:24.488 -> read:   87 00 90 60 00 00 00
22:27:24.488 -> Command not supported! 60
22:27:24.488 -> write:  8E 6A 81
22:27:24.488 -> read:   8F 00
22:27:24.488 -> write:  86
22:27:24.522 -> read:   87 00 00 A4 04 00 07 D2 76 00 00 85 01 01 00
22:27:24.522 -> write:  8E 90 00
22:27:24.522 -> read:   8F 00
22:27:24.522 -> write:  86
22:27:24.556 -> read:   87 00 00 A4 00 0C 02 E1 03
22:27:24.556 -> write:  8E 90 00
22:27:24.556 -> read:   8F 00
22:27:24.556 -> write:  86
22:27:24.556 -> read:   87 00 00 B0 00 00 0F
22:27:24.556 -> write:  8E 00 0F 20 00 54 00 FF 04 06 E1 04 00 80 00 00 90 00
22:27:24.589 -> read:   8F 00
22:27:24.589 -> write:  86
22:27:24.589 -> read:   87 00 00 A4 00 0C 02 E1 04
22:27:24.589 -> write:  8E 90 00
22:27:24.623 -> read:   8F 00
22:27:24.623 -> write:  86
22:27:24.623 -> read:   87 00 00 B0 00 00 02
22:27:24.623 -> write:  8E 00 09 90 00
22:27:24.623 -> read:   8F 00
22:27:24.623 -> write:  86
22:27:24.656 -> read:   87 00 00 A4 00 0C 02 E1 04
22:27:24.656 -> write:  8E 90 00
22:27:24.656 -> read:   8F 00
22:27:24.656 -> write:  86
22:27:24.656 -> read:   87 00 00 B0 00 00 09
22:27:24.656 -> write:  8E 00 09 D1 01 05 54 02 65 6E 90 00
22:27:24.690 -> read:   8F 00
22:27:24.690 -> write:  86
22:27:24.690 -> read:   87 00 00 B0 00 09 02
22:27:24.690 -> write:  8E 48 69 90 00
22:27:24.690 -> read:   8F 00
22:27:24.724 -> write:  86
22:27:24.724 -> read:   87 13
22:27:24.724 -> status is not ok
22:27:24.724 -> tgGetData failed!
22:27:24.724 -> write:  52 00
22:27:24.759 -> read:   53 00
22:27:24.759 -> write:  8C 04 08 00 12 34 56 60 01 FE A2 A3 A4 A5 A6 A7 C0 C1 C2 C3 C4 C5 C6 C7 FF FF AA 99 88 77 66 55 44 33 22 11 00 00
22:27:25.770 -> tgInitAsTarget timed out!timed out
22:27:26.787 -> write:  8C 04 08 00 12 34 56 60 01 FE A2 A3 A4 A5 A6 A7 C0 C1 C2 C3 C4 C5 C6 C7 FF FF AA 99 88 77 66 55 44 33 22 11 00 00

所以我不知道我应该在这个数组中写什么来让我的 android 读取它的内容。这是默认值:

      PN532_COMMAND_TGINITASTARGET,
      0x0,                  // MODE: 0x04 = PICC only, 0x01 = Passive only (0x02 = DEP only)

      // MIFARE PARAMS
      0x08, 0x00,         // SENS_RES (seeeds studio set it to 0x04, nxp to 0x08)
      0x00, 0x00, 0x00,   // NFCID1t    (is set over sketch with setUID())
      0x60,               // SEL_RES    (0x20=Mifare DelFire, 0x60=custom)

      // FELICA PARAMS
      0x01, 0xFE,         // NFCID2t (8 bytes) https://github.com/adafruit/Adafruit-PN532/blob/master/Adafruit_PN532.cpp FeliCa NEEDS TO BEGIN WITH 0x01 0xFE!
      0xA2, 0xA3, 0xA4,
      0xA5, 0xA6, 0xA7,
      0xC0, 0xC1, 0xC2,
      0xC3, 0xC4, 0xC5,
      0xC6, 0xC7, 0xFF,
      0xFF,
      0xAA, 0x99, 0x88, //NFCID3t (10 bytes)
      0x77, 0x66, 0x55, 0x44,
      0x33, 0x22, 0x11,

      0x00, // length of general bytes
      0x00  // length of historical bytes
  };

如果我使用这个数组,我只能将 PN532 检测为标签

        PN532_COMMAND_TGINITASTARGET,
        0x05,                  // MODE: PICC only, Passive only

        0x04, 0x00,         // SENS_RES
        0x0, 0x0, 0x0,   // NFCID1
        0x20,               // SEL_RES

        0,0,0,0,0,0,0,0,
        0,0,0,0,0,0,0,0,   // FeliCaParams
        0,0,

        0,0,0,0,0,0,0,0,0,0, // NFCID3t

        0, // length of general bytes
        0  // length of historical bytes
  };

然后这是我的串行输出:

22:50:02.335 -> Ndef encoded message size: 9
22:50:02.335 -> SAMConfig
22:50:02.335 -> write:  14 01 14 01
22:50:02.335 -> read:   15
22:50:02.335 -> write:  8C 05 04 00 12 34 56 20 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

如您所见,什么都没有发生...

我还用 Android S8 尝试了整个过程,它的行为类似于 Andoird S20。我希望有人能帮助我。

非常感谢您提前提供的帮助!

4

0 回答 0