我正在开发一款移动应用程序,我的目的是让Arduino与智能手机进行通信。到目前为止,我只能在应用程序处于非活动状态时读取arduino发送的第一条消息。
我使用了react-native-nfc-manager库的这个函数:
getLaunchTagEvent ()在此事件之后,我不能再读取其他NDEF消息。我该怎么解决呢?
代码如下:
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);
})
}
}此外,我试图在应用程序打开的情况下读取标记,但在arduino上操作失败。解决方案?代码如下:
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,
},
);
}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);
}发布于 2021-02-06 21:25:02
SNEP (和LLCP)的使用使事情变得复杂,因为这是一个点对点的协议,而点对点在Android10中已经被弃用,在iOS中不受支持,我对它不是很熟悉。
我不确定是否可以使用enableReaderMode读取SNEP消息(这是您要求使用的react-native-nfc-manager库)。
这是因为SNEP和(LLCP)不是TYPE A技术类型
如果你看一下https://pdfslide.net/documents/divnfc0804-250-nfc-standards-v18.html上的NFC标准图表
它可能是TYPE F技术类型,所以我会尝试而不是NfcAdapter.FLAG_READER_NFC_A,我会使用NfcAdapter.FLAG_READER_NFC_F,或者为了安全起见启用所有的技术(尽管我认为这可能不起作用)
但是,如果这不起作用,通常与Android点对点,它只希望发送NDEF消息,您已经禁止系统NFC应用程序处理NfcAdapter.FLAG_READER_SKIP_NDEF_CHECK的NDEF消息,所以我会尝试删除它,并与Ndef标签技术类型。
但我不认为这会有任何帮助,接下来我会尝试不将enableReaderMode与react-native-nfc-manager一起使用,而只是通过指定NfcManager.registerTagEvent();来使用底层的enableForgroundDispatch方法。
由于这与Android System NFC App在事件链中的稍后点交互,其中Android System NFC App正在创建Intents以与其他应用程序共享,以启动处理Intent的应用程序或将其传递给已请求发送NFC Intents的运行应用程序。
因为这看起来是Android System NFC App如何处理真正的NFC标签和点对点SNEP消息作为SNEP消息可以启动您的应用程序之间的共同点。
但接下来我不会使用SNEP (点对点),因为这是不推荐的,但让Arduino做主机卡仿真来发送数据(然后你可以使用阅读器模式)
https://stackoverflow.com/questions/66066062
复制相似问题