Android实现读取NFC卡卡号示例

 更新时间:2017年01月05日 09:09:44   作者:I_Gisvity  
本篇文章主要介绍了Android实现读取NFC卡卡号示例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

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

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

相关文章

  • Android实现圆形渐变加载进度条

    Android实现圆形渐变加载进度条

    这篇文章主要为大家详细介绍了Android实现圆形渐变加载进度条,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-09-09
  • Android控件之RatingBar自定义星级评分样式

    Android控件之RatingBar自定义星级评分样式

    RatingBar为评分条控件,默认效果为若干个绿色的星星,如果想将其换成其他自定义图片就要自定义它的style。接下来通过本文给大家介绍Android控件之RatingBar自定义星级评分样式,感兴趣的朋友一起学习吧
    2016-02-02
  • Android Gradle Build Error:Some file crunching failed, see logs for details的快速解决方法

    Android Gradle Build Error:Some file crunching failed, see l

    这篇文章主要介绍了Android Gradle Build Error:Some file crunching failed, see logs for details的快速解决方法的相关资料,需要的朋友可以参考下
    2016-10-10
  • 简单实现Android验证码

    简单实现Android验证码

    在登录或者注册的时候要求输入验证码,这篇文章主要为大家详细介绍了如何简单实现Android验证码的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-12-12
  • Android自定义ScrollView实现放大回弹效果实例代码

    Android自定义ScrollView实现放大回弹效果实例代码

    本篇文章主要介绍了Android自定义ScrollView实现放大回弹效果实例代码,具有一定的参考价值,有兴趣的可以了解一下。
    2017-03-03
  • Android直播软件搭建之实现背景颜色滑动渐变效果的详细代码

    Android直播软件搭建之实现背景颜色滑动渐变效果的详细代码

    这篇文章主要介绍了Android直播软件搭建之实现背景颜色滑动渐变效果的详细代码,代码简单易懂,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-09-09
  • android自定义View之复合控件

    android自定义View之复合控件

    这篇文章主要为大家详细介绍了android自定义View之复合控件,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-06-06
  • android实现扑克卡片翻转

    android实现扑克卡片翻转

    这篇文章主要为大家详细介绍了android实现扑克卡片翻转,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-05-05
  • Android运用onTouchEvent自定义滑动布局

    Android运用onTouchEvent自定义滑动布局

    这篇文章主要为大家详细介绍了Android运用onTouchEvent写一个上下滑动的布局,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-03-03
  • Android 对话框(Dialog)大全详解及示例代码

    Android 对话框(Dialog)大全详解及示例代码

    本文主要介绍Android 对话框的知识,这里整理了详细资料及实现示例代码及实现效果图,有兴趣的小伙伴可以参考下
    2016-09-09

最新评论