问题
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