2011-08-30 68 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 }; 

而这一切。