[英]Comunicazione NDEF
I'm working on a mobile app and my intent is to make the Arduino communicate with the smartphone.我正在开发一个移动应用程序,我的目的是让 Arduino 与智能手机通信。 so far I can only read the first message sent by the arduino, when the application is not active.到目前为止,当应用程序未激活时,我只能读取 arduino 发送的第一条消息。
I'm using this function of react-native-nfc-manager library:我正在使用 react-native-nfc-manager 库的 function :
getLaunchTagEvent ()
After this event I can no longer read other NDEF messages.在此事件之后,我无法再阅读其他 NDEF 消息。 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.我也在尝试在应用程序打开的情况下读取标签,但 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: Arduino代码如下:
#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. SNEP
(和 LLCP)的使用使事情变得复杂,因为这是一个点对点协议,并且点对点在 Android 10 中已被弃用,并且在 iOS 中不受支持,我对它不太熟悉。
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).我不确定是否可以使用enableReaderMode
读取SNEP
消息(这是您要求 react-native-nfc-manager 库使用的)。
This is because SNEP
and (LLCP) is not a TYPE A
technology type这是因为SNEP
和 (LLCP) 不是TYPE A
技术类型
If you look at the NFC standards diagram at https://pdfslide.net/documents/divnfc0804-250-nfc-standards-v18.html如果您查看https://pdfslide.net/documents/divnfc0804-250-nfc-standards-v18.html的 NFC 标准图
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)它可能是TYPE F
技术类型,所以我会尝试而不是NfcAdapter.FLAG_READER_NFC_A
我会使用NfcAdapter.FLAG_READER_NFC_F
或启用所有技术以确保安全(尽管我认为这可能不起作用)
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.但是,如果这不起作用,通常使用 Android 点对点它只希望发送NDEF
消息,并且您已禁用系统 NFC 应用程序使用NfcAdapter.FLAG_READER_SKIP_NDEF_CHECK
处理 NDEF 消息,所以我会尝试删除它并使用Ndef
标签技术类型。
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();
但是我认为这些都没有帮助,接下来我要尝试的是不要将enableReaderMode
与react-native-nfc-manager
一起使用,而是通过指定NfcManager.registerTagEvent();
来使用底层的enableForgroundDispatch
方法; . .
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
.由于这与 Android 系统 NFC 应用程序在事件链的稍后点进行交互,Android 系统 NFC 应用程序正在创建Intents
以与其他应用程序共享以启动应用程序来处理Intent
或将其传递给正在运行的应用程序要求发送 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.因为这看起来是 Android 系统 NFC 应用程序如何处理真正的 NFC 标签和点对点SNEP
消息之间的共同点,因为SNEP
消息可以启动您的应用程序。
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)但展望未来,我不会使用SNEP
(点对点),因为它已被弃用,而是让 Arduino 进行主机卡仿真以发送数据(然后你可以使用阅读器模式)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.