当前位置: 首页 > news >正文

Android -- 读取NFC卡号


1. menifest文件中需要添加:
<uses-permission android:name="android.permission.NFC" />
<uses-feature
    android:name="android.hardware.nfc"
    android:required="true" />






在activity中添加:
<intent-filter>
    <action android:name="android.nfc.action.TAG_DISCOVERED" />
    <category android:name="android.intent.category.DEFAULT" />
</intent-filter>



2. 初始化时代码: 
Intent nfcIntent = new Intent(this, getClass());
nfcIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
mPendingIntent =
        PendingIntent.getActivity(this, 0,nfcIntent , 0);


mAdapter = NfcAdapter.getDefaultAdapter(this);
if (mAdapter == null) {
    Toast.makeText(getApplicationContext(), "NFC feature is supported on this device.", Toast.LENGTH_SHORT).show();
    return;
}




需要Override的函数:
 private NfcAdapter mAdapter;
    private PendingIntent mPendingIntent;
    @TargetApi(Build.VERSION_CODES.GINGERBREAD_MR1)
    protected void onResume() {
        super.onResume();
        mAdapter.enableForegroundDispatch(this, mPendingIntent, null, null);
    }


    @Override
    protected void onNewIntent(Intent intent){
        getTagInfo(intent);
    }
    @TargetApi(Build.VERSION_CODES.GINGERBREAD_MR1)
    private void getTagInfo(Intent intent) {
        Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
        byte[] tagId = tag.getId();
        String str = ByteArrayToHexString(tagId);
        str = flipHexStr(str);
        Long cardNo = Long.parseLong(str, 16);


        String ignoreOperationId = m_operationid;
        if(m_isOnline){
            // if select all , pass in the operation id encoded in ticket
            // there should NOT be many operations, take the only one

            new CardValidationAsyncTask().execute(cardNo.toString());
        }
    }




    @TargetApi(Build.VERSION_CODES.GINGERBREAD_MR1)
    @Override
    protected void onPause() {
        super.onPause();
        if (mAdapter != null) {
            mAdapter.disableForegroundDispatch(this);
        }
    }





以下为helper函数:
   
 private String flipHexStr(String s){
        StringBuilder  result = new StringBuilder();
        for (int i = 0; i <=s.length()-2; i=i+2) {
            result.append(new StringBuilder(s.substring(i,i+2)).reverse());
        }
        return result.reverse().toString();
    }


    private String ByteArrayToHexString(byte[] inarray) {
        int i, j, in;
        String[] hex = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A",
                "B", "C", "D", "E", "F" };
        String out = "";


        for (j = 0; j < inarray.length; ++j) {
            in = (int) inarray[j] & 0xff;
            i = (in >> 4) & 0x0f;
            out += hex[i];
            i = in & 0x0f;
            out += hex[i];
        }
        return out;
    }

相关文章:

  • Windows 7安装以及VS2008和Office2007冲突的问题
  • C# Windows form application 播放小视频
  • 系列文章—内容模型系统开发总结一
  • LeetCode -- Coin chane
  • LeetCode -- Ransom Note
  • LeetCode -- Divide Two Integers
  • 背板带宽
  • IIS中 传递json配置数据最大长度
  • 一道C语言笔试题
  • LeetCode -- Convert a Number to Hexadecimal
  • [Windows编程] 开发DLL必读《Best Practices for Creating DLLs》
  • LeetCode -- Assign Cookies
  • 10g在ia64无法安装的错误
  • LeetCode --Shortest Palindrome
  • 几种第三方数据库工具之比较
  • [分享]iOS开发 - 实现UITableView Plain SectionView和table不停留一起滑动
  • 5分钟即可掌握的前端高效利器:JavaScript 策略模式
  • jquery cookie
  • PV统计优化设计
  • vue 个人积累(使用工具,组件)
  • 快速构建spring-cloud+sleuth+rabbit+ zipkin+es+kibana+grafana日志跟踪平台
  • 使用API自动生成工具优化前端工作流
  • 手机端车牌号码键盘的vue组件
  • 手写双向链表LinkedList的几个常用功能
  • 数据可视化之 Sankey 桑基图的实现
  • 我是如何设计 Upload 上传组件的
  • 吴恩达Deep Learning课程练习题参考答案——R语言版
  • 写代码的正确姿势
  • 用jQuery怎么做到前后端分离
  • 用简单代码看卷积组块发展
  • CMake 入门1/5:基于阿里云 ECS搭建体验环境
  • ​力扣解法汇总946-验证栈序列
  • ​软考-高级-信息系统项目管理师教程 第四版【第23章-组织通用管理-思维导图】​
  • #if 1...#endif
  • #Ubuntu(修改root信息)
  • #微信小程序:微信小程序常见的配置传旨
  • #我与Java虚拟机的故事#连载14:挑战高薪面试必看
  • $NOIp2018$劝退记
  • (附源码)计算机毕业设计SSM疫情下的学生出入管理系统
  • (淘宝无限适配)手机端rem布局详解(转载非原创)
  • (转)http-server应用
  • *** 2003
  • .NET Core 2.1路线图
  • .NET/C# 使用 SpanT 为字符串处理提升性能
  • .NET/C# 阻止屏幕关闭,阻止系统进入睡眠状态
  • /var/lib/dpkg/lock 锁定问题
  • []FET-430SIM508 研究日志 11.3.31
  • [android] 练习PopupWindow实现对话框
  • [Android]竖直滑动选择器WheelView的实现
  • [daily][archlinux][game] 几个linux下还不错的游戏
  • [ERROR]-Error: failure: repodata/filelists.xml.gz from addons: [Errno 256] No more mirrors to try.
  • [flask]http请求//获取请求头信息+客户端信息
  • [HackMyVM]靶场Boxing
  • [IE技巧] IE8中HTTP连接数目的变化
  • [NHibernate]条件查询Criteria Query