1

I implement NFC in my Flutter app using https://pub.dev/packages/nfc_in_flutter package. The thing is NFC is perfectly running in iOS app but in Android after scanning NFC, its automatically open "New Tag Scanned" / "Default Tag Viwer" Screen.

Any one can help me how can I prevent or stop this screen in Android ?

Thanjs

  • Android only shows the "New Tag Scanned" / "Default Tag Viewer" Screen if your App has not told it that you want to handle this type of Tag or Type of Data on the Tag. But without all your NFC handling code and details of the Tags and data type you are using then it is impossible to say where you have gone wrong. – Andrew Jan 02 '21 at 08:48
  • @Andrew https://stackoverflow.com/questions/65559235/flutter-nfc-how-to-prevent-stop-new-tag-scanned-default-activity-in-flutter-a Check this – ghulam__sabir Jan 04 '21 at 07:47

1 Answers1

0

I solved this by using the stream readers and writers instead of single read/write statements like this:

StreamSubscription<NDEFMessage> readFromTag() {

try {
  // ignore: cancel_subscriptions
  StreamSubscription<NDEFMessage> subscription =
      NFC.readNDEF().listen((tag) {
  
  }, onDone: () {
    _stream = null;
  }, onError: (e) {
    _stream = null;

    if (!(e is NFCUserCanceledSessionException)) {
      showDialog(
        context: Get.context,
        builder: (context) => AlertDialog(
          title: const Text("Error!"),
          content: Text(e.toString()),
        ),
      );
    }
  });

  return subscription;
} catch (err) {
  print("error: $err");
  _stream?.cancel();
  _stream = null;
  return _stream;
}
}

and then use this streams onData method to handle the the data on the tag like this:

StreamSubscription<NDEFMessage> _stream;
void _readNFC(BuildContext context) async {

if (_stream == null) {
  _stream = nfc.readFromTag();
  if (_stream != null) {
    _stream.onData((data) async {

      print("Data found in stream: ${data.payload}");
      if (readData != null || readData.isNotEmpty) {
       //handle data
      } else {
        print("Empty Tag!");
      }
    });
  }
}
}

and then don't forget to dispose the streams in dispose function.

Ayyaz meo
  • 450
  • 5
  • 13