欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android实现读取NFC卡卡号示例

程序员文章站 2024-02-21 10:50:04
android实现读取nfc卡卡号示例,具体如下: 1.权限

android实现读取nfc卡卡号示例,具体如下:

1.权限

  <uses-permission android:name="android.permission.nfc" />
    <uses-feature
    android:name="android.hardware.nfc"
    android:required="true" />

2.注册(静态)

      <intent-filter>
        <action android:name="android.nfc.action.tag_discovered" />
        <data android:mimetype="text/plain" />
      </intent-filter>

3.activity

初始化

    //初始化nfcadapter
    mnfcadapter = nfcadapter.getdefaultadapter(this);
        // 初始化pendingintent,当有nfc设备连接上的时候,就交给当前activity处理
    pi = pendingintent.getactivity(this, 0, new intent(this, getclass())
        .addflags(intent.flag_activity_single_top), 0);

启动

  @override
  protected void onresume() {
    super.onresume();
    mnfcadapter.enableforegrounddispatch(this, pi, null, null); //启动
  }

获取数据

  @override
  protected void onnewintent(intent intent) {
    super.onnewintent(intent);
    // 当前app正在前端界面运行,这个时候有intent发送过来,那么系统就会调用onnewintent回调方法,将intent传送过来
    // 我们只需要在这里检验这个intent是否是nfc相关的intent,如果是,就调用处理方法
    if (nfcadapter.action_tag_discovered.equals(intent.getaction())) {
      processintent(intent);
    }
  }

解析

  /**
   * parses the ndef message from the intent and prints to the textview
   */
  private void processintent(intent intent) {
    //取出封装在intent中的tag
    tag tagfromintent = intent.getparcelableextra(nfcadapter.extra_tag);
    string cardid =bytearraytohexstring(tagfromintent.getid());
  }
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;
  }

4.完整参考

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="cn.com.jslh.zjcdprogrect">

  <uses-permission android:name="android.permission.nfc" />
  <uses-permission android:name="android.permission.internet" />

  <uses-feature
    android:name="android.hardware.nfc"
    android:required="true" />

  <application
    android:name=".common.myapplication"
    android:allowbackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundicon="@mipmap/ic_launcher_round"
    android:supportsrtl="true"
    android:theme="@style/apptheme">
    <activity android:name=".loginactivity">
      <intent-filter>
        <action android:name="android.intent.action.main" />

        <category android:name="android.intent.category.launcher" />
      </intent-filter>
    </activity>
    <activity android:name=".saoka.workactivity">
      <intent-filter>
        <action android:name="android.nfc.action.tag_discovered" />
        <data android:mimetype="text/plain" />
      </intent-filter>
      <!--<meta-data android:name="android.nfc.action.tech_discovered" android:resource="@xml/nfc_tech_filter" />-->
    </activity>
  </application>

</manifest>

package cn.com.jslh.zjcdprogrect.saoka;

import android.app.pendingintent;
import android.content.context;
import android.content.intent;
import android.content.intentfilter;
import android.nfc.nfcadapter;
import android.nfc.tag;
import android.os.bundle;
import android.support.v7.app.appcompatactivity;

import cn.com.jslh.zjcdprogrect.r;

public class workactivity extends appcompatactivity {

  private nfcadapter mnfcadapter;
  private pendingintent pi;
  private intentfilter tagdetected;

  @override
  protected void oncreate(bundle savedinstancestate) {
    super.oncreate(savedinstancestate);
    setcontentview(r.layout.activity_work);

    //初始化nfcadapter
    mnfcadapter = nfcadapter.getdefaultadapter(this);
    //初始化pendingintent
    // 初始化pendingintent,当有nfc设备连接上的时候,就交给当前activity处理
    pi = pendingintent.getactivity(this, 0, new intent(this, getclass())
        .addflags(intent.flag_activity_single_top), 0);
    // 新建intentfilter,使用的是第二种的过滤机制
//    tagdetected = new intentfilter(nfcadapter.action_tech_discovered);
//    tagdetected.addcategory(intent.category_default);
  }

  @override
  protected void onnewintent(intent intent) {
    super.onnewintent(intent);
    // 当前app正在前端界面运行,这个时候有intent发送过来,那么系统就会调用onnewintent回调方法,将intent传送过来
    // 我们只需要在这里检验这个intent是否是nfc相关的intent,如果是,就调用处理方法
    if (nfcadapter.action_tag_discovered.equals(intent.getaction())) {
      processintent(intent);
    }
  }

  @override
  protected void onresume() {
    super.onresume();
    mnfcadapter.enableforegrounddispatch(this, pi, null, null);
  }

  /**
   * parses the ndef message from the intent and prints to the textview
   */
  private void processintent(intent intent) {
    //取出封装在intent中的tag
    tag tagfromintent = intent.getparcelableextra(nfcadapter.extra_tag);
    string cardid =bytearraytohexstring(tagfromintent.getid());
  }

  public static void startactivity(context context){
    intent intent = new intent();
    intent.setclass(context,workactivity.class);
    context.startactivity(intent);
  }

  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;
  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。