我如何在我的 flutter 应用程序中读取 NTAG424 NFC 标签,我不断收到“NDEF 未找到错误”

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

我正在开发一个 flutter 项目,使用 flutter_nfc_kit 扫描(读取) uri 有效负载的 NFC 标签,然后将该有效负载用于其他活动,我没有进行任何写入,焦点标签类型是 NTAG424 NFC 标签,我在使用 2 型标签时没有任何问题。

问题是该功能适用于 2 型标签(NTAG213、216 等),但是当我尝试扫描 NTAG424 标签时,它返回一个 “ndef 未找到错误”,我使用

flutter_nfc_kit: ^3.5.2
,我已经使用 nfc 进行了测试像“NFC 工具”和“NXP TagWriter”这样的工具,所以我确认标签功能正常并且我可以看到编码的 uri。 以下是扫描功能的相关代码。

Future<void> _startNfcScan(BuildContext context) async {
  try {
    // Show the bottom sheet for Android (iOS already has a native NFC prompt)
    if (!Platform.isIOS) {
      await _showReadyToScanSheet(context);
    }

    // Start the NFC polling session
    var tag = await FlutterNfcKit.poll(timeout: Duration(seconds: 10));

    // Check if NDEF is available and read NDEF content
    if (tag.ndefAvailable == true) {
      // Read decoded NDEF records
      List<ndef.NDEFRecord> ndefRecords = await FlutterNfcKit.readNDEFRecords();

      if (ndefRecords.isNotEmpty) {
        var firstRecord = ndefRecords.first;

        // Ensure the payload is not null
        if (firstRecord.payload != null && firstRecord.payload!.isNotEmpty) {
          // Manually decode the compressed URI
          String decodedUrl = _decodeUriPayload(firstRecord.payload!);

          // Log or print the full decoded URL
          print('Decoded URL from NFC tag: $decodedUrl');

          // Now you have the full URL,do something with it
          await _doSomething(context, decodedUrl);

          // Show the "Scan Complete" bottom sheet after scanning
          if (!Platform.isIOS) {
            await _showScanCompleteSheet(context);
          }
        } else {
          ScaffoldMessenger.of(context).showSnackBar(
            SnackBar(content: Text('No payload found on the NFC tag.')),
          );
        }
      } else {
        ScaffoldMessenger.of(context).showSnackBar(
          SnackBar(content: Text('No NDEF records found on the tag.')),
        );
      }
    } else {
      ScaffoldMessenger.of(context).showSnackBar(
        SnackBar(content: Text('NDEF not available on this tag.')),
      );
    }
  } catch (e) {
    // Handle errors during NFC scanning
    ScaffoldMessenger.of(context).showSnackBar(
      SnackBar(content: Text('Failed to read NFC tag: $e')),
    );
  } finally {
    // Finish the NFC session
    await FlutterNfcKit.finish(iosAlertMessage: 'Scan Complete');
  }
}

String _decodeUriPayload(List<int> payload) {
  // The first byte in the URI payload is the prefix byte
  final int prefixByte = payload[0];

  // The rest of the payload is the actual URI content
  final uriContent = utf8.decode(payload.sublist(1));

  // Map the prefix byte to the correct URL scheme
  const prefixMap = {
    0x00: '', // No prefix
    0x01: 'http://www.',
    0x02: 'https://www.',
    0x03: 'http://',
    0x04: 'https://',
    // 
  };

  // Get the prefix based on the prefix byte
  final prefix = prefixMap[prefixByte] ?? '';

  // Return the full URL by combining the prefix with the URI content
  return prefix + uriContent;
}

flutter flutter-dependencies nfc
1个回答
0
投票

我将其发布为答案,因为这对我有用:

  • 我不得不切换到使用nfc_manager 然后将其添加到 info.plist,现在我可以使用我的应用程序成功扫描 NTAG424 DNA 标签。

     <key>com.apple.developer.nfc.readersession.felica.systemcodes</key>
       <array>
         <string>8005</string>
         <string>8008</string>
         <string>0003</string>
         <string>fe00</string>
         <string>90b7</string>
         <string>927a</string>
         <string>86a7</string>
       </array>
      <key>com.apple.developer.nfc.readersession.iso7816.select-identifiers</key>
      <array>
          <string>A0000002471001</string>
          <string>A000000003101001</string>
          <string>A000000003101002</string>
          <string>A0000000041010</string>
          <string>A0000000042010</string>
          <string>A0000000044010</string>
          <string>44464D46412E44466172653234313031</string>
          <string>D2760000850100</string>
          <string>D2760000850101</string>
          <string>00000000000000</string>
      </array>
    

所以这似乎更多是一个不完整的设置问题,为那些对完整故事感兴趣的人写了一篇文章。 https://medium.com/@timidev34/solving-the-ndef-not-available-error-when-working-with-ntag424-nfc-tags-on-ios-in-flutter-26928edd717f

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