Flutter NFC: How to prevent/stop “New Tag Scanned” default activity in Flutter Android Build?

半腔热情 提交于 2021-01-29 05:06:34

问题


Everyone I'm using NFC plugin "nfc_in_flutter" in my flutter app but I'm facing "New Tag Scanned" issue in android build. Whenever I scan tag, First time, it works good but on second time automatically/bydefault "New Tag Scanned" activity open.

Here is my code:

Future<String> _nFCscan() async {
    String nfcdataString = "";
    setState(() {
      _inAsyncCall = true;
    });
    try {
      NDEFMessage message = await NFC
          .readNDEF(once: true, readerMode: NFCDispatchReaderMode())
          .timeout(Duration(seconds: 10))
          .first;
      if (message != null && message.payload != '') {
        setState(() => nfcdataString = message.data);
      }
    } on PlatformException catch (e) {
      if (e.code == BarcodeScanner.cameraAccessDenied) {
        Fluttertoast.showToast(
          msg: 'The user has\'t granted permission to the camera!',
        );
      } else {
        Fluttertoast.showToast(
          msg: 'Unknown error: ' + "$e",
        );
      }
    } on FormatException {} catch (e) {}

    setState(() {
      _inAsyncCall = false;
    });

    print(nfcdataString);
    if (nfcdataString != null) {
      return nfcdataString;
    } else {
      Fluttertoast.showToast(
        msg: 'NFC data issue',
      );
      return null;
    }
  }

Android Manifest.xml:

  <intent-filter>
          <action android:name="android.nfc.action.NDEF_DISCOVERED" />
          <category android:name="android.intent.category.DEFAULT" />
          <data android:scheme="*" />
  </intent-filter>


 <uses-permission android:name="android.permission.NFC" /> 
    <uses-feature android:name="android.hardware.nfc" android:required="false" />

回答1:


You have configured the plugin to only read one card

try changing .readNDEF(once: true, readerMode: NFCDispatchReaderMode())

to .readNDEF(once: false, readerMode: NFCDispatchReaderMode())

so you don't ask the plugin to only read one card.

Update

I'm not a flutter expert but the use of timeout and first on the await means you will cancel waiting for a tag after 10 seconds and only wait for the first event before cancelling waiting for NFC events. Both of these might be a reason why it only responds to the first NFC card.



来源:https://stackoverflow.com/questions/65559235/flutter-nfc-how-to-prevent-stop-new-tag-scanned-default-activity-in-flutter-a

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!