【问题标题】:read NFC tags in React Native app在 React Native 应用程序中读取 NFC 标签
【发布时间】: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


【解决方案1】:

我终于可以达到目标了!! 此时,您的总代码与另一个名为 nfc-ndef-react-native 的 npm 包一起丢失。

将以下包与您现有的代码链接,您可能会遇到 NDK 的一些问题,也请解决它们!终于你能成功了!!我现在正在根据需要运行这个应用程序。

【讨论】:

    猜你喜欢
    • 2019-06-06
    • 1970-01-01
    • 1970-01-01
    • 2023-03-06
    • 2014-12-03
    • 1970-01-01
    • 2012-09-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多