Android 在React本机应用程序中读取NFC标记
我想做一个react本机应用程序,可以读取NFC标签。我正在使用react native nfc,但无法正常工作。谁能帮我找出我做错了什么,并为我指明正确的方向 index.android.js中的代码如下所示:Android 在React本机应用程序中读取NFC标记,android,react-native,nfc,rfid,Android,React Native,Nfc,Rfid,我想做一个react本机应用程序,可以读取NFC标签。我正在使用react native nfc,但无法正常工作。谁能帮我找出我做错了什么,并为我指明正确的方向 index.android.js中的代码如下所示: import React, { Component } from 'react'; import { AppRegistry, StyleSheet, Text, View, ToastAndroid } from 'react-native'; import N
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>
);
}
}
<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>
import React,{Component}来自'React';
进口{
评估学,
样式表,
文本,
看法
蟾蜍
}从“反应本机”;
从“react native NFC”导入NFC,{NfcDataType,NdefRecordType};
导出默认类nfcTry扩展组件{
建造师(道具){
超级(道具);
}
componentDidMount(){
这个.bindNfcListener();
}
bindNfcListener(){
NFC.addListener((有效负载)=>{
警报(payload.data.id);
})
}
render(){
返回(
欢迎来到这里!
);
}
}
AndroidManifest.xml中的代码如下所示:
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>
);
}
}
<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>
我终于能够实现目标了!! 此时,另一个名为的npm包丢失了您的全部代码
将下面的包与您现有的代码链接,您可能会遇到NDK的一些问题,也要解决它们!最后你会成功的!!我正在根据需要立即运行此应用。当您说“我正在使用react native nfc,但无法使其工作”时,您是否收到任何错误消息?“react native nfc”的例子怎么样?你能让它工作吗?@OlivierM该应用程序安装在我的平板电脑上,工作正常,我没有收到任何错误,但当我试图从卡上读取nfc标签时,什么都没有发生。我试过“react-native-nfc”的例子,也是一样。你用第三方应用程序确认你的平板电脑上的nfc工作吗?@OlivierM是的,我确认了,它工作了。我已经在名为“NFC TagInfo”的应用程序中尝试过了。您是否也创建了文件“NFC_tech_filter.xml”?是否检查了“adb logcat”的输出?