Skip to content

Commit

Permalink
add docs for tag event stream support
Browse files Browse the repository at this point in the history
  • Loading branch information
dangfan committed Dec 21, 2024
1 parent 1824bad commit 4b177f0
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,64 @@ To use this plugin on Android, you also need to:

* Add [android.permission.NFC](https://developer.android.com/reference/android/Manifest.permission.html#NFC) to your `AndroidManifest.xml`.

To receive NFC tag events even when your app is in the foreground, you can set up tag event stream support:

1. Create a custom Activity that extends `FlutterActivity` in your Android project:

```kotlin
package your.package.name

import android.app.PendingIntent
import android.content.Intent
import android.nfc.NfcAdapter
import android.nfc.Tag
import io.flutter.embedding.android.FlutterActivity
import im.nfc.flutter_nfc_kit.FlutterNfcKitPlugin

class MainActivity : FlutterActivity() {
override fun onResume() {
super.onResume()
val adapter: NfcAdapter? = NfcAdapter.getDefaultAdapter(this)
val pendingIntent: PendingIntent = PendingIntent.getActivity(
this, 0, Intent(this, javaClass).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), PendingIntent.FLAG_MUTABLE
)
// See https://developer.android.com/reference/android/nfc/NfcAdapter#enableForegroundDispatch(android.app.Activity,%20android.app.PendingIntent,%20android.content.IntentFilter[],%20java.lang.String[][]) for details
adapter?.enableForegroundDispatch(this, pendingIntent, null, null)
}

override fun onPause() {
super.onPause()
val adapter: NfcAdapter? = NfcAdapter.getDefaultAdapter(this)
adapter?.disableForegroundDispatch(this)
}

override fun onNewIntent(intent: Intent) {
val tag: Tag? = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG)
if (tag != null) {
FlutterNfcKitPlugin.handleTag(tag)
}
}
}
```

2. Update your `AndroidManifest.xml` to use this activity instead of the default Flutter activity.

3. In your Flutter code, listen to the tag event stream:

```dart
@override
void initState() {
super.initState();
// Listen to NFC tag events
FlutterNfcKit.tagStream.listen((tag) {
print('Tag detected: ${tag.id}');
// Process the tag
});
}
```

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.

### iOS

This plugin now supports Swift package manager, and requires iOS 13+.
Expand Down

0 comments on commit 4b177f0

Please sign in to comment.