
1个回答
2016-05-21 · 百度知道合伙人官方认证企业
1【专注:Python+人工智能|Java大数据|HTML5培训】 2【免费提供名师直播课堂、公开课及视频教程】 3【地址:北京市昌平区三旗百汇物美大卖场2层,微信公众号:yuzhitc】
向TA提问
关注

展开全部
NFC读卡器的Android开发:
1. 配置Manifest
配置权限与feature
<!--Use NFC feature and Permissions-->
<uses-permission android:name="android.permission.NFC"/>
<uses-feature
android:name="android.hardware.nfc"
android:required="true"/>
注意配置Activity的模式为singleTop,配置Activity不可转变屏幕(防止Intent丢失,支付宝也是这样做的),配置NFCTech过滤器,配置Intent接收器
<activity
android:name=".NfcReaderActivity"
android:launchMode="singleTop"
android:alwaysRetainTaskState="true"
android:label="@string/title_activity_nfcscanner">
<!--nfc filter-->
<meta-data android:name="android.nfc.action.TECH_DISCOVERED" android:resource="@xml/nfc_tech_filter" />
<intent-filter>
<action android:name="android.nfc.action.TECH_DISCOVERED" />
<action android:name="android.nfc.action.TAG_DISCOVERED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
配置NFC卡片技术过滤器,每个卡对应一个<tech-list>
<!--file nfc_tech_filter.xml -->
<resources>
<!--一卡通-->
<tech-list>
<!--ISO 14443-4-->
<tech>android.nfc.tech.IsoDep</tech>
<!--ISO 14443-3A-->
<tech>android.nfc.tech.NfcA</tech>
</tech-list>
</resources>
2. 获取Intent
在Activity中针对收到的Intent进行处理,onCreate是从其他App进入的情况,onNewIntent是已经在本App中收到新的Intent的情况
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_nfcscanner);
handleIntent(getIntent());
}
@Override protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
handleIntent(intent);
}
接下来处理Intent,获取到Tag对象
private void handleIntent(Intent intent) {
//获取到Intent的Action,注意多打Log
Log.d(TAG, "handleIntent: " + intent.getAction());
if (!intent.getAction().equals(NfcAdapter.ACTION_TECH_DISCOVERED)) {
Log.d(TAG, "handleIntent: no valid action");
return;
}
//获取Tag对象
tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
//获取卡ID,这个ID一般没什么用,有可能是卡自动生成的
Log.d(TAG, "Id:" + Util.byteArraytoHexString(tag.getId()));
//NFC卡片所支持的技术标准
Log.d(TAG, "TechList:" + Arrays.toString(tag.getTechList()));
}
1. 配置Manifest
配置权限与feature
<!--Use NFC feature and Permissions-->
<uses-permission android:name="android.permission.NFC"/>
<uses-feature
android:name="android.hardware.nfc"
android:required="true"/>
注意配置Activity的模式为singleTop,配置Activity不可转变屏幕(防止Intent丢失,支付宝也是这样做的),配置NFCTech过滤器,配置Intent接收器
<activity
android:name=".NfcReaderActivity"
android:launchMode="singleTop"
android:alwaysRetainTaskState="true"
android:label="@string/title_activity_nfcscanner">
<!--nfc filter-->
<meta-data android:name="android.nfc.action.TECH_DISCOVERED" android:resource="@xml/nfc_tech_filter" />
<intent-filter>
<action android:name="android.nfc.action.TECH_DISCOVERED" />
<action android:name="android.nfc.action.TAG_DISCOVERED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
配置NFC卡片技术过滤器,每个卡对应一个<tech-list>
<!--file nfc_tech_filter.xml -->
<resources>
<!--一卡通-->
<tech-list>
<!--ISO 14443-4-->
<tech>android.nfc.tech.IsoDep</tech>
<!--ISO 14443-3A-->
<tech>android.nfc.tech.NfcA</tech>
</tech-list>
</resources>
2. 获取Intent
在Activity中针对收到的Intent进行处理,onCreate是从其他App进入的情况,onNewIntent是已经在本App中收到新的Intent的情况
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_nfcscanner);
handleIntent(getIntent());
}
@Override protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
handleIntent(intent);
}
接下来处理Intent,获取到Tag对象
private void handleIntent(Intent intent) {
//获取到Intent的Action,注意多打Log
Log.d(TAG, "handleIntent: " + intent.getAction());
if (!intent.getAction().equals(NfcAdapter.ACTION_TECH_DISCOVERED)) {
Log.d(TAG, "handleIntent: no valid action");
return;
}
//获取Tag对象
tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
//获取卡ID,这个ID一般没什么用,有可能是卡自动生成的
Log.d(TAG, "Id:" + Util.byteArraytoHexString(tag.getId()));
//NFC卡片所支持的技术标准
Log.d(TAG, "TechList:" + Arrays.toString(tag.getTechList()));
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询