android NFC开发

从上一篇的NFC支持所类型的卡读取之后,下面要解决的就是NFC的拦截响应,如果这一步没有做,当系统内有多个支持NFC的应用的时候,就会在nfc刷卡的时候弹出多个应用选择,我们需要的场景是,当前应用需要用NFC才去刷卡,然后本应用拦截intent分发(Using the Foreground Dispatch System).

具体的方案:

1.创建PendingIntent来分发要响应的Activity

 

 
  1. mPendingIntent = PendingIntent.getActivity(this, 0,

  2. new Intent(this, NFCActivity.class).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);


2.当刷卡的时候,使用intent过滤器来过滤出你要拦截的Intent

 

 

 
  1. IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);

  2. try {

  3. ndef.addDataType("*/*");

  4. } catch (IntentFilter.MalformedMimeTypeException e) {

  5. throw new RuntimeException("fail", e);

  6. }

  7. intentFiltersArray = new IntentFilter[]{ndef,};


3.设置你要处理的tag technologies到String数组中

 

 

techListsArray = new String[][]{new String[]{NfcA.class.getName()}};


4.在onResume和onPause中设置NFCAdapter

 

 

 
  1. public void onPause() {

  2. super.onPause();

  3. mAdapter.disableForegroundDispatch(this);

  4. }

  5.  
  6. public void onResume() {

  7. super.onResume();

  8. mAdapter.enableForegroundDispatch(this, pendingIntent, intentFiltersArray, techListsArray);

  9. }

当然之前要定义NfcAdapter

 

 

  nfcAdapter = NfcAdapter.getDefaultAdapter(this);

 

 
  1. import android.annotation.SuppressLint;

  2. import android.app.AlertDialog;

  3. import android.content.DialogInterface;

  4. import android.content.Intent;

  5. import android.nfc.NfcAdapter;

  6. import android.nfc.Tag;

  7. import android.os.Bundle;

  8. import android.os.Parcelable;

  9. import android.provider.Settings;

  10. import android.util.Log;

  11. import android.widget.Toast;

  12.  
  13. import com.hwuao.ocis.R;

  14. import com.hwuao.ocis.components.easyAndroid.BaseActivity;

  15. import com.hwuao.ocis.components.easyAndroid.ContentView;

  16. import com.hwuao.ocis.components.easyAndroid.tool.AppLogger;

  17. import com.hwuao.ocis.event.GetNFCCardEvent;

  18. import com.hwuao.ocis.util.StringUtils;

  19. import com.hwuao.ocis.util.ToastUtil;

  20.  
  21.  
  22. /**

  23. * NFC响应页面

  24. *

  25. * @author Jinyang

  26. */

  27. @SuppressLint("NewApi")

  28. @ContentView(R.layout.activity_nfc)

  29. public class NFCActivity extends BaseActivity {

  30.  
  31. public static final String TAG = "NFCActivity";

  32. private NfcAdapter nfcAdapter;

  33.  
  34. private String mClientCardNfcId;

  35.  
  36. @Override

  37. protected void onCreate(Bundle savedInstanceState) {

  38. super.onCreate(savedInstanceState);

  39. initNfcParse();

  40. resolveIntent(getIntent());

  41. }

  42.  
  43. private void initNfcParse() {

  44. nfcAdapter = NfcAdapter.getDefaultAdapter(this);

  45. if (nfcAdapter == null) {

  46. AppLogger.e("---->Nfc error !!!");

  47. Toast.makeText(getApplicationContext(), "不支持NFC功能!", Toast.LENGTH_SHORT).show();

  48. } else if (!nfcAdapter.isEnabled()) {

  49. AppLogger.e("---->Nfc close !!!");

  50. Toast.makeText(getApplicationContext(), "请打开NFC功能!", Toast.LENGTH_SHORT).show();

  51. }

  52. }

  53.  
  54. @Override

  55. protected void onNewIntent(Intent intent) {

  56. super.onNewIntent(intent);

  57. resolveIntent(intent);

  58. }

  59.  
  60. @Override

  61. public void onResume() {

  62. super.onResume();

  63. AppLogger.e("---->onDestroy");

  64. if (nfcAdapter != null) {

  65. if (!nfcAdapter.isEnabled()) {

  66. showWirelessSettingsDialog();

  67. }

  68. }

  69. }

  70.  
  71. private void showWirelessSettingsDialog() {

  72. AlertDialog.Builder builder = new AlertDialog.Builder(this);

  73. builder.setMessage("不支持此卡");

  74. builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {

  75. public void onClick(DialogInterface dialogInterface, int i) {

  76. Intent intent = new Intent(Settings.ACTION_WIRELESS_SETTINGS);

  77. startActivity(intent);

  78. }

  79. });

  80. builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {

  81. public void onClick(DialogInterface dialogInterface, int i) {

  82. finish();

  83. }

  84. });

  85. builder.create().show();

  86. return;

  87. }

  88.  
  89.  
  90. private void resolveIntent(Intent intent) {

  91. String action = intent.getAction();

  92. if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(action)

  93. || NfcAdapter.ACTION_TECH_DISCOVERED.equals(action)

  94. || NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)) {

  95. Parcelable tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);

  96. String nfcId = dumpTagData(tag);

  97. if (!nfcId.isEmpty()) {

  98. mClientCardNfcId = nfcId;

  99. Log.i(TAG, "卡的内容" + mClientCardNfcId);

  100. eventBus.post(new GetNFCCardEvent(mClientCardNfcId));

  101. finish();

  102. } else {

  103. ToastUtil.showToastLong(getApplicationContext(), "识别失败!请重新刷卡!");

  104. }

  105.  
  106. }

  107. }

  108.  
  109.  
  110. private String dumpTagData(Parcelable p) {

  111. Tag tag = (Tag) p;

  112. byte[] id = tag.getId();

  113. return StringUtils.hexToHexString(id);

  114. }

  115.  
  116. }


 

 

 

这个问题的解决体会到一件事,这个问题在国内的博客论坛都没有搜索到,在StackOverFlow上有一篇

 

http://stackoverflow.com/questions/16542147/how-to-read-nfc-tags-to-prevent-system-dialogs-nfc-starter-list-and-tags-being-d

里面的链接指向的是Android develop。。。NFC foreground dispatching

所有的基础教程都可以在developer中找到,以后再也不瞎折腾了,有问题找google

 

源码请见

https://github.com/xujinyang/NFCALL

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值