2014-11-26 2 views
0

NFC 태그 Read an NFC tag을 읽을 때만 메시지가 표시됩니다. 내가 놓친 게 무엇입니까? NFC ID을 감지하는 방법이 있습니까?nfc 태그를 표시하는 방법은 무엇입니까?

이어서 this 튜토리얼을 따랐습니다.

package com.example.zmynfc;  
import java.util.Arrays;  
import android.app.Activity; 
import android.app.PendingIntent; 
import android.content.Intent; 
import android.content.IntentFilter; 
import android.nfc.NdefMessage; 
import android.nfc.NdefRecord; 
import android.nfc.NfcAdapter; 
import android.nfc.Tag; 
import android.nfc.tech.NfcF; 
import android.os.Bundle; 
import android.os.Parcelable; 
import android.util.Log; 
import android.widget.TextView;  
public class MainActivity extends Activity {  
    private TextView  mTextView; 
    private NfcAdapter  mNfcAdapter; 
    private PendingIntent mPendingIntent; 
    private IntentFilter[] mIntentFilters; 
    private String[][]  mNFCTechLists;   
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main);   
     mTextView = (TextView)findViewById(R.id.textView1); 
     mNfcAdapter = NfcAdapter.getDefaultAdapter(this);  

     if (mNfcAdapter != null) { 
      mTextView.setText("Read an NFC tag"); 
     } else { 
      mTextView.setText("This phone is not NFC enabled."); 
     } 
     // create an intent with tag data and deliver to this activity 
     mPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);  
     // set an intent filter for all MIME data 
     IntentFilter ndefIntent = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED); 
     try { 
      ndefIntent.addDataType("*/*"); 
      mIntentFilters = new IntentFilter[] { ndefIntent }; 
     } catch (Exception e) { 
      Log.e("TagDispatch", e.toString()); 
     }  
     mNFCTechLists = new String[][] { new String[] { NfcF.class.getName() } }; 

    } 
    @Override 
    public void onNewIntent(Intent intent) { 
     String action = intent.getAction(); 
     Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);  
     String s = action + "\n\n" + tag.toString();    
    // parse through all NDEF messages and their records and pick text type only 
     Parcelable[] data = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES); 
     if (data != null) { 
      try { 
       for (int i = 0; i < data.length; i++) { 
        NdefRecord [] recs = ((NdefMessage)data[i]).getRecords(); 
        for (int j = 0; j < recs.length; j++) { 
         if (recs[j].getTnf() == NdefRecord.TNF_WELL_KNOWN && 
          Arrays.equals(recs[j].getType(), NdefRecord.RTD_TEXT)) { 
          byte[] payload = recs[j].getPayload(); 
          String textEncoding = ((payload[0] & 0200) == 0) ? "UTF-8" : "UTF-16"; 
          int langCodeLen = payload[0] & 0077; 

          s += ("\n\nNdefMessage[" + i + "], NdefRecord[" + j + "]:\n\"" + 
           new String(payload, langCodeLen + 1, payload.length - langCodeLen - 1, 
           textEncoding) + "\""); 
         } 
        } 
       } 
      } catch (Exception e) { 
       Log.e("TagDispatch", e.toString()); 
      } 
     }  
     mTextView.setText(s); 
    } 
    @Override 
    public void onResume() { 
     super.onResume();  
     if (mNfcAdapter != null) 
      mNfcAdapter.enableForegroundDispatch(this, mPendingIntent, mIntentFilters, mNFCTechLists); 
    }  
    @Override 
    public void onPause() { 
     super.onPause();  
     if (mNfcAdapter != null) 
      mNfcAdapter.disableForegroundDispatch(this); 
    } 
} 

답변

0

당신의에서 onCreate() 메소드이 추가 :

private NfcAdapter adapter = NfcAdapter.getDefaultAdapter(this); 
private PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, this.getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0); 

그런 다음 절편 NFC ID에 대한이 쓰기 :

public void onNewIntent(Intent intent) { 

    super.onNewIntent(intent); 

    byte[] id = intent.getByteArrayExtra(NfcAdapter.EXTRA_ID); 
    String rfid = byteToHex(id); 

} 
public String byteToHex(byte[] args) { 
    String risultato = ""; 

    for (int i = 0; i < args.length; i++) { 
     risultato += Integer.toString((args[i] & 0xff) + 0x100,16).substring(1); 
    } 
    return risultato; 
} 

public void enableForegroundMode() { 
    IntentFilter tagDetected = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED); 
    IntentFilter[] writeTagFilters = new IntentFilter[] {tagDetected}; 
    adapter.enableForegroundDispatch(this, pendingIntent, writeTagFilters, null); 
} 

public void disableForegroundMode() { 
    adapter.disableForegroundDispatch(this); 
} 

public void onResume() { 
    super .onResume(); 
    enableForegroundMode(); 
} 

public void onPause() { 
    super .onPause(); 
    disableForegroundMode(); 
} 
관련 문제