2013-05-12 129 views
4

我正在編寫一款可與NFC和MIFARE CARD配合使用的應用程序。Android nfc intent-filter在nfc發現標籤時顯示我的應用程序

當我的NFC設備檢測到卡時,它會顯示可以使用NFC的應用程序列表,但我的應用程序未提及。

我在android清單文件中丟失了什麼?

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

<uses-sdk android:minSdkVersion="14" android:targetSdkVersion="17" /> 

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

<application 
    android:icon="@drawable/ic_launcher"   android:allowBackup="true" 
    android:label="@string/app_name" android:theme="@style/AppTheme" > 
    <activity android:uiOptions="splitActionBarWhenNarrow" 
     android:name="it.namespace.app.MainActivity" 
     android:label="@string/app_name" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
     <intent-filter> 
      <action android:name="android.nfc.action.TECH_DISCOVERED" /> 
      <action android:name="android.nfc.action.TAG_DISCOVERED" /> 
      <category android:name="android.intent.category.DEFAULT" /> 
     </intent-filter> 
    <meta-data 
      android:name="android.nfc.action.TECH_DISCOVERED" 
      android:resource="@xml/tech_filter" /> 
    </activity> 
</application> 

這是我tech_filter文件的XML:

<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2" > 

    <tech-list> 
     <tech> 
android.nfc.tech.MifareClassic 
     </tech> 
    </tech-list> 

</resources> 

這裏顯示的是我的應用程序沒有在列表中的圖像: enter image description here

回答

1

你有沒有創建了一個技術列表資源?

來源:http://developer.android.com/guide/topics/connectivity/nfc/nfc.html#tech-disc

<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2"> 
    <tech-list> 
     <tech>android.nfc.tech.IsoDep</tech> 
     <tech>android.nfc.tech.NfcA</tech> 
     <tech>android.nfc.tech.NfcB</tech> 
     <tech>android.nfc.tech.NfcF</tech> 
     <tech>android.nfc.tech.NfcV</tech> 
     <tech>android.nfc.tech.Ndef</tech> 
     <tech>android.nfc.tech.NdefFormatable</tech> 
     <tech>android.nfc.tech.MifareClassic</tech> 
     <tech>android.nfc.tech.MifareUltralight</tech> 
    </tech-list> 
</resources> 

如果過濾的android.nfc.action.NDEF_DISCOVERED而不是android.nfc.action.TECH_DISCOVERED,你並不需要一個技術列表。

你目前所擁有的應該放在android.nfc.action.TAG_DISCOVERED中(請參閱所引用頁面上的流程圖​​)。

由於所有這些應用程序處理NDEF_DISCOVERED,所以很可能正在生成應用程序列表。 NFC調度員的一般意圖是創建一個意圖並將其交付給匹配的第一個應用程序。應用選擇器僅在多個應用匹配過濾器時纔會顯示。按照流程圖看起來,匹配停止時可以調度匹配的動作。

<intent-filter> 
    <action android:name="android.nfc.action.NDEF_DISCOVERED" /> 
    <action android:name="android.nfc.action.TECH_DISCOVERED" /> 
    <action android:name="android.nfc.action.TAG_DISCOVERED" /> 
    <category android:name="android.intent.category.DEFAULT" /> 
</intent-filter> 
+0

我已成立,但仍然沒有工作 – michele 2013-05-12 18:07:01

+0

嘗試增加對android.nfc添加動作許可

。 action.NDEF_DISCOVERED,可能有多個應用程序正在過濾這些內容,而列表僅基於這些應用程序。 – 2013-05-12 18:15:25

+0

嗯我該怎麼做?我不確定我是否理解。 – michele 2013-05-12 18:22:14

11

我有同樣的問題,我固定它基礎上,這句話在Android的文檔http://developer.android.com/guide/topics/connectivity/nfc/nfc.html#tech-disc

「如果爲ACTION_TECH_DISCOVERED意圖的活動過濾器,必須創建一個指定技術的XML資源文件您的活動在技術列表集內支持。如果技術列表集是標記支持的技術的子集,則您的活動被認爲是匹配的,您可以通過調用getTechList()來獲得這些技術的子組合。例如,如果掃描的標籤支持MifareClassic,NdefFormatable和NfcA,您的技術列表集必須指定所有三種,兩種或一種技術(而不是其他任何技術),以便您的活動得到匹配。「

您的nfc_tech_list需要定義當前標籤支持的技術的一個子集。

-define您的清單如下:

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

     <meta-data android:name="android.nfc.action.TECH_DISCOVERED" 
         android:resource="@xml/nfc_tech_list" /> 

</activity> 

-define的XML nfc_check_list這樣的:

<?xml version="1.0" encoding="utf-8"?> 
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2"> 
    <tech-list> 
     <tech>android.nfc.tech.NdefFormatable</tech> 
     <tech>android.nfc.tech.MifareUltralight</tech> 
    </tech-list> 

    <tech-list> 
     <tech>android.nfc.tech.NfcA</tech> 
     <tech>android.nfc.tech.Ndef</tech> 
    </tech-list> 

    <tech-list> 
     <tech>android.nfc.tech.NfcB</tech> 
     <tech>android.nfc.tech.Ndef</tech> 
    </tech-list> 
</resources> 

這將很好地工作。

+0

這幫助我花了2小時後,試圖解決它! – 2014-12-11 03:34:49

+0

對於其他人,如果您想知道您的標籤支持的「技術列表」,您可以使用名爲「NFC TagInfo」的應用程序進行檢查。 – 2014-12-11 03:36:44

0

做到這一點,也記得要添加到manifest.xml中

<uses-sdk android:minSdkVersion="12" /> 
<uses-feature 
    android:name="android.hardware.nfc" 
    android:required="true" />