c# - NFC Action_Tech_Discovered with foreground dispatch won't catch Mifare 1k card -
i'm coding in c# xamarin , trying scan mifare classic 1k card nfc.
the intent-filter of m1card_test working fine. don't want select activity want start. i'm trying use foreground dispatch.
here part of code (c#):
oncreate
intent myintent = new intent(this, gettype()); myintent.addflags(activityflags.singletop); mpendingintent = pendingintent.getactivity(this, 0, myintent, 0); ndefdetected = new intentfilter(nfcadapter.actiontechdiscovered); ndefdetected.adddatatype("*/*"); intentf = new intentfilter[] { ndefdetected }; techlists = new string[][] {new string[] { typeof(android.nfc.tech.nfca).fullname, typeof(android.nfc.tech.mifareclassic).fullname} };
onpause
nfcmanager manager = (nfcmanager)getsystemservice(nfcservice); manager.defaultadapter.disableforegrounddispatch(this);
onresume
nfcmanager manager = (nfcmanager)getsystemservice(nfcservice); manager.defaultadapter.enableforegrounddispatch(this,mpendingintent,intentf,techlists);
unfortunately, foreground dispatch doesn't work (i.e. not pick tag).
if change call enableforegrounddispatch()
to
manager.defaultadapter.enableforegrounddispatch(this,mpendingintent,null,null);
the foreground dispatch work fine. picks tags , not mifare classic , intent action_tag_discovered instead of action_tech_discovered.
how use action_tech_discovered foreground dispatch system?
did miss something?
tech_list.xml:
<?xml version="1.0" encoding="utf-8" ?> <resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2"> <tech-list> <tech>android.nfc.tech.nfca</tech> <tech>android.nfc.tech.mifareclassic</tech> </tech-list> </resources>
androidmanifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="m1card_test.m1card_test" android:versioncode="1" android:versionname="1.0"> <uses-sdk android:minsdkversion="16" /> <application android:label="m1card_test"></application> <uses-permission android:name="android.permission.nfc" /> </manifest>
my c# code:
using system; using system.collections.generic; using system.linq; using system.text; using android.app; using android.content; using android.os; using android.runtime; using android.views; using android.widget; using android.nfc; namespace m1card_test { [activity(label = "m1_read", icon = "@drawable/icon", launchmode = android.content.pm.launchmode.singletask)] [intentfilter( new[] {nfcadapter.actiontechdiscovered}, categories = new[] {intent.categorydefault,})] [metadata("android.nfc.action.tech_discovered", resource = "@xml/tech_list")] public class m1_read : activity { textview mtv; pendingintent mpendingintent; intentfilter ndefdetected; intentfilter[] intentf; string[][] techlists; protected override void oncreate(bundle savedinstancestate) { base.oncreate(savedinstancestate); setcontentview(resource.layout.m1_read); intent myintent = new intent(this, gettype()); myintent.addflags(activityflags.singletop); mpendingintent = pendingintent.getactivity(this, 0, myintent, 0); ndefdetected = new intentfilter(nfcadapter.actiontechdiscovered); ndefdetected.adddatatype("*/*"); intentf = new intentfilter[] { ndefdetected }; techlists = new string[][] {new string[] { typeof(android.nfc.tech.nfca).fullname, typeof(android.nfc.tech.mifareclassic).fullname} }; button button = findviewbyid<button>(resource.id.back_button); mtv = findviewbyid<textview>(resource.id.textview); button.click += delegate { intent main_intent = new intent(this, typeof(mainactivity)); this.startactivity(main_intent); finish(); }; } protected override void onpause() { base.onpause(); nfcmanager manager = (nfcmanager)getsystemservice(nfcservice); manager.defaultadapter.disableforegrounddispatch(this); } protected override void onresume() { base.onresume(); nfcmanager manager = (nfcmanager)getsystemservice(nfcservice); manager.defaultadapter.enableforegrounddispatch(this, mpendingintent,intentf,techlists); } protected override void onnewintent(intent intent) { base.onnewintent(intent); mtv.text = "onnewintent"; } } }
the tech_discovered
intent filter not have data-type (mime type) associated it. need remove line
ndefdetected.adddatatype("*/*");
moreover, i'm not quite sure if typeof(android.nfc.tech.mifareclassic).fullname
resolves correct name (full java class name) of tag technology. thus, should hard-code string (just in tech-filter xml file):
techlists = new string[][] { new string[] { "android.nfc.tech.nfca", "android.nfc.tech.mifareclassic" }};
finally, since mifareclassic tag technology implies nfca, can safely reduce tech-filter just
techlists = new string[][] { new string[] { "android.nfc.tech.mifareclassic" }};
Comments
Post a Comment