java - NFC Example Source Code: Does Not Read NFC Tags -


i'm using tutorial source - without changing @ - , won't read nfc tag.

when scan tag - nothing.

it supposed read nfc tag written as: wifinetworkname,password

^ - think - tutorial states "write network name , password (separate comma) tag" , i've tried 3 different ways - nothing works.

nor create new wireless profile - no matter do.

http://schoolofcode.wordpress.com/2013/03/20/connect-to-wifi-using-android-nfc-phone-and-nfc-tag/

what do:

launch when scan nfc tag (if write aar tag)

what not do:

  • create new wireless profiles

  • read nfc tag and/or connect properly

what i've tried do:

write read nfc tag as:

wifinetworkname,password

wifinetworkname, password

wifinetworkname , password

java:

import java.util.list; import android.net.wifi.wificonfiguration; import android.net.wifi.wifiinfo; import android.net.wifi.wifimanager; import android.nfc.ndefmessage; import android.nfc.nfcadapter; import android.os.bundle; import android.os.parcelable; import android.app.activity; import android.app.pendingintent; import android.content.context; import android.content.intent; import android.content.intentfilter; import android.util.log; import android.view.menu; import android.view.view; import android.view.view.onclicklistener; import android.widget.button; import android.widget.edittext; import android.widget.textview;  public class connect2 extends activity { private edittext wifiname; private string password; private  nfcadapter madapter; private  pendingintent mpendingintent;     @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.connect2);          madapter = nfcadapter.getdefaultadapter(this);         // create generic pendingintent deliver activity.         // nfc stack         // fill in intent details of discovered tag before         // delivering         // activity.         mpendingintent = pendingintent.getactivity(this, 0, new intent(this,                 getclass()).addflags(intent.flag_activity_single_top), 0);          // setup intent filter mime based dispatches         intentfilter ndef = new intentfilter(nfcadapter.action_ndef_discovered);          wifimanager wifimgr = (wifimanager)getsystemservice(context.wifi_service);         wifiinfo wifiinfo = wifimgr.getconnectioninfo();         string connectionname = wifiinfo.getssid();          textview tv=(textview)findviewbyid(r.id.wifiname);         if(connectionname==null){             tv.settext("you not connected  ");         }else{              tv.settext("you linked to: "+connectionname+"   ");         }          button close=(button)findviewbyid(r.id.close);         close.setonclicklistener(new onclicklistener() {              @override             public void onclick(view v) {                 // todo auto-generated method stub                 finish();                 system.exit(0);             }         });      }     @override     public void onresume(){         super.onresume();          if(nfcadapter.action_tag_discovered.equals(getintent().getaction())||nfcadapter.action_ndef_discovered.equals(getintent().getaction())){             processreadintent(getintent());         }     }     @override     public void onnewintent(intent intent){         setintent(intent);     }     public void processreadintent(intent intent){         parcelable[] rawmsgs = intent.getparcelablearrayextra(                 nfcadapter.extra_ndef_messages);          // 1 message sent during beam         ndefmessage msg = (ndefmessage) rawmsgs[0];          // record 0 contains mime type, record 1 aar, if present         log.d("msg", msg.getrecords()[0].getpayload().tostring());          byte[]payload=msg.getrecords()[0].getpayload();         string msgtext=null;         try{          //get text encoding         string textencoding = ((payload[0] & 0200) == 0) ? "utf-8" : "utf-16";          //get language code         int languagecodelength = payload[0] & 0077;         string languagecode = new string(payload, 1, languagecodelength, "us-ascii");          //get text         msgtext = new string(payload, languagecodelength + 1, payload.length - languagecodelength - 1, textencoding);         }catch(exception e){          }          // splitting message comma. first part name , 2nd part password.         string[]tagdata=msgtext.split(",");          string networkssid = tagdata[0].tostring();         string networkpass = tagdata[1].tostring();          wificonfiguration conf = new wificonfiguration();         conf.ssid = "\"" + networkssid + "\"";   // please note quotes. string should contain ssid in quotes          conf.presharedkey = "\""+ networkpass +"\"";         wifimanager wifimanager = (wifimanager)getsystemservice(context.wifi_service);          wifimanager.addnetwork(conf);          list<wificonfiguration> list = wifimanager.getconfigurednetworks();         for( wificonfiguration : list ) {             if(i.ssid != null && i.ssid.equals("\"" + networkssid + "\"")) {                  wifimanager.disconnect();                  wifimanager.enablenetwork(i.networkid, true);                  wifimanager.reconnect();                                 break;             }                     }          textview wifiname=(textview)findviewbyid(r.id.wifiname);         wifiname.settextcolor( getresources().getcolor(r.color.solid_red) );         wifiname.settext("successfully linked "+networkssid +"   ");      }     @override     public boolean oncreateoptionsmenu(menu menu) {         // inflate menu; adds items action bar if present.         getmenuinflater().inflate(r.menu.main, menu);         return true;     }  }    

i'm getting following in logcat when attempting execute it:

04-05 13:08:37.107: d/nfcdispatcher(905): dispatch tag: tag: tech [android.nfc.tech.mifareultralight, android.nfc.tech.nfca, android.nfc.tech.ndef] message: ndefmessage [ndefrecord tnf=1 type=54 payload=02656e414e4452452d50435f4e4554574f524b2c2070617373776f7264] 04-05 13:08:37.107: d/nfchandover(905): tryhandover(): ndefmessage [ndefrecord tnf=1 type=54 payload=02656e414e4452452d50435f4e4554574f524b2c2070617373776f7264] 04-05 13:08:37.117: i/activitymanager(478): start u0 {flg=0x10008000 cmp=com.android.nfc/.nfcrootactivity (has extras)} pid 905 04-05 13:08:37.137: i/nfcdispatcher(905): matched multiple tech 04-05 13:08:37.147: i/activitymanager(478): start u0 {cmp=com.android.nfc/.techlistchooseractivity (has extras)} pid 905 04-05 13:08:37.317: d/dalvikvm(905): gc_concurrent freed 7783k, 22% free 15072k/19148k, paused 9ms+9ms, total 52ms 04-05 13:08:37.327: i/activitymanager(478): displayed com.android.nfc/.techlistchooseractivity: +185ms (total +196ms) 04-05 13:08:38.247: d/nativenfctag(905): tag lost, restarting polling loop 

having made an application think tutorial overly simple.

  1. it seems use text record store user/name , password. not crime punishable death there better alternatives well-known uri record or handover records, , author seems think mime record.

  2. it says nothing security type, adding network never work. doubt code has ever worked.

  3. the code totally ignores wifi-related broadcast events, if fine, necessary in 99% of real-life cases.


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -