2012-06-26 184 views
0

我必须写一个简单的应用程序来读取ISO B证如何读取卡ISODEP NFC

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_TECH_DISCOVERED.equals (行动))。我系统地有“未知意图”。关于

清单:

<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" /> 

感谢您的帮助。

+0

你也可以添加'xml/nfc_tech_filter'的内容吗? <?XML版本= “1.0” 编码= “UTF-8”> –

+0

<资源的xmlns:XLIFF = “瓮:绿洲:名称:TC:XLIFF:文件:1.2”> \t \t 机器人。 nfc.tech.IsoDep user1482868

+0

我尝试检测卡iso B,没有任何成功,标签读取完美。谢谢你的帮助。 – user1482868

回答

0

你的问题似乎是,你在onCreate方法中调用resolveIntent。

这应该做的伎俩:,我只是在我使用

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

     NfcA tag = NfcA.get(tagFromIntent); 

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

    resolveIntent(pIntent); 

} 

为了让标签对象onNewIntent法

不幸的是我无法验证这是NfcB。

0

你需要创建一个NfcAdapter对象

private NfcAdapter mNfcAdapter; 

不是对象与NFC在onCreate方法

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); 
} 
上的onPause方法

禁用调度

@Override 
protected void onPause() { 
    mNfcAdapter.disableForegroundDispatch(activity); 
} 

终于改写onNewIntent的方法来处理这个意图

@Override 
protected void onNewIntent(Intent intent) { 
    //handle intent here 
} 

你的情况叫 “resolveIntent(意图)” 的onNewIntent内方法 比按照这个帖子来处理IsoDep标记:Read data from NFC tag (IsoDep)