iis服务器助手广告广告
返回顶部
首页 > 资讯 > 移动开发 >Android实现读取NFC卡卡号示例
  • 634
分享到

Android实现读取NFC卡卡号示例

示例nfcAndroid 2022-06-06 04:06:21 634人浏览 薄情痞子
摘要

Android实现读取NFC卡卡号示例,具体如下: 1.权限 <uses-permission android:name="android.permission.

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

解析


  
  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);
  }
  
  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平台上读写NFC标签android nfc常用标签读取总结android模拟器开发测试nfc应用实例详解深入分析Android NFC技术 android nfc开发


--结束END--

本文标题: Android实现读取NFC卡卡号示例

本文链接: https://www.lsjlt.com/news/22726.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作