React Native中无法获取NFC卡字符串值的技术求助
问题:React Native读取NFC卡存储字符串失败
将近三天尝试在React Native中读取NFC卡存储的字符串值,但始终无法成功。扫描卡片时,getTag()函数能在日志中返回一些数据,但其中找不到卡片中存储的目标字符串。
以下是先后尝试的两段代码:
第一段代码
try { NfcManager.start(); await NfcManager.requestTechnology(NfcTech.NfcA); const tag1 = await NfcManager.getTag(); console.log("🚀 ~ file: NFC.js ~ line 41 ~ readTags ~ tag", JSON.stringify(tag1)) } catch (ex) { console.warn('Oops!', ex); }
第二段代码
try { let tech = Platform.OS === ' ios ' ? NfcTech.MifareIOS : NfcTech.NfcA; let resp = await NfcManager.requestTechnology(tech, { alertMessage: " Ready for magic " }); let i; NfcManager.start(); let cmd = Platform.OS === 'ios' ? NfcManager.sendMifareCommandIOS : NfcManager.transceive; resp = await cmd([0x3A, 4, 4]) let payloadLength = parseInt(resp.toString().split(",")[1]); let payloadPages = Math.ceil(payloadLength / 4); let startPage = 5; let endPage = startPage + payloadPages - 1; resp = await cmd([0x3A, startPage, endPage]); let bytes = resp.toString().split(","); console.log("🚀 ~ file: NFC.js ~ line 51 ~ readTags ~ bytes", bytes) let text = ""; for (let i = 0; i < bytes.length; i++) { if (i < 5) { continue; } } if (parseInt(bytes[i]) === 254) { return; } text = text + String.fromCharCode(parseInt(bytes[i])); console.log("🚀 ~ file: NFC.js ~ line 61 ~ readTags ~ text", JSON.stringify(text)) } catch (ex) { console.warn('Oops!', ex); }
使用的NFC卡信息如下:
恳请各位帮忙解决该问题。
内容的提问来源于stack exchange,提问作者Shiv
相关产品推荐
相关产品推荐

