Flutter NFC:如何防止/阻止“扫描新标签"Flutter Android Build中的默认活动? [英] Flutter NFC: How to prevent/stop "New Tag Scanned" default activity in Flutter Android Build?
问题描述
每个人我正在使用NFC插件"nfc_in_flutter"在我的flutter应用程序中,但是我面对的是已扫描新标签"android版本中的问题.每当我扫描标签时,第一次运行都很好,但是第二次自动/默认情况下,新标签已扫描"会自动运行.活动已打开.
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.
这是我的代码:
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:
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" />
推荐答案
您已将该插件配置为仅读取一张卡
You have configured the plugin to only read one card
尝试更改 .readNDEF(一次:true,readerMode:NFCDispatchReaderMode())
到 .readNDEF(一次:false,readerMode:NFCDispatchReaderMode())
因此您不要求插件仅读取一张卡.
so you don't ask the plugin to only read one card.
更新
我不是专家,但是在 await
上使用 timeout
和 first
意味着您将取消等待标签的操作10秒,仅在取消等待NFC事件之前等待第一个事件.这两个原因可能是它仅对第一张NFC卡做出响应的原因.
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.
这篇关于Flutter NFC:如何防止/阻止“扫描新标签"Flutter Android Build中的默认活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!