开发者

Android NFC p2p handling

I've been working for a couple of months with a NFC based Android Application. This one can read&write NFC Tags as the Android NFC docs explain. (pretty good docs about NFC api). I've been playing with the NFCDemo app when I need some example to follow.

Here is my current XML Manifest:

<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>

Here is the tech_filter file definition:

<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>

I had also configured the 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() }};
    }

But now, I want to add p2p capabilities to my Android application. So when I push a Tag to other phone with my app already installed I want the Android Action chooser being fired with my app. And also if my app is already running, it must handle a p2p request. I could push p2p Tags correctly, using the Android docs about it, but the only app that can handle this Tags is the Google's one (Tags application that becomes with Nexus S), despite of I've several NFC apps already installed in m开发者_如何转开发y phone. Any ideas? Any useful documentation about it?


Already solved. If you need to handle P2P NFC requests in your Android App. You need to handle the android.nfc.action.TAG_DISCOVERED nfc type.

So your manifest must include (note that the category is DEFAULT):

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

This will display the Android Action Chooser and your app will be listed here. If you wanna also add foreground capabilities, you need to edit the foreground dispatch system in this way (look at the original one, wrote above):

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

And that's all.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜