2013-04-05 2 views
0

튜토리얼 소스를 전혀 사용하지 않고 사용하고 있으며 NFC 태그를 읽지 않습니다.NFC 예제 소스 코드 : NFC 태그를 읽지 않습니다

태그를 스캔 할 때 아무런 효과가 없습니다.

으로 작성된 NFC 태그를 읽을 예정이다

: WIFINETWORKNAME, 비밀번호

^- 내 생각 - 튜토리얼 상태 "쓰기 네트워크 이름과 태그에 암호 (쉼표로 구분)"나는 시도했다 그것은 3 가지 다른 방법 - 아무것도 작동하지 않습니다.

내가하는 일과 상관없이 새로운 무선 프로필을 만들지 않습니다.

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

는 할 대상 :

시작을 나는 그것이

하지 않습니다 무엇 (필자는 태그에 AAR을 작성하는 경우)을 NFC 태그를 스캔 할 때

  • 새 무선 프로파일 만들기

  • WIFINETWORKNAME

    WIFINETWORKNAME, 비밀번호 :

    쓰기 다음으로 NFC 태그를 읽을 수 : 510,403,210 제대로

    내가 할 시도한 무엇

NFC 태그를 읽기 및/또는 연결 , 암호

WIFINETWORKNAME, 암호

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 a generic PendingIntent that will be deliver to this activity. 
     // The NFC stack 
     // will fill in the intent with the details of the discovered tag before 
     // delivering to 
     // this activity. 
     mPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, 
       getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0); 

     // Setup an intent filter for all 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 are not connected "); 
     }else{ 

      tv.setText("You are 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); 

     // only one message sent during the beam 
     NdefMessage msg = (NdefMessage) rawMsgs[0]; 

     // record 0 contains the MIME type, record 1 is the AAR, if present 
     Log.d("msg", msg.getRecords()[0].getPayload().toString()); 

     byte[]payload=msg.getRecords()[0].getPayload(); 
     String msgtext=null; 
     try{ 

     //Get the Text Encoding 
     String textEncoding = ((payload[0] & 0200) == 0) ? "UTF-8" : "UTF-16"; 

     //Get the Language Code 
     int languageCodeLength = payload[0] & 0077; 
     String languageCode = new String(payload, 1, languageCodeLength, "US-ASCII"); 

     //Get the Text 
     msgtext = new String(payload, languageCodeLength + 1, payload.length - languageCodeLength - 1, textEncoding); 
     }catch(Exception e){ 

     } 

     // splitting the message by comma. The first part is the name and 2nd part is the password. 
     String[]tagdata=msgtext.split(","); 

     String networkSSID = tagdata[0].toString(); 
     String networkPass = tagdata[1].toString(); 

     WifiConfiguration conf = new WifiConfiguration(); 
     conf.SSID = "\"" + networkSSID + "\""; // Please note the 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 i : 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 the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 

} 

내가 그것을 실행하려고 할 때 내 로그 캣에서 다음을 받고 있어요는 :

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)} from 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)} from 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 

답변

0

내가 튜토리얼은 지나치게 단순하다고 생각 그냥 그런 an application했다 가졌어요.

  1. 사용자/이름과 암호를 저장하는 데 텍스트 레코드가 사용 된 것처럼 보입니다. 사망에 의해 처벌 될 수있는 범죄는 아니지만 Well-known URI Record 나 Handover Records와 같은 더 나은 대안이 있으며 저자는 그것이 mime 레코드라고 생각하는 것 같습니다.

  2. 보안 유형에 대해서는 아무 것도 말하지 않기 때문에 네트워크를 추가해도 작동하지 않습니다. 나는 그 코드가 효과가 있었는지 의심 스럽다.

  3. 코드는 무선 관련 브로드 캐스트 이벤트를 완전히 무시합니다.이 이벤트는 실제 상황의 99 %에서 필요하지만 괜찮습니다.

+0

나는 그것이 새들에게는 느낌이었습니다. –

+0

내 것을 만들 수있게 도와 주겠습니까? :) 나는 뭔가 붙어있어 ... http://stackoverflow.com/questions/15843824/issues-passing-string-data-from-nfc-tag-reader-to-wifi-manager –

관련 문제