2013-01-08 45 views
0

已經閱讀了許多類似於我的問題 - 如果我錯過了解決問題的方法,我只能道歉,但我真的無法解決這個問題!如何在NDEF發現/活動啓動後顯示NDEF消息?

我設法讓我的NFC活動工作 - 攻標籤啓動我的應用程序的正確行爲 - 但不顯示NDEF消息,我想不通爲什麼...

的標籤帶有純文本信息編碼的Mifare Ultralight。 (mimetype:plain/text)

這是我的代碼,謝謝大家的幫助,這個論壇應該有一個'捐一杯啤酒'按鈕!

package com.example.prototype02; 

import android.app.Activity; 
import android.app.AlertDialog; 
import android.app.PendingIntent; 
import android.content.Intent; 
import android.content.IntentFilter; 
import android.nfc.NdefMessage; 
import android.nfc.NfcAdapter; 
import android.os.Bundle; 
import android.os.Parcelable; 
import android.widget.Toast; 

public class nfcActivity extends Activity { 
    private static final String TAG = "NFCReadTag"; 
    private NfcAdapter mNfcAdapter; 
    private IntentFilter[] mNdefExchangeFilters; 
    private PendingIntent mNfcPendingIntent; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.nfclayout); 

     mNfcAdapter = NfcAdapter.getDefaultAdapter(this); 

     mNfcPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, 
       getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP 
       | Intent.FLAG_ACTIVITY_CLEAR_TOP), 0); 


     IntentFilter nfcIntent = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED); 
     nfcIntent.addDataScheme("text/plain");  
     mNdefExchangeFilters = new IntentFilter[] { nfcIntent }; 

    } 

    @Override 
    protected void onPause() { 
     super.onPause(); 
     if(mNfcAdapter != null) mNfcAdapter.disableForegroundDispatch(this); 
    } 

    @Override 
    protected void onNewIntent(Intent intent) { 
     super.onNewIntent(intent);  
     if (mNfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())) { 
      NdefMessage[] messages = null; 
      Parcelable[] rawMsgs = intent.getParcelableArrayExtra(mNfcAdapter.EXTRA_NDEF_MESSAGES); 
      if (rawMsgs != null) { 
       messages = new NdefMessage[rawMsgs.length]; 
       for (int i = 0; i < rawMsgs.length; i++) { 
        messages[i] = (NdefMessage) rawMsgs[i]; 
       }} 
       else if (rawMsgs == null){ 
        Toast.makeText(getApplicationContext(), "No NDEF Message Read", Toast.LENGTH_LONG).show(); 
       } 
      if(messages[0] != null) { 
       String result=""; 
       byte[] payload = messages[0].getRecords()[0].getPayload(); 
       for (int b = 1; b<payload.length; b++) { // skip SOH 
        result += (char) payload[b]; 
       } 
       Toast.makeText(getApplicationContext(), "Safe Location Registered - " + result, Toast.LENGTH_SHORT).show(); 
      } 
     } 
     else Toast.makeText(getApplicationContext(), "Intent Error...", Toast.LENGTH_LONG).show(); 

     } 
    } 
+0

我已經發表了一些模板activites這裏:http://code.google.com/p/ndef-tools-for-android/ – ThomasRS

回答

0

如果你的活動是由NFC意向開始,然後onNewIntent()不會被調用(當你掃描標籤時,活動已經在前臺它纔會被調用)。嘗試在onCreate()之內撥打onNewIntent(getIntent())

+0

非常感謝你 - 對於延遲響應,我們表示歉意。這解決了我的問題,第一個問題(新)...現在顯示NDEF消息,除了郵件的第一個字符...我在另一個nfc閱讀應用程序中檢查了標籤,並驗證了第一個字符是那裏。任何想法將不勝感激!我確定我錯過了明顯的。 – user1593869

+0

謝謝!再次 - 很抱歉延遲 - 我沒有看到你的評論重新陣列 - 愚蠢的打字錯誤:) – user1593869

0

嘗試這樣:(從StickyNotes NFC樣品取)

@Override 
protected void onResume() { 
    super.onResume(); 
    mResumed = true; 
    // Sticky notes received from Android 
    if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent().getAction())) { 
     NdefMessage[] messages = getNdefMessages(getIntent()); 
     byte[] payload = messages[0].getRecords()[0].getPayload(); 
     setNoteBody(new String(payload)); 
     setIntent(new Intent()); // Consume this intent. 
    } 
    enableNdefExchangeMode(); 
} 

@Override 
protected void onPause() { 
    super.onPause(); 
    mResumed = false; 
    mNfcAdapter.disableForegroundNdefPush(this); 
} 

@Override 
protected void onNewIntent(Intent intent) { 
    // NDEF exchange mode 
    if (!mWriteMode && NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())) { 
     NdefMessage[] msgs = getNdefMessages(intent); 
     promptForContent(msgs[0]); 
    } 

    // Tag writing mode 
    if (mWriteMode && NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())) { 
     Tag detectedTag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG); 
     writeTag(getNoteAsNdef(), detectedTag); 
    } 
}