前言:由于是初次接触nfc功能,照搬前辈的代码啊,还是网上找都是模模糊糊,而且网上资料也……于是决定自己写一些,有些设计到公司秘密,点到为止。
第一步、配置基础信息
1,AndroidMainfest配置,
开发需要配置权限:
<!-- nfc使用权限 -->
<uses-feature
android:name="android.hardware.nfc"
android:required="true"/>
2,xml中TAG标签配置
3,对应读写卡acitivity的配置
<activity
android:name=".ui.activity"
android:configChanges="keyboardHidden|orientation|screenSize"
android:screenOrientation="portrait"
android:windowSoftInputMode="adjustPan|stateHidden">
<intent-filter>
<action android:name="android.nfc.action.TECH_DISCOVERED"/>
</intent-filter>
<meta-data
android:name="android.nfc.action.TECH_DISCOVERED"
android:resource="@xml/nfc_tech_filter"/>
</activity>
第二步、判断设备是否有nfc功能
/**
* 项目名称:MoveOrder
* 创建人:kexiang
* 创建时间:2017/1/25 14:44
*/
public class NFCmanager {
private NfcAdapter mNfcAdapter;
@SuppressLint("NewApi")
public void init(Activity activity) {
mNfcAdapter = NfcAdapter.getDefaultAdapter(activity);
// 判断2
if (mNfcAdapter == null) {
// 如果手机不支持NFC,或者NFC没有打开就直接返回
Toast.makeText(activity, "手机不支持NFC功能!", Toast.LENGTH_SHORT).show();
// activity.finish();
return;
}
if (!mNfcAdapter.isEnabled()) {
Toast.makeText(activity, "手机NFC功能没有打开!", Toast.LENGTH_SHORT).show();
enableDialog(activity);
}
mPendingIntent = PendingIntent.getActivity(
activity,
0,
new Intent(
activity,
activity.getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP
)
, 0
);
}
private void enableDialog(final Activity activity) {
AlertDialog.Builder ab = new AlertDialog.Builder(activity);
ab.setTitle("提醒");
ab.setMessage("手机NFC开关未打开,是否现在去打开?");
ab.setNeutralButton("否", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
ab.setNegativeButton("是", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
activity.startActivity(new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS));
dialog.dismiss();
activity.finish();
}
});
ab.create().show();
}
@SuppressLint("NewApi")
public void onPause(Activity activity) {
if (mNfcAdapter != null) {
mNfcAdapter.disableForegroundDispatch(activity);
mNfcAdapter.disableForegroundNdefPush(activity);
}
}
private PendingIntent mPendingIntent;
@SuppressLint("NewApi")
public void onResume(Activity activity) {
if (mNfcAdapter != null) {
mNfcAdapter.enableForegroundDispatch(activity, mPendingIntent, null, null);
}
}
}
onPause(Activity activity)和onResume(Activity activity)方法在对应的Activity中响应的生命周期调用
第三步、读写卡
* Created by kexiang on 2017-01-06.
*/
public class M1NFCreadAndWrite {
public NfcInfoBean backNFCsetBean(Intent intent,
int sectors,
String miMa){
//判断返回的tag是否是响应tag
if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(action) ||
NfcAdapter.ACTION_TAG_DISCOVERED.equals(action)) {
// 取出封装在intent中的TAG
Tag tagFromIntent = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
//读取物理卡号
String cardID16 = bytesToHexString(tagFromIntent.getId());
MifareClassic mfc = MifareClassic.get(tagFromIntent);
try {
//连接
mfc.connect();
//判断读取扇区是否成功,成功既可以读写卡
boolean auth = mfc.authenticateSectorWithKeyA(sectors, HEX.hexToBytes(miMa));
if (auth) {
readCard(nfcInfoBean, mfc);
writeCard(nfcInfoBean, context, daoOrderTrade, mfc);
}
}
} catch (Exception e) {
} finally {
// if (mfc.isConnected()){
// try {
// mfc.close();
// } catch (IOException e1) {
// e1.printStackTrace();
// }
// }
}
//读卡
private void readCard(NfcInfoBean nfcInfoBean, MifareClassic mfc) throws IOException {
byte[] dataMoney = mfc.readBlock(nfcInfoBean.getReadAndWritePiece());
}
//写卡
private void writeCard(
Context context,
MifareClassic mfc) {
mfc.writeBlock(nfcInfoBean.getReadAndWritePiece(),
HEX.hexToBytes(nfcWrite));
}
}
nfc读写卡功能基本是这样的……