使用Android NFC检测MIFARE Classic时启动应用程序

时间:2014-05-07 23:55:50

标签: android android-intent nfc mifare

我在使用MIFARE方面有很多经验(自1996年以来,使用GEMPLUS制造的卡片时)。我甚至编写了低级代码来模拟MIFARE卡......但是现在事情变得更简单,更高级别我无法使用Java,Android和Android Studio!我想我会随着时间变得笨拙......

我所要做的就是在检测到MIFARE卡时启动应用程序。我知道它可以完成,因为我在我的设备中使用了NFC卡信息应用程序,它在MIFARE卡的存在下正确启动。我已经卸载它以确保唯一的NFC应用程序是我自己的。我试图遵循http://developer.android.com/guide/topics/connectivity/nfc/nfc.htmlhttp://developer.android.com/guide/topics/connectivity/nfc/advanced-nfc.html上不太令人满意的文档。

问题是我的应用程序从未启动过。代码非常简单,AFAIK应该正常工作......这是app清单:

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.nfctest.app" >

    <uses-sdk android:minSdkVersion="10"/>
    <uses-permission android:name="android.permission.NFC" />
    <uses-feature android:name="android.hardware.nfc" android:required="true" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.nfctest.app.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.NDEF_DISCOVERED"/>
                <category android:name="android.intent.category.DEFAULT"/>
            </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" />
        </activity>
    </application>
</manifest>

按NDEF_DISCOVERED过滤,因为TECH_DISCOVERED无法单独使用。

这是nfc_tech_filter资源文件:

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
    <tech-list>
        <tech>android.nfc.tech.Ndef</tech>
        <tech>android.nfc.tech.NdefFormatable</tech>
        <tech>android.nfc.tech.MifareClassic</tech>
        <tech>android.nfc.tech.MifareUltralight</tech>
    </tech-list>
</resources>

这是活动代码:

package com.example.nfctest.app;

import android.content.Intent;
import android.nfc.NfcAdapter;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity {

    TextView label;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        label = (TextView)findViewById(R.id.label);
    }

    @Override
    public void onResume() {
        super.onResume();
        if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent().getAction())) {
            label.setText("NDEF_DISCOVERED");
        } else if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(getIntent().getAction())) {
            label.setText("TECH_DISCOVERED");
        }
    }

    @Override
    public void onNewIntent(Intent intent) {
        label.setText("onNewIntent!!!");
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

活动布局非常简单:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="com.example.nfctest.app.MainActivity">
    <TextView
        android:id="@+id/label"
        android:text="@string/hello_world"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</RelativeLayout>

我需要开始编程实际的NFC代码,只要检测到TAG就启动应用程序。文档让声音变得如此简单,以至于我确信它再次成为我的愚蠢......

更新

我可以使用foregroundDispatcher通过将活动代码更改为:

来正确读取标记UID
package com.example.nfctest.app;

import android.app.PendingIntent;
import android.content.Intent;
import android.content.IntentFilter;
import android.nfc.NfcAdapter;
import android.nfc.Tag;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.widget.TextView;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;

public class MainActivity extends ActionBarActivity {

    TextView label;
    IntentFilter[] filters;
    String[][] techs;
    PendingIntent pendingIntent;
    NfcAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        label = (TextView)findViewById(R.id.label);
        pendingIntent = PendingIntent.getActivity(
                this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
        IntentFilter mifare = new IntentFilter((NfcAdapter.ACTION_TECH_DISCOVERED));
        filters = new IntentFilter[] { mifare };
        techs = new String[][] { new String[] {  NfcA.class.getName() } };
        adapter = NfcAdapter.getDefaultAdapter(this);
    }

    public void onPause() {
        super.onPause();
        adapter.disableForegroundDispatch(this);
    }

    public void onResume() {
        super.onResume();
        adapter.enableForegroundDispatch(this, pendingIntent, filters, techs);
    }

    public void onNewIntent(Intent intent) {
        Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
        byte[] id = tag.getId();
        ByteBuffer wrapped = ByteBuffer.wrap(id);
        wrapped.order(ByteOrder.LITTLE_ENDIAN);
        int signedInt = wrapped.getInt();
        long number = signedInt & 0xffffffffl;
        label.setText("Tag detected: " + number);
    }
}

我甚至不需要在清单中设置任何意图过滤器来实现此功能,只需像这样的简单清单即可:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.nfctest.app" >

    <uses-sdk android:minSdkVersion="10"/>
    <uses-permission android:name="android.permission.NFC" />
    <uses-feature android:name="android.hardware.nfc" android:required="true" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.nfctest.app.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

使用此代码,当我的应用程序位于前台时,我可以读取MIFARE卡序列号,但我仍然无法进行设置,以便ANDROID在后台启动我的活动...

当检测到标签(MIFARE或NOT)时,我仍然不知道如何让ANDROID启动我的app / activity。

2 个答案:

答案 0 :(得分:5)

您的问题是技术过滤器XML文件(因为您最终正确地发现了自己)。您最初使用的技术过滤器,

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
    <tech-list>
        <tech>android.nfc.tech.Ndef</tech>
        <tech>android.nfc.tech.NdefFormatable</tech>
        <tech>android.nfc.tech.MifareClassic</tech>
        <tech>android.nfc.tech.MifareUltralight</tech>
    </tech-list>
</resources>

没有任何意义。一个<tech>条目中的<tech-filter>条目使用逻辑AND组合。

因此,您需要拥有Ndef NdefFormatable MifareClassic MifareUltralight。这有两个原因:

  1. NdefNdefFormatable是互斥的。标签可以 已经包含NDEF数据/空NDEF消息( - &gt; Ndef它可能已准备好使用NDEF消息进行格式化( - &gt; NdefFormatable)。
  2. MifareClassicMifareUltralight是互斥的。标签 可以是MIFARE Classic标签 MIFARE Ultralight标签,但不能同时为两者。
  3. 因此,例如,在包含NDEF消息的标签上触发的正确技术过滤器是MIFARE Classic或MIFARE Ultralight,如下所示:

    <?xml version="1.0" encoding="utf-8"?>
    <resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
        <tech-list>
            <tech>android.nfc.tech.Ndef</tech>
            <tech>android.nfc.tech.MifareClassic</tech>
        </tech-list>
        <tech-list>
            <tech>android.nfc.tech.Ndef</tech>
            <tech>android.nfc.tech.MifareUltralight</tech>
        </tech-list>
    </resources>
    

    所有<tech-list>条目都与逻辑OR合并:(Ndef MifareClassic Ndef MifareUltralight)。

    在你的情况下,你似乎对NDEF不太感兴趣,但更多的是从MIFARE卡中获取任何东西。假设MIFARE你的意思是MIFARE Classic卡(因为你在谈论1996 ;-)),你的技术过滤器应该是这样的:

    <?xml version="1.0" encoding="utf-8"?>
    <resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
        <tech-list>
            <tech>android.nfc.tech.MifareClassic</tech>
        </tech-list>
    </resources>
    

    但请注意,这仅适用于采用NXP芯片组的设备(MIFARE Classic是专有的恩智浦技术。虽然恩智浦将卡侧授权给其他制造商,但他们并未授权读卡器端。因此,只有 1 < / sup>恩智浦的阅读器产品阅读MIFARE Classic。)。在具有Broadcom芯片组的设备上,MIFARE Classic卡 2 未被检测为MIFARE Classic。但由于MIFARE Classic使用标准的ISO 14443-3 A型防冲突和激活序列,这些设备通常 3 检测到NfcA这样的卡:

    <?xml version="1.0" encoding="utf-8"?>
    <resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
        <tech-list>
            <tech>android.nfc.tech.NfcA</tech>
        </tech-list>
    </resources>
    

    请注意,使用带有MifareClassic标记技术的第二个条目将是多余的,因为检测到MIFARE Classic的每个设备都会将其检测为MifareClassic {{1} }。


    1 )该规则有例外,因为有些制造商不同意恩智浦的立场(或者只是忽略它?)在读者端实施MIFARE Classic支持会侵犯他们的权利。

    2 )并且只有MIFARE Classic 。这不适用于符合标准的MIFARE产品,如Ultralight,DESFire,NTAG等。

    3 )一些采用Broadcom NFC芯片组的三星设备,如S4,是该规则的例外。在这些设备上,三星决定完全禁止MIFARE Classic,而是显示不支持&#34;标签。错误。正如他们所声称的那样,为了改善用户体验,用户不会理解他们为什么不能将数据写入这些标签。或者解释它,让你的用户讨厌作为应用开发者不能使你的应用工作在他们的带有标签的手机。查看ReTag appown app上的否定评论。

答案 1 :(得分:3)

经过大量的搜索和挫折后,我开始尝试并意识到您无法过滤所有Nfc标签类型。他们中的一些人并不能与其他人合作(例如NfcA和MifareClassic)。我没有试图弄清楚所有可能的无效组合,但只有NfcA过滤才能解决问题(MifareClassic和Ultralight也会被检测到,因为它们的行为类似于NfcA)。

技术列表改为:

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

清单改为:

<uses-sdk android:minSdkVersion="10"/>
<uses-permission android:name="android.permission.NFC" />
<uses-feature android:name="android.hardware.nfc" android:required="true" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.nfctest.app.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
            <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" />
    </activity>
</application>

然后它奏效了!该活动按预期启动。

在ANDROID文档中没有提到这一点。我发现它缺乏大部分时间我需要参考它!但这就是生命......另一个不眠之夜(巴西上午4点),今天的问题少了一个。 : - )