【问题标题】:Android: How to read NFC tag with current appAndroid:如何使用当前应用读取 NFC 标签
【发布时间】:2014-09-01 16:59:27
【问题描述】:

我正在编写一个需要读取 NFC 标签的应用程序。而不是通过意图过滤器读取标签打开一个新应用程序的行为,我希望我当前打开的应用程序只是读取附近的标签。

我曾尝试使用 enableForegroundDispatch() 但没有成功。每当出现 NFC 标签时,我的设备只会打开标准的“标签”应用程序。

我目前的代码是:

final Intent intent = new Intent(activity.getApplicationContext(), activity.getClass());
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
final PendingIntent pendingIntent = PendingIntent.getActivity(activity.getApplicationContext(), 0, intent, 0);
IntentFilter[] filters = new IntentFilter[1];
String[][] techList = new String[][]{};
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);

(基本上是从我找到的教程中复制的)。

有人知道这是否是正确的方法吗? 如何防止打开标准的“标签”应用程序而不是我自己的应用程序接收 NDEF_DISCOVERED 意图。

谢谢

【问题讨论】:

  • 您的标签上有哪些数据?您想触发特定的标签类型(参见android.nfc.tech.* 命名空间)还是任何标签?
  • 我唯一需要的(我认为标签上唯一的东西)就是 ID
  • 这并不能真正回答我的问题。
  • 好的,抱歉...标签类型是 MifareClassic,所以我可能只想触发它。我只对标签 ID 感兴趣。

标签: android nfc


【解决方案1】:

首先,您必须在 AndroidMenifest.xml 文件中获得 nfc 的权限。权限是:

<uses-permission android:name="android.permission.NFC" />

<uses-feature android:name="android.hardware.nfc" />

将执行 Nfc 读/写操作的 Activity,在 menifest.xml 文件中的该 Activity 中添加此意图过滤器:

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

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

在您的活动 onCreate() 方法中,您必须初始化 NFC 适配器并定义 Pending Intent:

NfcAdapter mAdapter;
PendingIntent mPendingIntent;
mAdapter = NfcAdapter.getDefaultAdapter(this);   
if (mAdapter == null) {
    //nfc not support your device.
    return;
}
mPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this,
        getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);

在 onResume() 回调中启用 Foreground Dispatch 以检测 NFC 意图。

mAdapter.enableForegroundDispatch(this, mPendingIntent, null, null);

在 onPause() 回调中,您必须禁用前台调度:

if (mAdapter != null) {
    mAdapter.disableForegroundDispatch(this);
}

在 onNewIntent() 回调方法中,您将获得新的 Nfc Intent。获取到 Intent 后,需要解析 Intent 来检测卡片:

    @Override
protected void onNewIntent(Intent intent){    
    getTagInfo(intent)
     }
private void getTagInfo(Intent intent) {
Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
}

现在你有了标签。然后您可以检查标签技术列表以检测该标签。标签检测技术在My Another Answer 完整项目在My GitHub Repo

【讨论】:

    【解决方案2】:

    您的代码当前注册前台调度系统以仅在包含 NDEF 文本记录(或“text/plain”MIME 类型记录)的 NFC 标签上触发。因此,如果您的标签不包含此类记录,则 NFC 发现事件将传递给其他应用(或在您的情况下由标准标签应用处理)。

    因此,您可能希望为适合您的标签的意图注册前台调度系统:

    IntentFilter[] filters = new IntentFilter[1];
    filters[0] = new IntentFilter();
    filters[0].addAction(NfcAdapter.ACTION_TECH_DISCOVERED);
    filters[0].addCategory(Intent.CATEGORY_DEFAULT);
    String[][] techList = new String[][]{ new String[] { NfcA.class.getName() } };
    adapter.enableForegroundDispatch(activity, pendingIntent, filters, techList);
    

    或者您可以只注册以触发任何标签:

    adapter.enableForegroundDispatch(activity, pendingIntent, null, null);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-12-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-09-30
      • 1970-01-01
      • 2014-12-03
      相关资源
      最近更新 更多