-
Notifications
You must be signed in to change notification settings - Fork 124
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Android default NFC reader gets triggered #17
Comments
Sorry, I don't really understand your situation. On Android our native code will try to poll all types of NFC Technology with/without NDEF checking according to flags passed. You could refer to: flutter_nfc_kit/android/src/main/kotlin/im/nfc/flutter_nfc_kit/FlutterNfcKitPlugin.kt Lines 291 to 297 in 6471b59
Can you actually detect polled tag in your code (that is, does |
I have meant when I try to read NFC through my app, the default NFC reader of the phone opened. I want to prevent this and make the phone reads the NFC tag only inside my app. |
What you need might be the foreground dispatch mode, which is possible with Flutter, but needs some tricky implementation in native side. I do not have much time to handle this right now. You may try (and maybe send a PR!) that if you want to avoid the system stepping in when scanning tags. |
Would be nice to avoid the system stepping in when scanning tags. if (pendingIntent == null) { |
Currently we are using reader mode only. I am not sure whether enabling foreground dispatch and reader mode simultaneously leads to current behaviour. Have you tried to enable foreground dispatch without doing anything in |
I have tried foreground dispatching mode, but seems nothing changes . When you are not in reader mode, a tag scan will still trigger the system dialog. To solve this, I believe we have to implement a streaming mode with foreground dispatch, as I mentioned in #17 (comment). |
Hello, is there any update regarding this issue? |
@jacopofranza As mentioned above, I cannot solve this without adding support for streaming mode with foreground dispatch. However this would be a great change to our API, and requires a relatively large amount of work. So, sorry, but I cannot give any guarantee on this. |
This comment has been minimized.
This comment has been minimized.
Hi Mythar, |
As I wrote, do not write to a ntag, write to the card mem directly - depends on what software you are using to encode your cards. |
This is the real workaround to avoid the problem.
In res/xml create a new file named "nfc_tech_filter" with the intent types you want to filter:
In the MainActivity insert the following lines to enable foreground dispatch:
|
@jacopofranza Thanks for this! I will look into your solution later. |
I wrote this for direct memory read of the NFCTagType.mifare_ultralight:
|
I encountered similar issue on Android, why it happens and how I solved it: Issue:After write tag, I called Why:NFC session implementation on Android is actually simulated by turning activity into reader mode, which suppresses the default NFC behaviours. And finishing nfc turn off the reader mode, and then default Android NFC behaviour kicked in. Solution:On Android, I put a short delay after UI tells user NFC operation finished and before actually finish the NFC. And UI literally tells user to move the tag away from the phone. So it avoided the default NFC behaviour Hope it helps |
Thanks for the help! but Why if the tag is empty it keeps firing the alert to choose the app for scan? |
it's not gonna work , do you have any other way to read NFC memory, dear ? |
Thank you so much it works perfectly for me |
Thank you for this solution, works nicely. Only thing to add is that 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_IMMUTABLE)
adapter?.enableForegroundDispatch(this, pendingIntent, null, null)
}
override fun onPause() {
super.onPause()
val adapter: NfcAdapter? = NfcAdapter.getDefaultAdapter(this)
adapter?.disableForegroundDispatch(this)
} |
@sampie777 Where do you put those functions? My MainActivity.kt looks like, but I get errors when I try to run:
Errors:
|
@amilkarSingular yeah you are missing imports. Should be something like this: package <yourpackage>
import android.app.PendingIntent
import android.content.Intent
import android.nfc.NfcAdapter
import io.flutter.embedding.android.FlutterActivity
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_IMMUTABLE)
adapter?.enableForegroundDispatch(this, pendingIntent, null, null)
}
override fun onPause() {
super.onPause()
val adapter: NfcAdapter? = NfcAdapter.getDefaultAdapter(this)
adapter?.disableForegroundDispatch(this)
}
}
|
@sampie777 should this code work in case I want to listen for poll results when my app is in background state (not terminated) |
I don't know, as I've not tested this and I've not dug that deep into the code. As far as I know, the code only prevents Android from executing its default handler for NFC scans while the app is in foreground. The app still receives these scans. What happens when the app is in background: I don't know. Android will resume executing the default handler for NFC scans, but I think it's not part of this code if your app will also receive these scans. You may have to register your app as a default NFC scan listener or maybe just remove the
|
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
are there no solutions beyond the previous walkaround? |
This issue only occurs on Android whenever I try to scan NFC tag using my own app where it triggers the default NFC reader of the phone.
Is there a way to prevent it and read only from the app library?
The text was updated successfully, but these errors were encountered: