2011-08-30 63 views
0

我一直在使用基於NFC的Android應用程序工作了幾個月。這個可以讀取&寫入NFC標籤作爲Android NFC docs說明。 (關於NFC API的相當不錯的文檔)。我一直在玩NFCDemo應用程序,當我需要一些示例來跟隨。Android NFC p2p處理

這是我目前的XML清單:

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
     package="com.my.package" 
     android:versionCode="1" 
     android:versionName="1.0.0"> 
    <uses-sdk android:minSdkVersion="10" /> 
    <uses-feature android:name="android.hardware.nfc" android:required="true" /> 
    <uses-permission android:name="android.permission.NFC" /> 
    <uses-permission android:name="android.permission.INTERNET" /> 
    <application android:icon="@drawable/icon" 
       android:label="@string/app_name" 
       android:launchMode="singleTask"> 
     <activity android:name=".MainActivity" 
        android:label="@string/app_name" 
        android:screenOrientation="portrait"> 
      <!-- the following actions and categories let the App be in Android Action Chooser and also Scan Tags when the app si running -->  
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> <!-- Main application --> 
       <category android:name="android.intent.category.LAUNCHER" /><!-- Logo Display in App List--> 
      </intent-filter> 

      <intent-filter> 
       <action android:name="android.nfc.action.TECH_DISCOVERED"/> 
      </intent-filter> 
      <meta-data android:name="android.nfc.action.TECH_DISCOVERED" 
           android:resource="@xml/nfc_tech_filter" /> 
      <intent-filter> 
       <action android:name="android.nfc.action.TAG_DISCOVERED"/> 
      </intent-filter> 

     </activity> 
    <activity android:name=".RouteActivity"> 
      <intent-filter> 
       <action android:name="android.nfc.action.NDEF_DISCOVERED"/> 
       <category android:name="android.intent.category.DEFAULT"/> 
       <data android:host="www.my.custom.tag.com" android:scheme="http" /> 
      </intent-filter> 
    </activity> 
</application> 
</manifest> 

這裏是tech_filter文件定義:

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

我還配置了Foreground Dispatch System

public void setUpForegroundDispatchSystem(Activity activity) { 
     this.nfcAdapter = NfcAdapter.getDefaultAdapter(activity); 

     this.pendingIntent = PendingIntent.getActivity(activity, 0, new Intent(activity, activity.getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0); 

     IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED); 
     this.intentFiltersArray = new IntentFilter[] { ndef }; 
     this.techListsArray = new String[][] { 
       new String[] { MifareUltralight.class.getName(), 
         Ndef.class.getName(), NfcA.class.getName() }, 
       new String[] { MifareClassic.class.getName(), 
         Ndef.class.getName(), NfcA.class.getName() }, 
       new String[] { MifareUltralight.class.getName(), 
         NdefFormatable.class.getName(), NfcA.class.getName() }, 
       new String[] { Ndef.class.getName(), NfcV.class.getName() }, 
       new String[] { NfcF.class.getName() }}; 
    } 

但現在,我想要將p2p功能添加到我的Android應用程序中。所以當我將標籤推送到其他手機並且我的應用程序已經安裝時,我希望Android Action選擇器被我的應用程序解僱。而且,如果我的應用程序已經運行,它必須處理一個p2p請求。 I could push p2p Tags correctly, using the Android docs about it,但唯一可以處理這個標籤的應用程序是Google的一款應用程序(Nexus S中的標籤應用程序),儘管我的手機中已經安裝了多個NFC應用程序。 有什麼想法?有關它的任何有用的文檔?

回答

1

已經解決。如果您需要在Android應用程序中處理P2P NFC請求。您需要處理android.nfc.action.TAG_DISCOVERED nfc類型。

所以,你的清單必須包括(請注意,類別爲默認值):

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

這將顯示Android的動作選擇器和您的應用程序將在這裏列出。如果你想還可以添加前景功能,你需要以這種方式來編輯前景調度系統(看原單,上面寫的):

IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED); 
IntentFilter tag = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED); 
this.intentFiltersArray = new IntentFilter[] { ndef , tag }; 

而這一切。