Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
592 views
in Technique[技术] by (71.8m points)

nfc - Comunicazione NDEF

I'm working on a mobile app and my intent is to make the Arduino communicate with the smartphone. so far I can only read the first message sent by the arduino, when the application is not active.

I'm using this function of react-native-nfc-manager library:

getLaunchTagEvent ()

After this event I can no longer read other NDEF messages. how can i solve?

The code is as follows:

componentDidMount(){
  NfcManager.isSupported()
        .then(supported => {
            this.setState({ supported });
            if (supported) {
                this._startNfc();
            }
        })
}
 
_startNfc() {
  if (Platform.OS === 'android') {
      NfcManager.getLaunchTagEvent()
          .then(tag => {
              console.log('launch tag', tag);
              if (tag) {
                  this.setState({ tag });
              }
          })
          .catch(err => {
              console.log(err);
          })
      
      
    }
  }

Also i am trying to read the tag with the application open, but the action fails on the arduino. solutions? The code is as follows:

readData = async () => {
    NfcManager.registerTagEvent(
     tag => {
         console.log('Tag Discovered', tag);
     },
     'Hold your device over the tag',
     {
      readerModeFlags:
      NfcAdapter.FLAG_READER_NFC_A | NfcAdapter.FLAG_READER_SKIP_NDEF_CHECK,
      readerModeDelay: 2,
   },
 );

}

The Arduino code is as follows:

#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];

void setup() {
     Serial.begin(9600);
     Serial.println("NFC Peer to Peer-Send Message");
}

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

     NdefMessage message = NdefMessage();
     message.addTextRecord("Hello");
    
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); 
}
question from:https://stackoverflow.com/questions/66066062/comunicazione-ndef

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The uses of SNEP (and LLCP) complicates things as this is a peer to peer protocol and peer to peer has been deprecated in Android 10 and not supported in iOS and I'm not so familiar with it.

I'm not sure it is possible read SNEP messages using enableReaderMode (this is what you have asked react-native-nfc-manager library to use).
This is because SNEP and (LLCP) is not a TYPE A technology type

If you look at the NFC standards diagram at https://pdfslide.net/documents/divnfc0804-250-nfc-standards-v18.html

It might be a TYPE F technology type so I would try instead of NfcAdapter.FLAG_READER_NFC_A I would use NfcAdapter.FLAG_READER_NFC_F or enable all of the technologies to be on the safe side (though I think this might not work as well)

But if this does not work, normally with Android Peer to Peer it expects only to be sent NDEF messages and you have disabled the System NFC App from processing NDEF messages with NfcAdapter.FLAG_READER_SKIP_NDEF_CHECK so I would try removing that and work with the Ndef tag technology type.

But I don't think any of that will help, the next thing I would try is to not use enableReaderMode with react-native-nfc-manager but use the underlying enableForgroundDispatch methods by just by specifying NfcManager.registerTagEvent();.
As this interacts with the Android System NFC App at a later point in the chain of events where the Android System NFC App is creating Intents to share with other Apps either to Launch an App to handle the Intent or pass it to a running App that has asked to be sent NFC Intents.
As this looks to be a common point between how the Android System NFC App handles real NFC Tags and Peer to Peer SNEP messages as a SNEP message can launch your App.

But going forward I would not use SNEP (peer to peer) as this is deprecated but get the Arduino to do Host Card Emulation to send the data (Then you could use Reader Mode)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...