My NFC project , using PN532 and android phone

Hello ,
here is my code :

#include <NfcAdapter.h>
#include <PN532/PN532/PN532.h>
#include "SPI.h"
#include <PN532/PN532_HSU/PN532_HSU.h>
#include "PN532/PN532/snep.h"
#include "NdefMessage.h"

PN532_HSU pn532hsu(Serial);
SNEP nfc(pn532hsu);

uint8_t ndefBuf[128];

void setup(void) {
SERIAL.begin(115200);
SERIAL.println("NDEF Reader");
}

void loop(void) {
SERIAL.println("Send a message to Peer");

NdefMessage message = NdefMessage();
message.addUriRecord("http://google.com");



int messageSize = message.getEncodedSize();
if (messageSize > sizeof(ndefBuf)) {
    SERIAL.println("ndefBuf is too small");
    while (1) {
    }
}

message.encode(ndefBuf);
if (0 >= nfc.write(ndefBuf, messageSize) , ) {
    SERIAL.println("Failed");
} else {
    SERIAL.println("Success");
}

delay(3000);
}

//===========================================================================

This code is very simple and I used it to handle some events .
when I keep my Samsung Galaxy J7 phone near to PN532 , I get The message and the google.com comes up into the chrome .
but in other android mobile or even other Samsung mobile nothing is happening .
another question is here :
Could we in p2p mode , make the target ( in my code PN532 ) parameters , something like MIfare classic .

I used these libraries :

GitHub - Seeed-Studio/PN532: NFC library using PN532 to read/write card and communicate with android

please edit your post, select the code part and press the </> icon in the tool bar to mark it as code. (also make sure you indented the code in the IDE before copying, that’s done by pressing ctrlT on a PC or cmdT on a Mac)
—-

Triggering a connection as soon as a NFC tag is identified is a security risk, so might be blocked by some versions or user acceptance might be required.

Not sure what you mean with Mifare

1 Like

Sorry , I did edit the post . Could you please inform me how does matter exists ?
SNEP does not work with some of Android device such SAMSUNG ?

Are you sure those phones have NFC capability ?

I’m an iPhone user so don’t know all the differences in the zillions android devices out there but if you don’t have an NFC chip then that clearly won’t work :slight_smile:

(Also with physical tags - Not all the tags are formatted in the same way or react to the same frequency)

Yes they all got NFC capability . Do you know that Are all android phones support SNEP communication ? I guess it might be something like this .
In addition Could you please inform me that how can we make a session with Iphones (ios) ?
I found out that the ios does not support p2p . is there any way to have something like this ?
//

 int msgSize = nfcP2P.read (  ndefBufTest , sizeof ( ndefBufTest )  ) ;
if (msgSize > 0) {
nfc.emulate ( 2000 )  ; 
}

Android is deprecating SNEP and thus the current peer to peer support
I don’t think iOS ever supported it

So DO YOU KNOW what should I do ?
Isn't there any library like snep ?

I don’t know what you want to do… what’s the end goal ?

Well , My projects is very simple . I am using an Arduino uno with pn532 module.
I want to emulate the pn532 to mifare classic card that android phone can detect it .

I’m not sure what you mean by that.

you format the card as NDEF and that’s what iOS and Android can easily read and if the data is an URL the device will prompt the user to go visit that URL (or go directly possibly).

Let me be more clear .
this is the simple code :

#include <Wire.h>
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

//libraries for NFC

#include <Arduino.h>
#include <NfcAdapter.h>
#include <PN532/PN532/PN532.h>
#include <PN532/PN532/emulatetag.h>
#include <PN532/PN532_HSU/PN532_HSU.h>
#include "NdefMessage.h"





PN532_HSU pn532hsu ( Serial2 ) ;
EmulateTag nfc ( pn532hsu ) ;
NfcAdapter nfc_adapter = NfcAdapter ( pn532hsu ) ;

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.seeedstudio.com");
  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();

  
}

// the loop function runs over and over again until power down or reset

void loop()
{
  Serial.println("Begin emulation.");
  if (!nfc.emulate(10000))//timeout 10 seconds
    Serial.println("timed out");
  else
    Serial.println("mobile device found!");

  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);
}

when I use this code I expect that if I keep my android phone , near to the pn532 module , the URL comes up . but nothing happen . after using NFC reader application I found that the pn532 is emulating to Felica card , not Mifare classic .
this is the issue .
after changing this code of emulate() in emulate.cpp from this

uint8_t command[] = {
    PN532_COMMAND_TGINITASTARGET,
    5, // MODE: PICC only, Passive only

    0x04, 0x00,       // SENS_RES
    0x00, 0x00, 0x00, // 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
    };

to this

  uint8_t command[] = {
      PN532_COMMAND_TGINITASTARGET,
      0x05,                  // MODE: 0x04 = PICC only, 0x01 = Passive only (0x02 = DEP only)

      // MIFARE PARAMS
      0x04, 0x00,         // SENS_RES (seeeds studio set it to 0x04, nxp to 0x08)
      0x00, 0x00, 0x00,   // NFCID1t	(is set over sketch with setUID())
      0x20,               // 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!
      0x05, 0x01, 0x86,
      0x04, 0x02, 0x02,
      0x03, 0x00,         // PAD (8 bytes)
      0x4B, 0x02, 0x4F, 
      0x49, 0x8A, 0x00,   
      0xFF, 0xFF,         // System code (2 bytes)
      
      0x01, 0x01, 0x66,   // NFCID3t (10 bytes)
      0x6D, 0x01, 0x01, 0x10,
      0x02, 0x00, 0x00,

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

my android phone can get nothing .

Ok I’ve never tried emulating other cards nor do I care about Android support.I used NDEF formatting (a while ago) and wrote a custom app to read the card. Not sure a phone would pick up a different card. Have you checked Android NFC specs for what’s built in?


Side note: I’ve noticed they have a list of compatible phones to * Communicate with android 4.0+(Lists of devices supported)

Yes I've checked Android NFC specs .
Thank you for your time .

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.