我必须编写一个简单的应用来阅读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;
}
}
我的问题是我没有成功进入测试if(NfcAdapter.ACTION_TECH_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" />
感谢您的帮助。
答案 0 :(得分:0)
您的问题似乎是,您正在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);
在我的onNewIntent-Method
中不幸的是我无法为NfcB验证这一点。
答案 1 :(得分:0)
您需要创建一个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方法的启用Dispatch
@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方法上的禁用Dispatch
@Override
protected void onPause() {
mNfcAdapter.disableForegroundDispatch(activity);
}
最后覆盖onNewIntent方法来处理意图
@Override
protected void onNewIntent(Intent intent) {
//handle intent here
}
在你的情况下,在onNewIntent方法中调用“resolveIntent(intent)” 而不是按照这篇文章来处理IsoDep标签:Read data from NFC tag (IsoDep)