首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android p2p处理

Android p2p处理
EN

Stack Overflow用户
提问于 2011-08-30 13:19:33
回答 1查看 4.1K关注 0票数 0

我已经在一个基于NFC的Android应用程序上工作了几个月。这个可以像Android NFC文档解释的那样读写NFC标记。(非常好的关于NFC的文档)。当我需要学习一些例子的时候,我一直在玩NFCDemo应用程序。

下面是我当前的XML声明:

代码语言:javascript
复制
<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文件的定义:

代码语言:javascript
复制
<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>

我还配置了前景调度系统

代码语言:javascript
复制
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选择器。而且,如果我的应用程序已经在运行,它必须处理p2p请求。我可以正确地推送p2p标签,使用Android的文档。,但是唯一能处理这个标签的应用是Google的应用程序(标签应用程序变成了Nexus ),尽管我的手机上已经安装了几个NFC应用程序。有什么想法吗?有关于它的有用文档吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-08-30 17:51:40

已经解决了。如果您需要在Android中处理P2P NFC请求。您需要处理发现 nfc类型。

因此,您的清单必须包括(请注意,类别是默认的):

代码语言:javascript
复制
    <intent-filter>
        <action android:name="android.nfc.action.TAG_DISCOVERED"/>
        <category android:name="android.intent.category.DEFAULT"/>
    </intent-filter>

这将显示Android行动选择器,您的应用程序将在这里列出。如果还想添加前台功能,则需要以这种方式编辑前台调度系统(查看上面所写的原始系统):

代码语言:javascript
复制
IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
IntentFilter tag = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED);
this.intentFiltersArray = new IntentFilter[] { ndef , tag };

仅此而已。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7243796

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档