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);
}
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
SNEPmessages usingenableReaderMode(this is what you have asked react-native-nfc-manager library to use).This is because
SNEPand (LLCP) is not aTYPE Atechnology typeIf you look at the NFC standards diagram at https://pdfslide.net/documents/divnfc0804-250-nfc-standards-v18.html
It might be a
TYPE Ftechnology type so I would try instead ofNfcAdapter.FLAG_READER_NFC_AI would useNfcAdapter.FLAG_READER_NFC_For 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
NDEFmessages and you have disabled the System NFC App from processing NDEF messages withNfcAdapter.FLAG_READER_SKIP_NDEF_CHECKso I would try removing that and work with theNdeftag technology type.But I don't think any of that will help, the next thing I would try is to not use
enableReaderModewithreact-native-nfc-managerbut use the underlyingenableForgroundDispatchmethods by just by specifyingNfcManager.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
Intentsto share with other Apps either to Launch an App to handle theIntentor pass it to a running App that has asked to be sent NFCIntents.As this looks to be a common point between how the Android System NFC App handles real NFC Tags and Peer to Peer
SNEPmessages as aSNEPmessage 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)