我正在尝试创建一个 android 应用程序,该应用程序将用于使用 Ingenico 终端进行付款。我将 react-native-nfc-manager ( https://github.com/whitedogg13/react-native-nfc-manager ) 添加到我的项目中,并遵循 v2-ios+android-write-ndef 示例。我还在手机上启用了 NFC。我从亚马逊买了一个 NFC USB 阅读器。我安装了它,还得到了一个名为 GoToTags 的 Windows 应用程序,它成功连接到 USB 模块。
我启动了我的应用程序并测试了 NFC 按钮。
扫描仪发出哔哔声(因此 NFC 技术已加载)但 GoToTags 发回一条消息(不支持 NFC 标签的类型),如下所示:
{"Uid":null,"ReadOnly":false,"DataLength":null,"CanMakeReadOnly":false,
"Formatted":false,"Records":null,"TagTech":null,"MaxDataLength":null,
"Exception":"The NFC tag's type is not supported."}
我不确定我做错了什么。我按照信中的说明进行操作,还观看并遵循了 YouTube 教程。
https://www.youtube.com/watch?v=Kx22B6OH3Oc
我的代码和 YouTube 视频中的那个人之间的唯一区别是我使用的是安卓手机而不是 iPhone。
这是我的代码:
import React from 'react';
import {
View,
Text,
TouchableOpacity,
} from 'react-native';
import NfcManager, {Ndef, NfcTech} from 'react-native-nfc-manager';
function buildUrlPayload(valueToWrite) {
return Ndef.encodeMessage([
Ndef.uriRecord(valueToWrite),
]);
}
class AppV2Ndef extends React.Component {
componentDidMount() {
NfcManager.start();
}
componentWillUnmount() {
this._cleanUp();
}
render() {
return (
<View style={{padding: 20}}>
<Text>NFC Demo</Text>
<TouchableOpacity
style={{padding: 10, width: 200, margin: 20, borderWidth: 1, borderColor: 'black'}}
onPress={this._testNdef}
>
<Text>Test Ndef</Text>
</TouchableOpacity>
<TouchableOpacity
style={{padding: 10, width: 200, margin: 20, borderWidth: 1, borderColor: 'black'}}
onPress={this._cleanUp}
>
<Text>Cancel Test</Text>
</TouchableOpacity>
</View>
)
}
_cleanUp = () => {
NfcManager.cancelTechnologyRequest().catch(() => 0);
}
_testNdef = async () => {
try {
let resp = await NfcManager.requestTechnology(NfcTech.Ndef, {
alertMessage: 'Ready to write some NFC tags!'
});
console.warn(resp);
let ndef = await NfcManager.getNdefMessage();
console.warn(ndef);
let bytes = buildUrlPayload('https://www.google.com');
await NfcManager.writeNdefMessage(bytes);
console.warn('successfully write ndef');
await NfcManager.setAlertMessageIOS('I got your tag!');
this._cleanUp();
} catch (ex) {
console.warn('ex', ex);
this._cleanUp();
}
}
}
export default AppV2Ndef;
我需要做的是使用 NFC 技术从手机向 Ingenico 终端发送一个 4 位数代码。但在我努力让这成为可能之前,我只想让 NFC 先工作。将https://google.com发送到读卡器将是一个很好的第一步。但就这样,没有运气。
我错过了什么?看起来很简单,不是吗?