Reading the tag UID of Mifare classic card
I am creating an app for NFC where my first objective is to get the tag uid from the mifare tag. When i press the tag button my activvity goes to second activity where i should be getting the tagID.
I am getting error of resource lookup. I know i am doing some major mistakes but could not find it.
Request you to please help.
This is my Meanifest file
<?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>
This is my activity one:
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();
}
});
}
}
This is my Activity 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{
Text开发者_开发知识库View 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());
}
}
If the error is about "I am getting error of resource lookup", is not NFC related.
With this code you're getting the UID of the Tag correctly
.
.
.
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));
}
If you are simulating a tag detection from your activity 1, then add the appropriate extras. Else let the android nfc service generate the intent for TECH_DISCOVERED, when it detects the actual tag.
精彩评论