我创造NFC应用程序在我的第一个目标是获得从MIFARE标签的标签UID。当我preSS标签按钮,我activvity去的地方,我应该得到的TAGID第二个活动。
我收到资源查找的错误。我知道我在做一些重大的错误,但无法找到它。
请你,请帮助。
这是我的Meanifest文件
<?XML版本=1.0编码=UTF-8&GT?;<清单的xmlns:机器人=http://schemas.android.com/apk/res/android 包=com.chetan.nfc 安卓版code =1 机器人:=的versionName1.0><采用-SDK安卓的minSdkVersion =10>< /使用-SDK><使用特征的android:要求=真正的机器人:名字=android.hardware.nfc>< /使用特征><使用许可权的android:NAME =android.permission.NFC>< /使用许可权><应用机器人:图标=@绘制/图标机器人:标签=@字符串/ APP_NAME机器人:可调试=真正的机器人:启用=真正的> <活动机器人:名字=。actOne 机器人:标签=@字符串/ APP_NAME> &所述;意图滤光器> <作用机器人:名字=android.intent.action.MAIN/> <类机器人:名字=android.intent.category.LAUNCHER/> &所述; /意图滤光器> < /活性GT; <活动机器人:名字=taginfo机器人:标签=@字符串/ APP_NAME机器人:launchMode =标准> &所述;意图滤光器> <作用机器人:名字=android.nfc.action.TECH_DISCOVERED/> <类机器人:名字=android.intent.category.DEFAULT/> &所述; /意图滤光器> <元数据的android:资源=@ XML / nfc_tech_filter机器人:名字=@字符串/ nfc_tech_filter>< /元数据> < /活性GT;< /用途>< /清单>
这是我的活动之一:
包com.chetan.nfc;进口java.util.Date;进口android.app.Activity;进口android.app.LauncherActivity;进口android.content.Intent;进口android.content.IntentFilter;进口android.nfc.NfcAdapter;进口android.nfc.tech.NdefFormatable;进口android.os.Bundle;进口android.os.Parcelable;进口android.view.View;进口android.view.View.OnClickListener;进口android.widget.Button;进口android.widget.TextView;公共类actOne延伸活动{/ **当第一次创建活动调用。 * /按钮标签;我的意图;TextView的电视;公共无效的onCreate(捆绑savedInstanceState){ super.onCreate(savedInstanceState); 的setContentView(R.layout.main); 标签=(按钮)findViewById(R.id.Tag); 电视=(的TextView)findViewById(R.id.textView1); tag.setOnClickListener(新Button.OnClickListener() { 公共无效的onClick(视图v) { I =新意图(v.getContext(),taginfo.class); startActivity(ⅰ); // launchActivity(); } });}}
这是我的活动2
包com.chetan.nfc;进口java.io.IOException异常;进口android.app.Activity;进口android.app.PendingIntent;进口android.content.Intent;进口android.content.IntentFilter;进口android.content.IntentFilter.MalformedMimeTypeException;进口android.nfc.NfcAdapter;进口android.nfc.Tag;进口android.nfc.tech.MifareClassic;进口android.os.Bundle;进口android.os.Parcelable;进口android.util.Log;进口android.widget.EditText;进口android.widget.TextView;公共类taginfo延伸活动{TextView的电视;的EditText tagIntent;日志中记录;IntentFilter的mFilters [];串mTechLists [] [];/ **当第一次创建活动调用。 * /@覆盖公共无效的onCreate(捆绑savedState){super.onCreate(savedState);的setContentView(R.layout.tagdata);tagIntent =(EditText上)findViewById(R.id.editText1);电视=(的TextView)findViewById(R.id.textView1);tv.setText(getIntent()的toString());log.e(前拿到intetn,);readTag(getIntent());}公共无效readTag(意向意图){ //tagIntent.setText(intent.getAction()); 的PendingIntent mPendingIntent = PendingIntent.getActivity(这一点,0, 新意图(本,的getClass())addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP),0)。 NfcAdapter nfc_adapter = NfcAdapter.getDefaultAdapter(本);IntentFilter的NDEF =新的IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED);标签MyTag的=(TAG)intent.getParcelableExtra(NfcAdapter.EXTRA_TAG); 字节[] = TAGID intent.getByteArrayExtra(NfcAdapter.EXTRA_ID); tagIntent.setText(); tagIntent.setText(tagId.toString());}}
解决方案
如果错误是关于我得到资源查找的错误,是不是NFC相关。
有了这个code你正确地获取标签的UID
。。。标签标签= intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);Log.d(TAG,UID:+ BIN2HEX(tag.getId()));。。。//要显示UID静态字符串BIN2HEX(字节[]数据){ 返回的String.format(%0+(data.length * 2)+X的,新的BigInteger(1,数据));}
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{
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());
}
}
解决方案
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));
}