【发布时间】:2017-05-30 15:01:40
【问题描述】:
我想制作一个可以读取 NFC 标签的 react-native 应用程序。我正在使用 react-native-nfc 但无法使其正常工作。任何人都可以帮助找出我做错了什么并指出我正确的方向吗?
index.android.js 中的代码如下:
import React, { Component } from 'react';
import {
AppRegistry,
StyleSheet,
Text,
View,
ToastAndroid
} from 'react-native';
import NFC, {NfcDataType, NdefRecordType} from "react-native-nfc";
export default class nfcTry extends Component {
constructor(props){
super(props);
}
componentDidMount(){
this.bindNfcListener();
}
bindNfcListener(){
NFC.addListener((payload)=>{
alert(payload.data.id);
})
}
render() {
return (
<View style={styles.container}>
<Text style={styles.welcome}>
Welcome to React Native!
</Text>
</View>
);
}
}
AndroidManifest.xml 中的代码如下:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.nfctry"
android:versionCode="1"
android:versionName="1.0">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
<uses-permission android:name="android.permission.NFC" />
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="22" />
<application
android:name=".MainApplication"
android:allowBackup="true"
android:label="@string/app_name"
android:icon="@mipmap/ic_launcher"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:launchMode="singleTask"
android:label="@string/app_name"
android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
android:windowSoftInputMode="adjustResize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.nfc.action.NDEF_DISCOVERED"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
<intent-filter>
<action android:name="android.nfc.action.TECH_DISCOVERED"/>
</intent-filter>
<meta-data android:name="android.nfc.action.TECH_DISCOVERED" android:resource="@xml/nfc_tech_filter" />
</activity>
<activity android:name="com.facebook.react.devsupport.DevSettingsActivity" />
</application>
</manifest>
【问题讨论】:
-
当您说“我正在使用 react-native-nfc 但无法使其正常工作”时,您是否收到任何错误消息? 'react-native-nfc' 的例子怎么样,你能让它工作吗?
-
@OlivierM 该应用程序安装在我的平板电脑上并且工作正常,我没有收到任何错误,但是当我尝试从卡中读取 NFC 标签时没有任何反应。我尝试了为 ''react-native-nfc' 给出的示例,它是一样的。
-
您是否通过第三方应用程序确认 NFC 在您的平板电脑上工作?
-
@OlivierM 是的,我做到了,而且效果很好。我已经尝试过使用名为“NFC TagInfo”的应用程序。
-
您是否还创建了文件“nfc_tech_filter.xml”?你检查过'adb logcat'的输出吗?
标签: android react-native nfc rfid