2011-05-06 282 views
1

我正在为NFC创建应用程序,其中第一个目标是从mifare标签获取标签uid。 当我按下标签按钮时,我的活动进入第二个活动,我应该获取tagID。读取Mifare经典卡的标签UID

我收到资源查找错误。我知道我正在犯一些重大错误,但找不到它。

请求你帮忙。

这是我Meanifest文件

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.chetan.nfc" 
    android:versionCode="1" 
    android:versionName="1.0"> 
<uses-sdk android:minSdkVersion="10"></uses-sdk> 
<uses-feature android:required="true" android:name="android.hardware.nfc"></uses-feature> 
<uses-permission android:name="android.permission.NFC"></uses-permission> 


<application android:icon="@drawable/icon" android:label="@string/app_name" android:debuggable="true" android:enabled="true"> 
    <activity android:name=".actOne" 
       android:label="@string/app_name"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 
      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
     </activity> 
    <activity android:name="taginfo" android:label="@string/app_name" android:launchMode="standard"> 
     <intent-filter> 
     <action android:name="android.nfc.action.TECH_DISCOVERED"/> 
     <category android:name="android.intent.category.DEFAULT"/>   
     </intent-filter> 
     <meta-data android:resource="@xml/nfc_tech_filter" android:name="@string/nfc_tech_filter"></meta-data> 

    </activity> 

</application> 

</manifest> 

这是我的活动之一:

package com.chetan.nfc; 

import java.util.Date; 

import android.app.Activity; 
import android.app.LauncherActivity; 
import android.content.Intent; 
import android.content.IntentFilter; 
import android.nfc.NfcAdapter; 
import android.nfc.tech.NdefFormatable; 
import android.os.Bundle; 
import android.os.Parcelable; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.TextView; 

public class actOne extends Activity { 
/** Called when the activity is first created. */ 

Button tag; 
Intent i; 
TextView tv; 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    tag=(Button)findViewById(R.id.Tag); 
    tv=(TextView)findViewById(R.id.textView1); 

    tag.setOnClickListener(new Button.OnClickListener() 
    { 
     public void onClick(View v) 
     { 
      i=new Intent(v.getContext(),taginfo.class); 
      startActivity(i); 


     //launchActivity(); 
     } 
    }); 

} 
} 

这是我的活动2

package com.chetan.nfc; 

import java.io.IOException; 

import android.app.Activity; 
import android.app.PendingIntent; 
import android.content.Intent; 
import android.content.IntentFilter; 
import android.content.IntentFilter.MalformedMimeTypeException; 
import android.nfc.NfcAdapter; 
import android.nfc.Tag; 
import android.nfc.tech.MifareClassic; 
import android.os.Bundle; 
import android.os.Parcelable; 
import android.util.Log; 
import android.widget.EditText; 
import android.widget.TextView; 

public class taginfo extends Activity{ 
TextView tv; 
EditText tagIntent; 
Log log; 
IntentFilter mFilters[]; 
String mTechLists[][]; 
/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedState) { 
super.onCreate(savedState); 
setContentView(R.layout.tagdata); 
tagIntent=(EditText)findViewById(R.id.editText1); 
tv=(TextView)findViewById(R.id.textView1); 
tv.setText(getIntent().toString()); 

log.e("before get intetn", ""); 

readTag(getIntent()); 
} 

public void readTag(Intent intent) 
{ 
    //tagIntent.setText(intent.getAction()); 

    PendingIntent mPendingIntent = PendingIntent.getActivity(this, 0, 
      new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0); 

    NfcAdapter nfc_adapter=NfcAdapter.getDefaultAdapter(this); 
IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED); 

Tag myTag = (Tag) intent.getParcelableExtra(NfcAdapter.EXTRA_TAG); 
    byte[] tagId = intent.getByteArrayExtra(NfcAdapter.EXTRA_ID); 
    tagIntent.setText(""); 
    tagIntent.setText(tagId.toString());  
} 
} 
+0

您可以发布错误的UID? – Pasha 2011-05-06 11:51:51

回答

0

如果要模拟从你的标签检测活动1,然后添加适当的演员。否则,让Android nfc服务在检测到实际标记时生成TECH_DISCOVERED的意图。

4

如果错误是关于“我正在获取资源查找错误”,与NFC不相关。

有了这个代码,你得到的标签正确

. 
. 
. 
Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG); 
Log.d(TAG, "UID: " + bin2hex(tag.getId())); 
. 
. 
. 
//To display the UID 
static String bin2hex(byte[] data) { 
    return String.format("%0" + (data.length * 2) + "X", new BigInteger(1,data)); 
}