【问题标题】:Can't invoke NFC onNewIntent() method无法调用 NFC onNewIntent() 方法
【发布时间】:2014-06-22 01:27:46
【问题描述】:

我正在尝试使用前台调度读取 Mifare Classic 1k 卡的 ID。从我的日志中可以看出,我可以启用前台调度,但不能调用 onNewIntent() 方法。任何建议将不胜感激。

MainActivity.java

...
@Override
   protected void onResume() {
      setupForegroundDispatch(this, mAdapter);
      super.onResume();
} 

public static void setupForegroundDispatch(final Activity activity, NfcAdapter adapter) {
    final Intent intent = new Intent(activity.getApplicationContext(), activity.getClass());
    intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    System.out.println("Setup FGD.");  // i can see this output.

    final PendingIntent pendingIntent = PendingIntent.getActivity(activity.getApplicationContext(), 0, intent, 0);

    IntentFilter[] filters = new IntentFilter[1];
    String[][] techList = new String[][]{};

    // Notice that this is the same filter as in our manifest.
    filters[0] = new IntentFilter();
    filters[0].addAction(NfcAdapter.ACTION_NDEF_DISCOVERED);
    filters[0].addCategory(Intent.CATEGORY_DEFAULT);
    try {
        filters[0].addDataType(MIME_TEXT_PLAIN);
    } catch (MalformedMimeTypeException e) {
        throw new RuntimeException("Check your mime type.");
    }

    adapter.enableForegroundDispatch(activity, pendingIntent, filters, techList);
    System.out.println("Enabled FGD.");  // and this one.
}

protected void onNewIntent(Intent intent) { 
    System.out.println("Intent."); but i cannot see this one,
    handleIntent(intent);
}

private void handleIntent(Intent intent) {      
    System.out.println("Handle.");  // and this one.
    String action = intent.getAction();
    if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)) {
        System.out.println("NDEF discovered.");
         ....

AndroidManifest

....
 <activity
        android:name=".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.NDEF_DISCOVERED" />
            <action android:name="android.nfc.action.TAG_DISCOVERED" />
         </intent-filter>

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

【问题讨论】:

  • 您的标签是否包含一条以 Text 记录(或 MIME 类型为 text/plain 的 MIME 类型记录)作为第一条记录的 NDEF 消息?
  • 实际上,当我将 ACTION_NDECF_DISCOVERED 之后的 ACTION_TECH_DISCOVERED 操作添加到我的 NfcAdapter 时,它就像魔术一样解决了。但也感谢您的解决方案。
  • 它并没有真正“像魔术一样解决”,但实际上回答了我的问题:所以你的标签不包含这样的 NDEF 记录,你需要一种更通用的方法来检测标签(通过它的标签技术而不是它的内容)。

标签: android android-intent nfc intentfilter mifare


【解决方案1】:

如果您希望能够检测每个 MIFARE Classic 标签(而不仅仅是那些包含文本记录(或 MIME 类型为 text/plain 的 MIME 类型记录)的标签,您应该调整前台调度以检测特定标签技术而不是特定的 NDEF 记录类型:

public static void setupForegroundDispatch(final Activity activity, NfcAdapter adapter) {
    final Intent pendingIntent = PendingIntent.getActivity(activity, 0, new Intent(activity, activity.getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);

    IntentFilter[] filters = new IntentFilter[] { new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED) };
    String[][] techList = new String[][] { new String[] { MifareClassic.class.getName() } };

    adapter.enableForegroundDispatch(activity, pendingIntent, filters, techList);
}

如果您还想在具有 Broadcom NFC 芯片组的设备上获取 MIFARE Classic 标签的 UID(由于 NXP 专有技术的许可问题,这些设备无法将 MIFARE Classic 检测为 MIFARE Classic),您可以改为过滤所有 @ 987654322@ 标签(MIFARE Classic 在所有设备上都将被检测为NfcA,因此您无需同时过滤NfcAMifareClassic):

    String[][] techList = new String[][] { new String[] { NfcA.class.getName() } };

最后,清单中的意图过滤器与代码中的意图过滤器不匹配!您的前台调度的等效意图过滤器将是:

<intent-filter>
    <action android:name="android.nfc.action.NDEF_DISCOVERED" />
    <category android:name="android.intent.category.DEFAULT" />
    <data android:mimeType="text/plain" />
</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" />

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

或者(如果匹配NfcA):

<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
    <tech-list>
        <tech>android.nfc.tech.NfcA</tech>
    </tech-list>
</resources>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多