一、NFC简介
NFC是一种近距离无线通信技术,一种非接触式的识别和互联技术。
NFC由非接触式射频识别标准(RFID)及互联互通技术演变而成,通过在芯片上集成了感应式读卡器、感应式卡片和点对点三种功能,能够近距离进行识别和数据交换。
NFC支持双向连接和识别,通过NFC技术,可以简化整个认证识别过程,使电子设备间互相访问更直接、更安全和更清楚。
NFC Forum 负责制定NFC相关的技术标准,通过NFC认证测试,保证各厂家的NFC产品符合NFC规范。
二、工具类封装
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.nfc.NfcAdapter;
import android.nfc.NfcManager;
import android.nfc.Tag;
import com.gmtech.sensormodule.GmSensorManager;
import com.gmtech.sensormodule.data.SensorCard;
/**
* @author francisbingo on 5/8/21 3:05 PM
*/
public class TbNfcCardUtil {
private static NfcAdapter mNfcAdapter;
public TbNfcCardUtil() {
}
public static void initNFC(Context context, NfcAdapter.ReaderCallback callback) {
NfcManager mNfcManager = (NfcManager) context.getSystemService(Context.NFC_SERVICE);
mNfcAdapter = mNfcManager.getDefaultAdapter();
// PendingIntent mPendingIntent = PendingIntent.getActivity(context, 0, new Intent(context, context.getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
// mNfcAdapter.enableForegroundDispatch((Activity) context, mPendingIntent, null, null);
IntentFilter tagDetected = new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED);
tagDetected.addCategory(Intent.CATEGORY_DEFAULT);
if (mNfcAdapter != null) {
mNfcAdapter.enableReaderMode((Activity) context,
callback,
NfcAdapter.FLAG_READER_NFC_A |
NfcAdapter.FLAG_READER_NFC_B |
NfcAdapter.FLAG_READER_NFC_F |
NfcAdapter.FLAG_READER_NFC_V |
NfcAdapter.FLAG_READER_NFC_BARCODE |
NfcAdapter.FLAG_READER_NO_PLATFORM_SOUNDS,
null);
}
}
public static void unregistNFC(Activity context) {
if (mNfcAdapter != null) {
mNfcAdapter.disableForegroundDispatch(context);
mNfcAdapter = null;
context = null;
}
}
public static String bytesToHexString(byte[] src) {
StringBuilder stringBuilder = new StringBuilder("0x");
if (src != null && src.length > 0) {
char[] buffer = new char[2];
for (int i = 0; i < src.length; ++i) {
buffer[0] = Character.forDigit(src[i] >>> 4 & 15, 16);
buffer[1] = Character.forDigit(src[i] & 15, 16);
stringBuilder.append(buffer);
}
return stringBuilder.toString();
} else {
return null;
}
}
public static String toHexString(byte[] data) {
if (data == null) {
return "";
} else {
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < data.length; ++i) {
String string = Integer.toHexString(data[i] & 255);
if (string.length() == 1) {
stringBuilder.append("0");
}
stringBuilder.append(string.toUpperCase());
}
return stringBuilder.toString();
}
}
public static byte[] toBytes(String string) {
String hexStr = "0123456789ABCDEF";
String s = string.toUpperCase();
int len = s.length();
String str;
if (len % 2 == 1) {
str = s + "0";
len = len + 1 >> 1;
} else {
str = s;
len >>= 1;
}
byte[] bytes = new byte[len];
int i = 0;
for (int j = 0; i < len; j += 2) {
byte high = (byte) (hexStr.indexOf(str.charAt(j)) << 4);
byte low = (byte) hexStr.indexOf(str.charAt(j + 1));
bytes[i] = (byte) (high | low);
++i;
}
return bytes;
}
private static byte charToByte(char c) {
return (byte) "0123456789ABCDEF".indexOf(c);
}
public static byte[] hexStringToBytes(String hexString) {
if (hexString == null || hexString.equals("")) {
return null;
}
hexString = hexString.toUpperCase();
int length = hexString.length() / 2;
char[] hexChars = hexString.toCharArray();
byte[] d = new byte[length];
for (int i = 0; i < length; i++) {
int pos = i * 2;
d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
}
return d;
}
public static String bytearray2Str(byte[] data, int start, int length, int targetLength) {
long number = 0;
if (data.length < start + length) {
return "";
}
for (int i = 1; i <= length; i++) {
number *= 0x100;
number += (data[start + length - i] & 0xFF);
}
return String.format("%0" + targetLength + "d", number);
}
// 读卡开锁
public static void proessTag(Tag tag) {
if (tag == null) {
return;
}
byte[] ID = new byte[20];
ID = tag.getId();
String UID = TbNfcCardUtil.bytesToHexString(ID);
String IDString = TbNfcCardUtil.bytearray2Str(TbNfcCardUtil.hexStringToBytes(UID.substring(2, UID.length())), 0, 4, 10);
}
}
三、工具类的使用
1.实现在多个Activity监听IC卡的读取
在onCreate中调用initNFC方法 在onPause中调用unregistNFC方法
在回调onTagDiscovered 方法中做读取IC卡的处理
TbNfcCardUtil.proessTag(tag);