Android 如何阅读ISODEP NFC卡

Android 如何阅读ISODEP NFC卡,android,android-intent,nfc,Android,Android Intent,Nfc,我必须编写一个简单的应用程序来读取ISO B卡 protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.tag_viewer); mTagContent = (LinearLayout) findViewById(R.id.list); mTitle = (TextView) findViewBy

我必须编写一个简单的应用程序来读取ISO B卡

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.tag_viewer);
    mTagContent = (LinearLayout) findViewById(R.id.list);
    mTitle = (TextView) findViewById(R.id.title);
    resolveIntent(getIntent());
}



void resolveIntent(Intent intent) {
    // Parse the intent
    String action = intent.getAction();
    Log.e(TAG, "toto ");
    if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(action)) {
    try{
            myTag.connect();
            if (myTag.isConnected()){
                byte[] response = myTag.transceive(command);
                //logText(new String(response));
                Log.e(TAG, "Result " + response);
            }
            myTag.close();

        }catch (IOException e) {
               e.printStackTrace();
        }

    } else {
        Log.e(TAG, "Unknown intent " + intent);
        finish();
        return;
    }
}
我的问题是如果(NfcAdapter.ACTION\u TECH\u DISCOVERED.equals(ACTION))我没有成功进入测试。我有“未知意图”

关于舱单:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.nfc">
<uses-permission android:name="android.permission.NFC" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<application
    android:icon="@drawable/icon"
    android:label="@string/app_name"
>
       <activity android:name="TagViewer"
        android:theme="@android:style/Theme.NoTitleBar"
    >
    <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"/>
            <category android:name="android.intent.category.DEFAULT"/>
        </intent-filter>
            <meta-data android:name="android.nfc.action.TECH_DISCOVERED"
                android:resource="@xml/nfc_tech_filter" />
    </activity>
</application>
<uses-sdk android:minSdkVersion="9" />
<uses-feature android:name="android.hardware.nfc" android:required="true" />


谢谢您的帮助。

您的问题似乎是,您正在onCreate方法中调用resolveIntent

这应该可以做到:

    @Override
protected void onNewIntent(Intent pIntent) {
    super.onNewIntent(pIntent);

    resolveIntent(pIntent);

}
要获取标记对象,我只需使用

Tag tagFromIntent = pIntent.getParcelableExtra(NfcAdapter.EXTRA_TAG);

        NfcA tag = NfcA.get(tagFromIntent);
用我的方法


很遗憾,我无法为NfcB验证这一点。

您需要创建一个NfcAdapter对象

 private NfcAdapter mNfcAdapter;
然后在onCreate方法中将对象与NFC关联

mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
在启动应用程序之前进行一些检查,以确保手机具有NFC功能

if (mNfcAdapter == null) { 
  // Stop here, we definitely need NFC
  Toast.makeText(this, "This device doesn't support NFC.", Toast.LENGTH_LONG).show();
  finish();
  return;
}
在onResume方法上启用分派

@Override
public void onResume() {
  super.onResume();
  Intent intent = new Intent(activity.getApplicationContext(), activity.getClass());
  PendingIntent pendingIntent = PendingIntent.getActivity(activity.getApplicationContext(), 0, intent, 0);
  mNfcAdapter.enableForegroundDispatch(activity, pendingIntent, null, null);
}
@Override
protected void onPause() {
  mNfcAdapter.disableForegroundDispatch(activity);
}
在onPause方法上,禁用分派

@Override
public void onResume() {
  super.onResume();
  Intent intent = new Intent(activity.getApplicationContext(), activity.getClass());
  PendingIntent pendingIntent = PendingIntent.getActivity(activity.getApplicationContext(), 0, intent, 0);
  mNfcAdapter.enableForegroundDispatch(activity, pendingIntent, null, null);
}
@Override
protected void onPause() {
  mNfcAdapter.disableForegroundDispatch(activity);
}
最后重写onNewIntent方法来处理意图

@Override
protected void onNewIntent(Intent intent) {
  //handle intent here
}
在您的例子中,在onNewIntent方法中调用“resolvetent(intent)”
然后按照这篇文章来处理IsoDep标签:

您是否也可以添加
xml/nfc\u tech\u filter
的内容?我试图检测iso B卡,但没有任何成功,标签的读取是完美的。谢谢你的帮助。你得到的未知意图的动作串是什么?您可能还需要重写方法
onNewIntent()