android - Can't invoke NFC onNewIntent() method -
android - Can't invoke NFC onNewIntent() method -
i'm trying read id of mifare classic 1k card using foreground dispatch. can see logs, can enable foreground dispatch, can not invoke onnewintent() method. suggestions appreciated.
mainactivity.java
... @override protected void onresume() { setupforegrounddispatch(this, madapter); super.onresume(); } public static void setupforegrounddispatch(final activity activity, nfcadapter adapter) { final intent intent = new intent(activity.getapplicationcontext(), activity.getclass()); intent.setflags(intent.flag_activity_single_top); system.out.println("setup fgd."); // can see output. final pendingintent pendingintent = pendingintent.getactivity(activity.getapplicationcontext(), 0, intent, 0); intentfilter[] filters = new intentfilter[1]; string[][] techlist = new string[][]{}; // notice same filter in our manifest. filters[0] = new intentfilter(); filters[0].addaction(nfcadapter.action_ndef_discovered); filters[0].addcategory(intent.category_default); seek { filters[0].adddatatype(mime_text_plain); } grab (malformedmimetypeexception e) { throw new runtimeexception("check mime type."); } adapter.enableforegrounddispatch(activity, pendingintent, filters, techlist); system.out.println("enabled fgd."); // , one. } protected void onnewintent(intent intent) { system.out.println("intent."); cannot see one, handleintent(intent); } private void handleintent(intent intent) { system.out.println("handle."); // , one. string action = intent.getaction(); if (nfcadapter.action_ndef_discovered.equals(action)) { system.out.println("ndef discovered."); ....
androidmanifest
.... <activity android:name=".mainactivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> <intent-filter> <action android:name="android.nfc.action.tech_discovered" /> <action android:name="android.nfc.action.ndef_discovered" /> <action android:name="android.nfc.action.tag_discovered" /> </intent-filter> <meta-data android:name="android.nfc.action.tech_discovered" android:resource="@xml/nfc_tech_filter" /> </activity> ....
if want able observe every mifare classic tag (and not contain text record (or mime type record mime type text/plain), should adapt foreground dispatch observe specific tag technology rather specific ndef record type:
public static void setupforegrounddispatch(final activity activity, nfcadapter adapter) { final intent pendingintent = pendingintent.getactivity(activity, 0, new intent(activity, activity.getclass()).addflags(intent.flag_activity_single_top), 0); intentfilter[] filters = new intentfilter[] { new intentfilter(nfcadapter.action_tech_discovered) }; string[][] techlist = new string[][] { new string[] { mifareclassic.class.getname() } }; adapter.enableforegrounddispatch(activity, pendingintent, filters, techlist); }
if want uid of mifare classic tags on devices broadcom's nfc chipset (those devices unable observe mifare classic mifare classic due licensing issues of nxp's proprietary technology), instead filter nfca
tags (mifare classic detected nfca
on devices, don't need filter both nfca
, mifareclassic
):
string[][] techlist = new string[][] { new string[] { nfca.class.getname() } };
last, intent filter in manifest not match intent filter in code! foreground dispatch's equivalent intent filter be:
<intent-filter> <action android:name="android.nfc.action.ndef_discovered" /> <category android:name="android.intent.category.default" /> <data android:mimetype="text/plain" /> </intent-filter>
the manifest equivalent of foreground dispatch showed above be:
<intent-filter> <action android:name="android.nfc.action.tech_discovered" /> </intent-filter> <meta-data android:name="android.nfc.action.tech_discovered" android:resource="@xml/nfc_tech_filter" />
with nfc_tech_filter.xml
containing:
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2"> <tech-list> <tech>android.nfc.tech.mifareclassic</tech> </tech-list> </resources>
or (in case of matching nfca
):
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2"> <tech-list> <tech>android.nfc.tech.nfca</tech> </tech-list> </resources>
android android-intent nfc mifare
Comments
Post a Comment