2014-09-01 99 views
0

我正在編寫需要讀取NFC標籤的應用。而不是閱讀標籤通過意圖過濾器打開一個新的應用程序的行爲,我希望我目前打開的應用程序只需讀取附近的標籤。Android:如何使用當前應用讀取NFC標籤

我試圖使用enableForegroundDispatch()但沒有運氣。無論何時出現NFC標籤,我的設備都會打開標籤「標籤」應用程序。

我到目前爲止的代碼是:

final Intent intent = new Intent(activity.getApplicationContext(), activity.getClass()); 
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); 
final PendingIntent pendingIntent = PendingIntent.getActivity(activity.getApplicationContext(), 0, intent, 0); 
IntentFilter[] filters = new IntentFilter[1]; 
String[][] techList = new String[][]{}; 
filters[0] = new IntentFilter(); 
filters[0].addAction(NfcAdapter.ACTION_NDEF_DISCOVERED); 
filters[0].addCategory(Intent.CATEGORY_DEFAULT); 
try { 
    filters[0].addDataType(MIME_TEXT_PLAIN); 
} catch (MalformedMimeTypeException e) { 
    throw new RuntimeException("Check your mime type."); 
} 
adapter.enableForegroundDispatch(activity, pendingIntent, filters, techList); 

(這基本上是從一個教程,我發現複製)。

有誰知道這是否是正確的方法去做這件事? 如何防止標準「標籤」應用打開,而不是我自己的應用收到NDEF_DISCOVERED意圖。

感謝

+0

什麼數據你有你的標籤?你想觸發一個特定的標籤類型(見'android.nfc.tech。*'命名空間)或任何標籤? – 2014-09-01 17:46:40

+0

我需要的唯一東西(我認爲標籤上唯一的東西)是ID – chris 2014-09-02 08:05:18

+0

這並不能真正回答我的問題。 – 2014-09-02 08:40:35

回答

0

你的代碼目前登記前臺調度系統只有在包含了NDEF文本記錄(或「text/plain的」 MIME類型記錄)NFC標籤來觸發。因此,如果您的標籤不包含這樣的記錄,那麼NFC發現事件會傳遞給其他應用(或者由您的標準標籤應用處理)。

因此,您可能要註冊的前景調度系統適合您標籤的意圖:

IntentFilter[] filters = new IntentFilter[1]; 
filters[0] = new IntentFilter(); 
filters[0].addAction(NfcAdapter.ACTION_TECH_DISCOVERED); 
filters[0].addCategory(Intent.CATEGORY_DEFAULT); 
String[][] techList = new String[][]{ new String[] { NfcA.class.getName() } }; 
adapter.enableForegroundDispatch(activity, pendingIntent, filters, techList); 

或者你可以只登記在任何標記觸發:

adapter.enableForegroundDispatch(activity, pendingIntent, null, null); 
5

第一所有您必須在AndroidMenifest.xml文件中獲得nfc權限。權限是:

<uses-permission android:name="android.permission.NFC" /> 

<uses-feature android:name="android.hardware.nfc" /> 

將執行NFC讀/寫操作的行爲,在menifest.xml文件活動添加此意圖過濾:

<intent-filter> 
     <action android:name="android.nfc.action.TAG_DISCOVERED" /> 

     <category android:name="android.intent.category.DEFAULT" /> 
    </intent-filter> 

在您的活動onCreate()方法你必須初始化NFC適配器和定義待定意圖:

NfcAdapter mAdapter; 
PendingIntent mPendingIntent; 
mAdapter = NfcAdapter.getDefaultAdapter(this); 
if (mAdapter == null) { 
    //nfc not support your device. 
    return; 
} 
mPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, 
     getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0); 

在的onResume()回撥使前景調度來檢測NFC意圖。

mAdapter.enableForegroundDispatch(this, mPendingIntent, null, null); 

在的onPause()回調您必須禁用於地面調度:

if (mAdapter != null) { 
    mAdapter.disableForegroundDispatch(this); 
} 

在onNewIntent()回調方法,你會得到新的NFC意向。獲得該意圖後,您必須解析檢測該卡的意圖:

@Override 
protected void onNewIntent(Intent intent){  
    getTagInfo(intent) 
    } 
private void getTagInfo(Intent intent) { 
Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG); 
} 

現在您已經有標籤。然後,您可以檢查標籤技術列表以檢測該標籤。標籤檢測技術在這裏My Another Answer 完整的項目是在My GitHub Repo

+0

非常感謝!爲我工作 – GrafOrlov 2015-12-01 15:03:48

+0

歡迎兄弟:) – 2015-12-01 16:40:27