PN532 - 在同一草图中与Mifare标签和NFC手机通信

时间:2018-01-18 17:16:54

标签: android arduino nfc mifare

我正在尝试使用带有mifare标签的PN532和同一草图中的Android应用程序。但问题似乎是mifare模块以及NFC模块检测到android手机。因此,在大约一半的情况下,它会尝试与手机通信,就像它是一个mifare标签一样。仅此一项不会成为问题,但在完成所有操作后仍无法再检测到手机。

作为一个小型演示,如果我将标签放置几次并在一段时间后将其移除,我会得到这样的输出:

[5150] Mifare Placed
[7301] Mifare Removed
[10545] Mifare Placed
[11626] Mifare Removed

但如果我放置并移除手机,输出如下:

[1821] Mobile Placed
[2951] Mobile Removed
[3615] Mifare Placed
[3692] Mifare Removed

正如您所看到的,第二次尝试让它与手机通信时,mifare模块会识别它并显示错误信息。

这是我的代码:

#include <Adafruit_PN532.h>


#define PN532_IRQ   (2)
#define PN532_RESET (3)

Adafruit_PN532 nfc(PN532_IRQ, PN532_RESET);


static uint8_t lastType = 255;

#include "mifareclassic.h"
MifareClassic* mifare;
#include "nfc.h"
NFC* mobile;


void setup() {  
  Serial.begin(115200);

  nfc.begin();

  uint32_t versiondata;
  versiondata = nfc.getFirmwareVersion();
  if (! versiondata) {
    Serial.println("Error!");
  }

  nfc.setPassiveActivationRetries(3); //else, it will endlessly try to retrieve the tag
  nfc.SAMConfig(); //read tags
}


void loop() {
  if(lastType==255){
    //MIFARE PLACED?
    if(mifare->isPlaced()){
      lastType = 0;
      Serial.println("["+String(millis())+"] Mifare Placed");
    }
    //NFC PLACED?
    else if(mobile->isPlaced()){
      lastType = 1;
      Serial.println("["+String(millis())+"] Mobile Placed");
    }
  } else {
    //MIFARE REMOVED?
    if(lastType==0){
      if(!mifare->isPlaced()){
        Serial.println("["+String(millis())+"] Mifare Removed");
        lastType = 255;
      }
    }
    //NFC REMOVED?
    else if(lastType==1){
      if(!mobile->isPlaced()){
        Serial.println("["+String(millis())+"] Mobile Removed");
        lastType = 255;
      }
    }
  }  
}

main.ino

#define block 4

class MifareClassic {
  public:
    MifareClassic(){}
    bool isPlaced();
};

bool MifareClassic::isPlaced(){
  uint8_t len;
  uint8_t uid[8] = {0};
  if(nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &len)){
    return true;
  } else {
    return false;
  }
}

mifareclassic.h

#define block 4

class NFC {
  public:
    NFC(){}
    bool isPlaced();
};

bool NFC::isPlaced(){
  if(lastType == 255){
    if(nfc.inListPassiveTarget()) {
      //nfc.inListPassiveTarget();
      //authentificate via AID
      uint8_t selectApdu[] = { 0x00, /* CLA */
                                0xA4, /* INS */
                                0x04, /* P1  */
                                0x00, /* P2  */
                                0x07, /* Length of AID  */
                                0xF0, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, /* AID defined on Android App */
                                0x00  /* Le  */ };
      uint8_t uid[8];
      uint8_t len;

      if(nfc.inDataExchange(selectApdu, sizeof(selectApdu), uid, &len)) {
        return true;
      }
    }
  } else {
    uint8_t apdu[] = {0};
    uint8_t back[32];
    uint8_t len; 

    if(nfc.inDataExchange(apdu, sizeof(apdu), back, &len)) {
      return true;
    }
  }

  return false;
}

nfc.h

1 个答案:

答案 0 :(得分:2)

经过大量的精子研究后,我找到了一个解决方案:我只是使用inListPassiveTarget()函数来查明是否放置了任何东西。如果是这种情况,我会通过inDataExchange()对AID进行身份验证来检查已放置的手机,如果不起作用,只会通过readPassiveTargetID()检查mifareclassic。

如果将来某个人遇到同样的问题,这是我的(现在正在工作的)代码:

#include <Wire.h>
#include <SPI.h>
#include <Adafruit_PN532.h>



#define PN532_IRQ   (2)
#define PN532_RESET (3)

Adafruit_PN532 nfc(PN532_IRQ, PN532_RESET);


enum { NONE, MIFARECLASSIC, MOBILE };
int lastType = NONE;

#include "mifareclassic.h"
#include "mobile.h"


void setup() {  
  Serial.begin(115200);

  nfc.begin();

  uint32_t versiondata;
  versiondata = nfc.getFirmwareVersion();
  if (! versiondata) {
    Serial.println("Error!");
  }

  nfc.setPassiveActivationRetries(3); //else, it will endlessly try to retrieve the tag
  nfc.SAMConfig(); //read tags
}


void loop() {

  if(lastType==NONE){
    if(nfc.inListPassiveTarget()){
      //MOBILE PLACED?
      if(Mobile::isNewTagPut()){
        lastType = MOBILE;
        Serial.println("["+String(millis())+"] Mobile Placed");
      } 
      //MIFARECLASSIC PLACED?
      else if(MifareClassic::isNewTagPut()){
        lastType = MIFARECLASSIC;
        Serial.println("["+String(millis())+"] Mifare Placed");
      }
    }
  } else {
    //MOBILE REMOVED?
    if(lastType==MOBILE){
      if(!Mobile::isTagStillPlaced()){
        Serial.println("["+String(millis())+"] Mobile Removed");
        lastType = NONE;
      }
    }
    //MIFARECLASSIC REMOVED?
    else if(lastType==MIFARECLASSIC){
      if(!MifareClassic::isTagStillPlaced()){
        Serial.println("["+String(millis())+"] Mifare Removed");
        lastType = NONE;
      }
    }
  }
}

main.ino

#define block 4

class MifareClassic {
  public:
    static bool isNewTagPut();
    static bool isTagStillPlaced();
};

bool MifareClassic::isNewTagPut(){
  return isTagStillPlaced();
}

bool MifareClassic::isTagStillPlaced(){
  uint8_t len;
  uint8_t uid[8] = {0};
  return nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &len);
}

mifareclassic.h

class Mobile {
  public:
    static bool isNewTagPut();
    static bool isTagStillPlaced();
};

bool Mobile::isNewTagPut(){  
  //authentificate via AID
  uint8_t selectApdu[] = { 0x00, /* CLA */
                            0xA4, /* INS */
                            0x04, /* P1  */
                            0x00, /* P2  */
                            0x07, /* Length of AID  */
                            0xF0, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, /* AID defined on Android App */
                            0x00  /* Le  */ };
  uint8_t uid[8];
  uint8_t len;

  return nfc.inDataExchange(selectApdu, sizeof(selectApdu), uid, &len);
}

bool Mobile::isTagStillPlaced(){
  uint8_t apdu[] = {0};
  uint8_t back[32];
  uint8_t len; 

  return nfc.inDataExchange(apdu, sizeof(apdu), back, &len);
}

mobile.h