React Native(不是 Expo):使用“react-native-nfc-manager”在 NTAG216 上设置和验证密码

问题描述 投票:0回答:1

我正在构建一个与 NXP NTAG216 NFC 标签交互的 React Native 应用程序(不使用 Expo)。我成功地使用

react-native-nfc-manager
来读取和写入数据,但我很难为写入操作实现密码保护

主要挑战:

  • 我找不到使用此库设置和验证密码的简单方法。

具体问题:

  1. 是否可以使用
    react-native-nfc-manager
    直接在NTAG216标签上设置和验证密码保护?
    如果可以,该库提供了哪些具体方法或途径?
  2. 如果
    react-native-nfc-manager
    不支持密码保护,是否有我可以探索的替代库或策略?
    如果需要,我愿意使用自定义命令或较低级别的通信。

代码片段:

import React from 'react';
import {View, Text, TouchableOpacity, StyleSheet} from 'react-native';
import NfcManager, {Ndef, NfcTech, nfcManager} from 'react-native-nfc-manager';

// Pre-step, call this before any NFC operations
NfcManager.start();

function App() {
  async function readNdef() {
    try {
      console.log('🚀 ~ readNdef ~ NfcManager:', 'entered');
      // register for the NFC tag with NDEF in it
      await NfcManager.requestTechnology(NfcTech.Ndef);
      // the resolved tag object will contain `ndefMessage` property
      const tag = await NfcManager.getTag();
      console.warn('Tag found', tag);
    } catch (ex) {
      console.warn('Oops!', ex);
    } finally {
      // stop the nfc scanning
      console.log('exited');
      NfcManager.cancelTechnologyRequest();
    }
  }
  function buildUrlPayload(valueToWrite) {
    return Ndef.encodeMessage([Ndef.uriRecord(valueToWrite)]);
  }
  const writeNFC = async () => {
    let result = false;

    try {
      await NfcManager.requestTechnology(NfcTech.Ndef);
      const bytes = buildUrlPayload(
        'https://stackoverflow.com/questions/11896981/change-nfc-tag-to-be-read-only',
      );
      await NfcManager.ndefHandler.writeNdefMessage(bytes);
      console.log('🚀 ~ writeNFC ~ bytes:', bytes);
      console.log('makeReadOnly');
    } catch (ex) {
      console.warn(ex);
    } finally {
      console.log('eixt write');
      NfcManager.cancelTechnologyRequest();
    }

    return result;
  };
  return (
    <View style={styles.wrapper}>
      <TouchableOpacity onPress={readNdef}>
        <Text>Scan a Tag</Text>
      </TouchableOpacity>
      <TouchableOpacity onPress={writeNFC}>
        <Text style={{color: 'grey'}}>Write Tag</Text>
      </TouchableOpacity>
    </View>
  );
}

const styles = StyleSheet.create({
  wrapper: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
  },
});

export default App;

版本信息:

  • Node.js 版本:
    v18.17.1
  • 反应版本:
    18.2.0
  • React Native 版本:
    0.73.4
  • react-native-nfc-manager
    版本:
    3.14.12

NFC标签信息:

enter image description here

期望的结果: 了解如何使用react-native-nfc-manager或其他方法在我的React Native应用程序中的NTAG216标签上实现密码保护。

android react-native nfc react-native-nfc-manager
1个回答
0
投票

一些答案

Q1)是的,可以使用react-native-nfc-manager设置和验证密码

Q2)密码设置不是 NFC 标准的一部分,各种标签硬件系列往往有不同的方式来实现密码保护,因此您必须在比 Ndef 数据格式更低的级别与标签进行交互。除了react-native-nfc-manager也可以使用的较低级别之外,其他库不太可能直接支持它。

因此,首先阅读一些背景知识,您应该阅读并理解 Ntag21x 的数据表,特别是第 8.8 节和 10.7 节

您的代码需要访问 NfcA 级别的标签。

所以编写类似的代码

await NfcManager.requestTechnology(NfcTech.NfcA);
// Build a byte array to send to Tag
const commandBytes = [162,...,...]
const responceBytes = await NfcManager.NfcAHandler.transceive(commandBytes)
// Check response
// Repeat until all the necessary commands are sent

设置密码的通用命令集在数据表中定义,并且与平台无关,并且有许多有关密码设置的 stackoverflow 问题,例如 https://stackoverflow.com/a/63259021/2373819

© www.soinside.com 2019 - 2024. All rights reserved.