android - Issue in writing NDEF message to NFC tag -
i trying write ndef message nfc tag. have code write ndef message in onnewintent(). control not going onnewintent(). after onresume(), hanging. pls find below code.
public class mainactivity extends activity { private bluetoothadapter mbluetoothadapter; private nfcadapter mnfcadapter; static final byte[] type_bt_oob = "application/vnd.bluetooth.ep.oob". getbytes(charset.forname("us_ascii")); private ndefmessage mndefmessage; string mlocalbluetoothaddress; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); toast.maketext(this, "inside oncreate", toast.length_long).show(); mnfcadapter = nfcadapter.getdefaultadapter(this); if (mnfcadapter == null) { // stop here, need nfc toast.maketext(this, "this device doesn't support nfc.", toast.length_long).show(); finish(); return; } } public void onresume() { super.onresume(); toast.maketext(this, "inside onresume", toast.length_long).show(); nfcadapter nfcadapter = nfcadapter.getdefaultadapter(this); pendingintent pendingintent = pendingintent.getactivity(this, 0, new intent(this, getclass()).addflags(intent.flag_activity_single_top), 0); nfcadapter.enableforegrounddispatch(this, pendingintent, null, null); } public void onnewintent(intent intent) { toast.maketext(this, "inside onnewintent", toast.length_long).show(); if (nfcadapter.action_tag_discovered.equals(intent.getaction())) { tag tag = intent.getparcelableextra(nfcadapter.extra_tag); if (tag != null) { ndef ndef = ndef.get(tag); if (ndef != null) { try { ndef.connect(); ndef.writendefmessage(createhandoverrequestmessage()); } catch (exception e) { log.e("tagwriting", e.tostring()); } { try { ndef.close(); } catch (exception e) { } } } } } } public void onpause() { super.onpause(); nfcadapter nfcadapter = nfcadapter.getdefaultadapter(this); nfcadapter.disableforegrounddispatch(this); }
i appreciate sorted out.
check manifest file see
<activity android:name=".mainactivity" android:alwaysretaintaskstate="true" android:configchanges="orientation|keyboardhidden|screensize" android:label="@string/app_name" android:launchmode="singleinstance" android:screenorientation="nosensor" >
android:alwaysretaintaskstate="true"
(i use following method , works) if fine try catching pending intent @ oncreate().
public class mainactivity extends activity { private bluetoothadapter mbluetoothadapter; private nfcadapter mnfcadapter; static final byte[] type_bt_oob = "application/vnd.bluetooth.ep.oob". getbytes(charset.forname("us_ascii")); private ndefmessage mndefmessage; private pendingintent pendingintent; string mlocalbluetoothaddress; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); toast.maketext(this, "inside oncreate", toast.length_long).show(); mnfcadapter = nfcadapter.getdefaultadapter(this); if (mnfcadapter == null) { // stop here, need nfc toast.maketext(this, "this device doesn't support nfc.", toast.length_long).show(); finish(); return; } else { pendingintent = pendingintent.getactivity(this, 0, new intent(this, getclass()).addflags(intent.flag_activity_single_top), 0); ontagfound(getintent()); } } public void onresume() { super.onresume(); toast.maketext(this, "inside onresume", toast.length_long).show(); nfcadapter nfcadapter = nfcadapter.getdefaultadapter(this); if (nfcadapter != null && pendingintent != null) { nfcadapter.enableforegrounddispatch(this, pendingintent, null, null); } } public void onnewintent(intent intent) { toast.maketext(this, "inside onnewintent", toast.length_long).show(); ontagfound(intent); } public void ontagfound(intent intent) { if (nfcadapter.action_tag_discovered.equals(intent.getaction())) { tag tag = intent.getparcelableextra(nfcadapter.extra_tag); if (tag != null) { ndef ndef = ndef.get(tag); if (ndef != null) { try { ndef.connect(); ndef.writendefmessage(createhandoverrequestmessage()); } catch (exception e) { log.e("tagwriting", e.tostring()); } { try { ndef.close(); } catch (exception e) { } } } } } } public void onpause() { super.onpause(); nfcadapter nfcadapter = nfcadapter.getdefaultadapter(this); nfcadapter.disableforegrounddispatch(this); } }
Comments
Post a Comment