我必须编写一个简单的应用程序来读取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;
}
}我的问题是如果(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" />
谢谢你的帮助。
发布于 2012-12-17 23:43:03
您的问题似乎是,您在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验证这一点。
发布于 2014-09-13 07:36:25
您需要创建一个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方法上启用调度
@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
}在本例中,在onNewIntent方法中调用"resolveIntent(intent)“,然后按照本文处理IsoDep标记:Read data from NFC tag (IsoDep)
https://stackoverflow.com/questions/11208486
复制相似问题