Android NFC基础知识汇总

android.nfc.tech.NfcF

android.nfc.tech.NfcV

android.nfc.tech.Ndef

android.nfc.tech.NdefFormatable

android.nfc.tech.MifareUltralight

android.nfc.tech.MifareClassic

在res下新建xml,然后新建一个xxx.xml的文件将上方内容copy即可。然后在AndroidManifest.xml中,添加NFC权限:

后将要做NFC功能的界面的launchMode设置为singleTop类型,然后添加meta-data内容,如下:

<meta-data

android:name=“android.nfc.action.TECH_DISCOVERED”

android:resource=“@xml/nfc_tech_filter”/>

然后在activity中oncreate和onresume中初始化NFC。

private void initNfc() {

defaultAdapter = NfcAdapter.getDefaultAdapter(this);

if (null == defaultAdapter) {

Toast.makeText(this, “当前设备不支持NFC功能”, Toast.LENGTH_SHORT).show();

return;

}

try {

if (!defaultAdapter.isEnabled()) {

Toast.makeText(this, “请打开NFC功能”, Toast.LENGTH_SHORT).show();

return;

}

} catch (Exception e) {

return;

}

mPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()), 0);

}

然后重写onNewInstance方法中通过

判断当前具备NFC功能的标签属于那种格式协议的。ndef格式举例:

if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())) {

Log.e(“===========”, "processIntent:2 ");

Tag tag = intent.getParcelableExtra(defaultAdapter.EXTRA_TAG);

if (tag == null)

return;

String[] techList = tag.getTechList();

byte[] id = tag.getId();

tagIdStr = “ID:” + Utils.bytesToHexString(id) + “\n”;

tagIdStr += “type:” + tag.describeContents() + “\n”;

boolean is15693 = false;

for (String tecj : techList) {

tagIdStr += tecj + “\n”;

if (tecj.equals(“android.nfc.tech.NfcV”)) {

is15693 = true;

}

}

}

public class Utils {

public static String bytesToHexString(byte[] src){

StringBuilder stringBuilder = new StringBuilder(“”);

if (src == null || src.length <= 0) {

return null;

}

for (int i = 0; i < src.length; i++) {

int v = src[i] & 0xFF;

String hv = Integer.toHexString(v);

if (hv.length() < 2) {

stringBuilder.append(0);

}

stringBuilder.append(hv);

}

return stringBuilder.toString();

}

public static byte[] stringToBytes(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 byte uniteBytes(byte src0, byte src1) {

byte _b0 = Byte.decode(“0x” + new String(new byte[] {src0})).byteValue();

_b0 = (byte) (_b0 << 4);

byte _b1 = Byte.decode(“0x” + new String(new byte[] { src1 })).byteValue();

byte ret = (byte) (_b0 ^ _b1);

return ret;

}

private static byte charToByte(char c) {

return (byte) “0123456789ABCDEF”.indexOf©;

}

public static byte[] hexString2Bytes(String src) {

if (null == src || 0 == src.length()) {

return null;

}

byte[] ret = new byte[src.length() / 2];

byte[] tmp = src.getBytes();

for (int i = 0; i < (tmp.length / 2); i++) {

ret[i] = uniteBytes(tmp[i * 2], tmp[i * 2 + 1]);

}

return ret;

}

}

这样基本上就可以了实现数据读取了。本文只针对于富立叶PDA NFC读取功能。代码请关注微信公众号“只会写bug的咸鱼”,回复NFC获取。如有问题可私信或者微信公众号留言。

创作不易,不喜勿喷!!!

  • 20
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值