| 
1 | 1 | # Example of flutter_nfc_kit  | 
2 | 2 | 
 
  | 
3 |  | -## Simple usage  | 
 | 3 | +## Polling  | 
 | 4 | + | 
 | 5 | +This is the default operation mode and is supported on all platforms.  | 
 | 6 | +We recommend using this method to read NFC tags to ensure the consistency of cross-platform interactions.  | 
 | 7 | + | 
4 | 8 | 
 
  | 
5 | 9 | ```dart  | 
6 | 10 | import 'package:flutter_nfc_kit/flutter_nfc_kit.dart';  | 
@@ -71,6 +75,68 @@ await FlutterNfcKit.finish(iosAlertMessage: "Success");  | 
71 | 75 | await FlutterNfcKit.finish(iosErrorMessage: "Failed");  | 
72 | 76 | ```  | 
73 | 77 | 
 
  | 
 | 78 | +## Event Streaming  | 
 | 79 | + | 
 | 80 | +This is only supported on Android now. To receive NFC tag events even when your app is in the foreground, you can set up tag event stream support by:  | 
 | 81 | + | 
 | 82 | +1. Create a custom Activity that extends `FlutterActivity` in your Android project:  | 
 | 83 | + | 
 | 84 | +```kotlin  | 
 | 85 | +package your.package.name  | 
 | 86 | + | 
 | 87 | +import android.app.PendingIntent  | 
 | 88 | +import android.content.Intent  | 
 | 89 | +import android.nfc.NfcAdapter  | 
 | 90 | +import android.nfc.Tag  | 
 | 91 | +import io.flutter.embedding.android.FlutterActivity  | 
 | 92 | +import im.nfc.flutter_nfc_kit.FlutterNfcKitPlugin  | 
 | 93 | + | 
 | 94 | +class MainActivity : FlutterActivity() {  | 
 | 95 | +    override fun onResume() {  | 
 | 96 | +        super.onResume()  | 
 | 97 | +        val adapter: NfcAdapter? = NfcAdapter.getDefaultAdapter(this)  | 
 | 98 | +        val pendingIntent: PendingIntent = PendingIntent.getActivity(  | 
 | 99 | +            this, 0, Intent(this, javaClass).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), PendingIntent.FLAG_MUTABLE  | 
 | 100 | +        )  | 
 | 101 | +        // See https://developer.android.com/reference/android/nfc/NfcAdapter#enableForegroundDispatch(android.app.Activity,%20android.app.PendingIntent,%20android.content.IntentFilter[],%20java.lang.String[][]) for details   | 
 | 102 | +        adapter?.enableForegroundDispatch(this, pendingIntent, null, null)  | 
 | 103 | +    }  | 
 | 104 | + | 
 | 105 | +    override fun onPause() {  | 
 | 106 | +        super.onPause()  | 
 | 107 | +        val adapter: NfcAdapter? = NfcAdapter.getDefaultAdapter(this)  | 
 | 108 | +        adapter?.disableForegroundDispatch(this)  | 
 | 109 | +    }  | 
 | 110 | + | 
 | 111 | +    override fun onNewIntent(intent: Intent) {  | 
 | 112 | +        val tag: Tag? = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG)  | 
 | 113 | +        tag?.apply(FlutterNfcKitPlugin::handleTag)  | 
 | 114 | +    }  | 
 | 115 | +}  | 
 | 116 | +```  | 
 | 117 | + | 
 | 118 | +You may also invoke `enableForegroundDispatch` and `disableForegroundDispatch` in other places as needed.  | 
 | 119 | + | 
 | 120 | +2. Update your `AndroidManifest.xml` to use it as the main activity instead of the default Flutter activity.  | 
 | 121 | + | 
 | 122 | +3. In Flutter code, listen to the tag event stream and process events:  | 
 | 123 | + | 
 | 124 | +```dart  | 
 | 125 | +@override  | 
 | 126 | +void initState() {  | 
 | 127 | +  super.initState();  | 
 | 128 | +  // listen to NFC tag events  | 
 | 129 | +  FlutterNfcKit.tagStream.listen((tag) {  | 
 | 130 | +    print('Tag detected: ${tag.id}');  | 
 | 131 | +    // process the tag as in polling mode  | 
 | 132 | +    FlutterNfcKit.transceive("xxx", ...);  | 
 | 133 | +    // DO NOT call `FlutterNfcKit.finish` in this mode!  | 
 | 134 | +  });  | 
 | 135 | +}  | 
 | 136 | +```  | 
 | 137 | + | 
 | 138 | +This will allow your app to receive NFC tag events through a stream, which is useful for scenarios where you need continuous tag reading or want to handle tags even when your app is in the foreground but not actively polling.  | 
 | 139 | + | 
74 | 140 | ## GUI Application  | 
75 | 141 | 
 
  | 
76 | 142 | See `lib/main.dart` for a GUI application on Android / iOS / web. Skeleton code for specific platforms are not uploaded to <pub.dev>. Please refer to the [GitHub repository](https://github.com/nfcim/flutter_nfc_kit).  | 
0 commit comments