Connecting NFC scanner to phone in order to show a pop-up

106 Views Asked by At

My name is Daniel Spierings, and I am reaching out to you on behalf of my project-group. We are currently working on a school project in our first year, which in our case includes the usage of NFC.

We've paired our BBC Micro:bit controller to a PN532 NFC Module V3 (HW-147) and wrote some code to let our NFC scanner recognize NFC tags that are brought in contact. I'll provide you with the code, which is written in the Arduino IDE. Our hope is that we can make the PN532 show a pop-up on our phone.

We are hoping you're be able to help us out when it comes to connecting a phone to the PN532 in order to show a pop-up, as we (after a lot of googling) have no idea how to get this to work.

Ideally the pop-up menu should show a yes/no option button, but this is not an absolute must. It would already be amazing if we are able to hold our phone against the PN532 in order for a pop-up to appear on our screen. We're hoping you could help us out with this.

Thank you for your time

  • Daniel
#include <Wire.h>
#include <Adafruit_PN532.h>

#define SDA_PIN 20  // Define the SDA pin on the micro:bit
#define SCL_PIN 19  // Define the SCL pin on the micro:bit

Adafruit_PN532 nfc(SDA_PIN, SCL_PIN);

void setup(void) {
  Serial.begin(115200);
  Serial.println("Hello!");

  nfc.begin();
  uint32_t versiondata = nfc.getFirmwareVersion();
  if (!versiondata) {
    Serial.print("Didn't find PN53x board");
    while (1);
  }

  nfc.SAMConfig();
  Serial.println("Waiting for an NFC card ...");
}

void loop(void) {
  uint8_t success;
  uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 };
  uint8_t uidLength;

  success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength);

  if (success) {
    Serial.println("Found an NFC card!");

    Serial.print("UID Length: "); Serial.print(uidLength, DEC); Serial.println(" bytes");
    Serial.print("UID Value: ");
    for (uint8_t i = 0; i < uidLength; i++) {
      Serial.print(" 0x"); Serial.print(uid[i], HEX);
    }
    Serial.println("");
    delay(1000);
  }
}
1

There are 1 best solutions below

0
On

This is a very big question and something I can only give pointers to as I don't really do NFC on Microcontrollers.

First off NFC hardware has 2 main modes of operating - Reader/Write mode (Initiator in NFC terms) and (Host) card emulation mode (Target in NFC terms).

And all communication is started by the hardware acting as an Initiator to a piece of hardware acting as a Target.

A lot of hardware can act as Initiator or Target with the right commands and software, luckily the PN532 can act as both.

So to achieve what you want the phone needs to act as the Initiator and the PN532 as the target. Android (and I believe iOS) have some default software installed to read selected types of Data of a Target device without additional software, usually these are data in the Ndef URL or Text format.

Otherwise you will have to write an App for the phone to read other data formats.

Details of the Ndef data format are here

Moving on your current code is for the PN532 to act as an Initiator, which is of no use as 2 Initiators won't communicate with each other.

If you read section 7.4 and 7.5 of the PN532 datasheet this give the commands for "ISO/IEC14443-4 PICC mode" which will allow the PN532 to behave like a NFC Type 4 Tag to store Ndef data.

So in summary you need to write your BBC Micro:bit controller code to configure the PN532 in to Target mode and then correctly respond to the Type 4 commands for reading of Ndef data format and respond with some Ndef Text/Url data.

A quick google shows some example code for the Adafruit PN532 in correct mode here and basic APDU parsing and responding. You will needs to correctly respond to a series of APDU's to send your Ndef data to the phone.

I hope that helps and gives you a pointer to this complex problem.