有关NFC操作的疑问...

彩虹蜡染的天色 2014-04-16 03:33:56
 
<uses-feature android:name="android.hardware.nfc" android:required="true" />

<uses-permission android:name="android.permission.NFC" />
<intent-filter>
<action android:name="android.nfc.action.TECH_DISCOVERED" />
</intent-filter>

<meta-data android:name="android.nfc.action.TECH_DISCOVERED"
android:resource="@xml/filter_nfc"
/>




<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
<!-- capture anything using NfcF -->
<tech-list>
<tech>android.nfc.tech.NfcA</tech>
<tech>android.nfc.tech.MifareClassic</tech>
<tech>android.nfc.tech.MifareUltralight</tech>

</tech-list>

</resources>



isNFC = getPackageManager().hasSystemFeature(PackageManager.FEATURE_NFC);
if (isNFC) {
mAdapter = NfcAdapter.getDefaultAdapter(this);

mPendingIntent = PendingIntent.getActivity(this, 0,
new Intent(this, getClass())
.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
// Setup an intent filter for all MIME based dispatches
IntentFilter ndef = new IntentFilter(
NfcAdapter.ACTION_TECH_DISCOVERED);
ndef.addAction(NfcAdapter.ACTION_TAG_DISCOVERED);
try {
ndef.addDataType("*/*");
} catch (MalformedMimeTypeException e) {
Log.e("NFCfail", "runtime Exception");
}

mFilters = new IntentFilter[] { ndef };

// Setup a tech list for all MifareClassic tags
mTechLists = new String[][] {
new String[] { MifareClassic.class.getName() },
new String[] { NfcA.class.getName() } };

}




protected void onResume() {
super.onResume();

if (isNFC && mAdapter.isEnabled()) {

mAdapter.enableForegroundDispatch(this, mPendingIntent, mFilters,
mTechLists);
}


}



感应卡之后在onNewIntent中获取到的数据如下:
public void onNewIntent(Intent intent) {
String action = intent.getAction();
// action打印输出为:android.nfc.action.TECH_DISCOVERED

Tag tagFromIntent = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
// tagFromIntent 打印输出为:TAG: Tech [android.nfc.tech.NfcA]

问题是:
MifareClassic mfc = MifareClassic.get(tagFromIntent);
//这里mfc打印输出为null 这是为什么呢

}


谢谢!

...全文
116 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
求解答啊啊啊

80,337

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧