2013-01-15 35 views
1

我正在嘗試讀取NFC標籤ID。我知道我無法讀取數據,因爲它是一個安全標籤(MBTA票價卡)。我想閱讀標籤唯一的ID和烤麪包的價值。我成功地獲得了NFC意圖,以便在掃描標籤時我的應用會嘗試處理它。但是,應用不會顯示帶有標籤ID的敬酒。從前臺調度中讀取標籤NFC ID

package com.example.nfctest; 

import android.nfc.NdefMessage; 
import android.nfc.NfcAdapter; 
import android.nfc.Tag; 
import android.nfc.tech.NfcF; 
import android.os.Bundle; 
import android.app.Activity; 
import android.app.PendingIntent; 
import android.content.Intent; 
import android.content.IntentFilter; 
import android.content.IntentFilter.MalformedMimeTypeException; 
import android.util.Log; 
import android.view.Menu; 
import android.widget.Toast; 

public class MainActivity extends Activity { 
    private NfcAdapter mAdapter; 
     private PendingIntent mPendingIntent; 
     private IntentFilter[] mFilters; 
     private String[][] mTechLists; 
     private int mCount = 0; 

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

     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); 
    try { 
     ndef.addDataType("*/*"); 
    } catch (MalformedMimeTypeException e) { 
     throw new RuntimeException("fail", e); 
    } 
    mFilters = new IntentFilter[] { 

    }; 

    // Setup a tech list for all NfcF tags 
    mTechLists = new String[][] { new String[] { NfcF.class.getName() } }; 
} 
public void onResume() { 
    super.onResume(); 
    mAdapter.enableForegroundDispatch(this, mPendingIntent, mFilters, mTechLists); 

} 

@Override 
public void onNewIntent(Intent intent) { 
    Log.i("Foreground dispatch", "Discovered tag with intent: " + intent); 
    CharSequence text = ("Discovered tag " + ++mCount + " with intent: " + intent); 
    int duration = Toast.LENGTH_SHORT; 

    Tag myTag = (Tag) intent.getParcelableExtra(NfcAdapter.EXTRA_TAG); 


    Toast toast = Toast.makeText(MainActivity.this,myTag.getId().toString() , duration); 
    toast.show(); 
} 


@Override 
public void onPause() { 
    super.onPause(); 
    //mAdapter.disableForegroundDispatch(this); 
    throw new RuntimeException("onPause not implemented to fix build"); 
} 



@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    getMenuInflater().inflate(R.menu.activity_main, menu); 
    return true; 
} 

} 

它不再崩潰,只是沒有做與ID的吐司。

+0

你可以發佈你從logcat獲得的異常嗎? – palako

+0

[鏈接](http://pastebin.com/0XVA2L6f)粘貼時間太長。 – Dandrews

+0

這應該會給你一個提示:沒找到類「com.example.nfctest.nfc.TechFilter」 – palako

回答

0

我強烈建議這this sample applicationthis link。不要像我一樣,盡一切努力使自己的行動順利進行,從而讓自己變得痛苦。

您可能在onResume和onNewIntent中的操作位置上做錯了事情。但是,在我鏈接到的示例中,它具有onResume和onNewIntent中的新標記和ndef控件。如果你真的想繼續自己的項目,試試這個。

+0

該示例確實有效,但是它確實比我需要的更好。我只需要掃描標籤,獲取其唯一ID並根據它執行一些代碼。對於一個班級來說,所以我不能真正使用它,我必須在一定程度上學習它。 – Dandrews

+0

我想,寫作比去除更復雜。以調試模式打開項目並逐步觀察過程,您將輕鬆發現應該刪除的內容。這就是我所做的:) –