android byte[] 转string 好多问号_android上用NFC刷卡

71213e8856869bba2febdf7299c95eb9.png

NFC on android

NFC on android

2019/5/9 目录

[TOC]

代码下载

Android NFC Demo

e9bd2c0c3f0c3df2e4040b86b6dbd76c.png

目的

手头的门卡,想在android系统上刷出信息来。

代码

网上搜了一下,用android 的 NFC 读卡模块, 能读到卡信息。 参见 参考1.

申请NFC权限

在工程的 AndroidManifest.xml 文件中添加如下代码,用于获取 NFC 硬件访问权限:

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

为Activity 添加 singleTask

仍然在是AndroidManifest.xml,为你的Activity添加singleTask.

<activity android:name="your activity" android:launchMode="singleTask"/>

不给 Activity 添加singleTask的话,每次你刷卡,当前的Activity会被 重新创建一遍,让你无法接收到android发给你的卡片信息。

接收卡片信息

用户刷卡后,android系统会给app发通知。

在app的Activity内,注册一下,就能收到刷卡时的通知。

Activity.onCreate 内初始化NFC

onCreate内,添加以下代码,向NFC系统注册一下,让刷卡后,NFC系统调用你:

``` public boolean init(){ NfcManager mNfcManager = (NfcManager) activity_.getSystemService(Context.NFC_SERVICE); mNfcAdapter = mNfcManager.getDefaultAdapter(); if (mNfcAdapter == null) { message = "tv_nfc_notsupport"; return false; } else if ((mNfcAdapter != null) && (!mNfcAdapter.isEnabled())) { message = "tv_nfc_notwork"; return false; } else if ((mNfcAdapter != null) && (mNfcAdapter.isEnabled())) { message = "tv_nfc_working"; } Class c = activity_.getClass(); mPendingIntent =PendingIntent.getActivity(activity_, 0, new Intent( activity_, c), 0);

init_NFC();
    return true;
}

private void init_NFC() {
    IntentFilter tagDetected = new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED);
    tagDetected.addCategory(Intent.CATEGORY_DEFAULT);
}

```

Activity.onResume内处理一下NFC

public void resume(){ if (mNfcAdapter != null) { mNfcAdapter.enableForegroundDispatch(activity_, mPendingIntent, null, null); Intent i = activity_.getIntent(); String i_a = i.getAction(); if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(i_a)) { String data = processIntent(activity_.getIntent()); } } }

Activity.onPause 内停止接收 NFC

public void stopNFC_Listener() { if(mNfcAdapter!=null) { mNfcAdapter.disableForegroundDispatch(activity_); } }

Activity.onNewIntent 内接收NFC发给你的卡片信息

public String processIntent(Intent intent) { String data = null; Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG); String[] techList = tag.getTechList(); byte[] ID = new byte[20]; data = tag.toString(); ID = tag.getId(); String UID = Utils.bytesToHexString(ID); String IDString = bytearray2Str(hexStringToBytes(UID.substring(2,UID.length())), 0, 4, 10); data += "nnUID:n" + UID; data += "nnID:n" + IDString; data += "nData format:"; for (String tech : techList) { data += "n" + tech; } data += "nwg26status:-->" + PosUtil.getWg26Status(Long.parseLong(IDString)) + "n"; data += "wg34status:-->" + PosUtil.getWg34Status(Long.parseLong(IDString)) + "n"; return data; }

结论

要接收到刷卡信息,只要在AndroidManifest.xml内添加NFC权限,注意也要为你的Actvity添加 singleTask属性。

然后,在你的Activity的onCreate/onResume/onPause/onNewIntent内,添加NFC代码,就能接收到刷卡信息。

参考

  • 参考1: Android NFC开发教程
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值